1 | <?php
|
---|
2 |
|
---|
3 | /*
|
---|
4 | * Criado em 21/10/2008
|
---|
5 | *
|
---|
6 | * Autor: André Alexandre Ávila/DIDES-C1/Celepar
|
---|
7 | * Projeto NatPHPDC1
|
---|
8 | *
|
---|
9 | */
|
---|
10 | /**
|
---|
11 | ====================== FORMATO ======================
|
---|
12 | [FIELD] => array
|
---|
13 | [type] = N(numeric), S(String), A(Array)
|
---|
14 | [length] = 6(six numbers), 9.2 or 9,2
|
---|
15 | if have array
|
---|
16 | [length] number of loops
|
---|
17 | [fields] = fields of array
|
---|
18 | =>
|
---|
19 | [FIELD] => array
|
---|
20 | [type]
|
---|
21 | [length]
|
---|
22 | [fields]
|
---|
23 |
|
---|
24 | ================ FORMATO DO RETORNO =================
|
---|
25 | array ('name' => 'value')
|
---|
26 | array ('name' => array(
|
---|
27 | 0 => array(
|
---|
28 | name1 => value1,
|
---|
29 | name2 => value2,
|
---|
30 | name3 => value3
|
---|
31 | )))
|
---|
32 | =====================================================
|
---|
33 | * Prioridade de tratamento:
|
---|
34 | * 1 length
|
---|
35 | * 2 div
|
---|
36 | * 3 array e str
|
---|
37 | * 4 funcarr e funcstr
|
---|
38 | * 5 trim global
|
---|
39 | */
|
---|
40 |
|
---|
41 | class PosString {
|
---|
42 | public $trim = false;
|
---|
43 | private $types = null;
|
---|
44 | public $debug = false;
|
---|
45 | public $clearStep = true;
|
---|
46 | public $dbgln = array ();
|
---|
47 |
|
---|
48 | function __construct($type) {
|
---|
49 | $this->types = & $type;
|
---|
50 | }
|
---|
51 | /**
|
---|
52 | Funcao sprintf com suporte a array
|
---|
53 | */
|
---|
54 | private function sprintf_array($string, $array) {
|
---|
55 | $keys = array_keys($array);
|
---|
56 | $keysmap = array_flip($keys);
|
---|
57 | $values = array_values($array);
|
---|
58 | array_unshift($values, $string);
|
---|
59 | return call_user_func_array('sprintf', $values);
|
---|
60 | }
|
---|
61 | /**
|
---|
62 | * Monta String de acordo com o formato
|
---|
63 | * @param Array Formato
|
---|
64 | * @param Array Dados Array
|
---|
65 | * @return String String Posicional
|
---|
66 | */
|
---|
67 | public function mountString($format, $dataArr) {
|
---|
68 | $dataS = '';
|
---|
69 | $iz = 0;
|
---|
70 | foreach ($format as $fieldName => $fieldDef) {
|
---|
71 | $type = $this->types->getType($fieldDef['type']);
|
---|
72 | if (!isset ($type['loop'])) {
|
---|
73 | $length = 0;
|
---|
74 | $value = '';
|
---|
75 | // procura o length
|
---|
76 | if (isset ($type['length']))
|
---|
77 | $length = $type['length'];
|
---|
78 | if (isset ($fieldDef['length']))
|
---|
79 | $length = $fieldDef['length'];
|
---|
80 | if (isset ($dataArr[$fieldName]))
|
---|
81 | $value = $dataArr[$fieldName];
|
---|
82 | if (isset ($type['div']) && is_numeric($value))
|
---|
83 | $value = (int) $value * $type['div'];
|
---|
84 | /* tipagem com mais opções e flexivel */
|
---|
85 | /* volta p/ valor original */
|
---|
86 | if (isset ($type['arr']) && isset ($type['str']))
|
---|
87 | $value = $this->returnFormat($value, $type['arr'], $type['str']);
|
---|
88 | if (isset ($type['funcstr']) && method_exists($this->types, $type['funcstr'])) {
|
---|
89 | $mtd = $type['funcstr'];
|
---|
90 | $value = $this->types-> $mtd ($value);
|
---|
91 | }
|
---|
92 | /* Usa o PAD para completar o campo caso não seja do tamanho necessário */
|
---|
93 | $padN = STR_PAD_RIGHT;
|
---|
94 | $padF = ' ';
|
---|
95 | if (isset ($type['padl']) || isset ($type['padr'])) {
|
---|
96 | if (isset ($type['padl'])) {
|
---|
97 | $padN = STR_PAD_LEFT;
|
---|
98 | $padF = $type['padl'];
|
---|
99 | } else {
|
---|
100 | $padN = STR_PAD_RIGHT;
|
---|
101 | $padF = $type['padr'];
|
---|
102 | }
|
---|
103 | }
|
---|
104 | $value = str_pad($value, $length, $padF, $padN);
|
---|
105 | if ($this->debug) {
|
---|
106 | $this->dbgln[] = "Campo:{$fieldName};Tipo:{$fieldDef['type']};Tamanho:{$length};Valor:{$value};";
|
---|
107 | }
|
---|
108 | $dataS .= $value;
|
---|
109 | } else {
|
---|
110 | $iz = 0;
|
---|
111 | // Loop para pegar os dados do nivel abaixo
|
---|
112 | /* No caso da geracao de string percorrer todos os campos eh necessario para
|
---|
113 | * se manter a ordem correta
|
---|
114 | */
|
---|
115 | while ($iz < $fieldDef['length']) {
|
---|
116 | $value = '';
|
---|
117 | if (isset ($dataArr[$fieldName])) {
|
---|
118 | if ($type['type'] == 'assoc')
|
---|
119 | $value = $dataArr[$fieldName][$iz];
|
---|
120 | else
|
---|
121 | $value = $dataArr[$fieldName][$iz];
|
---|
122 | }
|
---|
123 | $dataS .= $this->mountString($fieldDef['fields'], $value);
|
---|
124 | ++$iz;
|
---|
125 | }
|
---|
126 | }
|
---|
127 | }
|
---|
128 | return $dataS;
|
---|
129 | }
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * Monta Array de acordo com o formato
|
---|
133 | * @param Array Formato
|
---|
134 | * @param String String Posicional
|
---|
135 | * @return Array Dados
|
---|
136 | */
|
---|
137 | public function mountResult($format, $string, & $pt = 0) {
|
---|
138 | if ($pt == 0 && $this->clearStep)
|
---|
139 | $this->dbgln = array ();
|
---|
140 | $data = array ();
|
---|
141 | $iz = 0;
|
---|
142 | foreach ($format as $fieldName => $fieldDef) {
|
---|
143 | $type = $this->types->getType($fieldDef['type']);
|
---|
144 | if (!isset ($type['loop'])) {
|
---|
145 | $length = 0;
|
---|
146 | // procura o length
|
---|
147 | if (isset ($type['length']))
|
---|
148 | $length = $type['length'];
|
---|
149 | if (isset ($fieldDef['length']))
|
---|
150 | $length = $fieldDef['length'];
|
---|
151 | $value = substr($string, $pt, $length);
|
---|
152 | if (isset ($type['div']))
|
---|
153 | $value = (float) $value / $type['div'];
|
---|
154 | if (isset ($type['arr']) && isset ($type['str']))
|
---|
155 | $value = $this->returnFormat($value, $type['str'], $type['arr']);
|
---|
156 | if (isset ($type['funcarr']) && method_exists($this->types, $type['funcarr'])) {
|
---|
157 | $mtd = $type['funcarr'];
|
---|
158 | $value = $this->types-> $mtd ($value);
|
---|
159 | }
|
---|
160 | $pt += $length;
|
---|
161 | $data[$fieldName] = ($this->trim) ? (trim($value)) : ($value);
|
---|
162 | if ($this->debug) {
|
---|
163 | $this->dbgln[] = "Campo:{$fieldName};Tipo:{$fieldDef['type']};Tamanho:{$length};Valor:{$data[$fieldName]};";
|
---|
164 | }
|
---|
165 | } else {
|
---|
166 | $iz = 0;
|
---|
167 | $data[$fieldName] = array();
|
---|
168 | // caso haja um campo com o limitador de ocorrencias define ele como o length
|
---|
169 | if (isset ($fieldDef['index']) && $fieldDef['index'] != null)
|
---|
170 | $fieldDef['length'] = $data[$fieldDef['index']];
|
---|
171 | // Loop para pegar os dados do nivel abaixo
|
---|
172 | //echo 'ddsadsa'.$type['type'];
|
---|
173 | while ($iz < $fieldDef['length']) {
|
---|
174 | if ($type['type'] == 'assoc'){
|
---|
175 | $tmp = $this->mountResult($fieldDef['fields'], $string, $pt);
|
---|
176 | $data[$fieldName] = array_merge_recursive($data[$fieldName],$tmp);
|
---|
177 | } else {
|
---|
178 | $data[$fieldName][$iz] = $this->mountResult($fieldDef['fields'], $string, $pt);
|
---|
179 | }
|
---|
180 | ++$iz;
|
---|
181 | }
|
---|
182 | }
|
---|
183 | }
|
---|
184 | return $data;
|
---|
185 | }
|
---|
186 | /**
|
---|
187 | * Conta o Tamanho total dos campos
|
---|
188 | * @param Array Formato
|
---|
189 | * @return Integer Tamanho total dos campos
|
---|
190 | */
|
---|
191 | public function countFormat($format) {
|
---|
192 | $pt = 0;
|
---|
193 | foreach ($format as $fieldName => $fieldDef) {
|
---|
194 | $type = $this->types->getType($fieldDef['type']);
|
---|
195 | if (!isset ($type['loop'])) {
|
---|
196 | $pt += $fieldDef['length'];
|
---|
197 | } else {
|
---|
198 | $pt += $fieldDef['length'] * $this->countFormat($fieldDef['fields']);
|
---|
199 | }
|
---|
200 | }
|
---|
201 | return $pt;
|
---|
202 | }
|
---|
203 | /**
|
---|
204 | * Obtem o resultado da array de log.
|
---|
205 | * @param bool Limpar array de log após obte-la
|
---|
206 | */
|
---|
207 | function getDebugLog($clear = false) {
|
---|
208 | $log = $this->dbgln;
|
---|
209 | if ($clear)
|
---|
210 | $this->dbgln = array ();
|
---|
211 | return $log;
|
---|
212 | }
|
---|
213 | /**
|
---|
214 | * Formata valor de acordo com o padrão sprintf
|
---|
215 | * @param String Entrada
|
---|
216 | * @param String Formato entrada
|
---|
217 | * @param String Formato saída
|
---|
218 | */
|
---|
219 | private function returnFormat($value, $typeIn, $typeOut) {
|
---|
220 | $tmp = sscanf($value, $typeIn);
|
---|
221 | if (!is_array($tmp)) {
|
---|
222 | return $value;
|
---|
223 | }
|
---|
224 | $ret = $this->sprintf_array($typeOut, $tmp);
|
---|
225 | return $ret;
|
---|
226 | }
|
---|
227 | }
|
---|
228 | ?>
|
---|