1 | <?php |
||
32 | class Converter |
||
33 | { |
||
34 | use ValidatorTrait; |
||
35 | |||
36 | /** |
||
37 | * XML Root name |
||
38 | * |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $root_name = 'csv'; |
||
42 | |||
43 | /** |
||
44 | * XML Node name |
||
45 | * |
||
46 | * @var string |
||
47 | */ |
||
48 | protected $record_name = 'row'; |
||
49 | |||
50 | /** |
||
51 | * XML Item name |
||
52 | * |
||
53 | * @var string |
||
54 | */ |
||
55 | protected $item_name = 'cell'; |
||
56 | |||
57 | /** |
||
58 | * XML column attribute name |
||
59 | * |
||
60 | * @var string |
||
61 | */ |
||
62 | protected $column_attr = 'name'; |
||
63 | |||
64 | /** |
||
65 | * XML offset attribute name |
||
66 | * |
||
67 | * @var string |
||
68 | */ |
||
69 | protected $offset_attr = 'offset'; |
||
70 | |||
71 | /** |
||
72 | * Tell whether to preserve offset |
||
73 | * |
||
74 | * @var bool |
||
75 | */ |
||
76 | protected $preserve_offset = false; |
||
77 | |||
78 | /** |
||
79 | * Charset Encoding for the CSV |
||
80 | * |
||
81 | * This information is used when converting the CSV to XML or JSON |
||
82 | * |
||
83 | * @var string |
||
84 | */ |
||
85 | protected $input_encoding = 'UTF-8'; |
||
86 | |||
87 | /** |
||
88 | * Conversion method list |
||
89 | * |
||
90 | * @var array |
||
91 | */ |
||
92 | protected $encoder = [ |
||
93 | 'item' => [ |
||
94 | true => 'itemToElementWithAttribute', |
||
95 | false => 'itemToElement', |
||
96 | ], |
||
97 | 'record' => [ |
||
98 | true => 'recordToElementWithAttribute', |
||
99 | false => 'recordToElement', |
||
100 | ], |
||
101 | ]; |
||
102 | |||
103 | /** |
||
104 | * XML Root name setter |
||
105 | * |
||
106 | * @param string $root_name |
||
107 | * |
||
108 | * @return self |
||
109 | */ |
||
110 | 6 | public function rootName(string $root_name): self |
|
121 | |||
122 | /** |
||
123 | * XML Record name setter |
||
124 | * |
||
125 | * @param string $record_name |
||
126 | * |
||
127 | * @return self |
||
128 | */ |
||
129 | 6 | public function recordName(string $record_name): self |
|
140 | |||
141 | /** |
||
142 | * XML Item name setter |
||
143 | * |
||
144 | * @param string $item_name |
||
145 | * |
||
146 | * @return self |
||
147 | */ |
||
148 | 6 | public function itemName(string $item_name): self |
|
159 | |||
160 | /** |
||
161 | * XML Column attribute name setter |
||
162 | * |
||
163 | * @param string $column_attr |
||
164 | * |
||
165 | * @return self |
||
166 | */ |
||
167 | 4 | public function columnAttributeName(string $column_attr): self |
|
178 | |||
179 | /** |
||
180 | * XML Offset attribute name setter |
||
181 | * |
||
182 | * @param string $offset_attr |
||
183 | * |
||
184 | * @return self |
||
185 | */ |
||
186 | 4 | public function offsetAttributeName(string $offset_attr): self |
|
197 | |||
198 | |||
199 | /** |
||
200 | * Whether we should preserve the CSV records keys. |
||
201 | * |
||
202 | * If set to true CSV document record keys will added to |
||
203 | * the conversion output . |
||
204 | * |
||
205 | * @param bool $status |
||
206 | * |
||
207 | * @return self |
||
208 | */ |
||
209 | 6 | public function preserveOffset(bool $status): self |
|
220 | |||
221 | /** |
||
222 | * Sets the CSV encoding charset |
||
223 | * |
||
224 | * @param string $str |
||
225 | * |
||
226 | * @throws InvalidArgumentException if the charset is empty |
||
227 | * |
||
228 | * @return static |
||
229 | */ |
||
230 | 12 | public function inputEncoding(string $str): self |
|
248 | |||
249 | /** |
||
250 | * Convert Csv file into UTF-8 |
||
251 | * |
||
252 | * @param array|Traversable $records the CSV records collection |
||
253 | * |
||
254 | * @return array|Iterator |
||
255 | */ |
||
256 | 6 | protected function convertToUtf8($records) |
|
274 | |||
275 | /** |
||
276 | * Convert an Record collection into a DOMDocument |
||
277 | * |
||
278 | * @param array|Traversable $records the CSV records collection |
||
279 | * |
||
280 | * @return DOMDocument |
||
281 | */ |
||
282 | 4 | public function toXML($records): DOMDocument |
|
297 | |||
298 | /** |
||
299 | * Convert an Record collection into a Json string |
||
300 | * |
||
301 | * @param array|Traversable $records the CSV records collection |
||
302 | * |
||
303 | * @return string |
||
304 | */ |
||
305 | 2 | public function toJson($records, int $options = 0, int $depth = 512): string |
|
314 | |||
315 | /** |
||
316 | * Convert an Record collection into a HTML table |
||
317 | * |
||
318 | * @param array|Traversable $records the CSV records collection |
||
319 | * |
||
320 | * @return DOMDocument |
||
321 | */ |
||
322 | 2 | public function toHTML($records, string $class_attr = 'table-csv-data'): string |
|
329 | |||
330 | /** |
||
331 | * Convert a CSV record into a DOMElement and |
||
332 | * adds its offset as DOMElement attribute |
||
333 | * |
||
334 | * @param DOMDocument $doc |
||
335 | * @param array $record CSV record |
||
336 | * @param string $item_encoder CSV Cell encoder method name |
||
337 | * @param int $offset CSV record offset |
||
338 | * |
||
339 | * @return DOMElement |
||
340 | */ |
||
341 | 2 | protected function recordToElementWithAttribute(DOMDocument $doc, array $record, string $item_encoder, int $offset): DOMElement |
|
348 | |||
349 | /** |
||
350 | * Convert a CSV record into a DOMElement |
||
351 | * |
||
352 | * @param DOMDocument $doc |
||
353 | * @param array $record CSV record |
||
354 | * @param string $item_encoder CSV Cell encoder method name |
||
355 | * |
||
356 | * @return DOMElement |
||
357 | */ |
||
358 | 4 | protected function recordToElement(DOMDocument $doc, array $record, string $item_encoder): DOMElement |
|
368 | |||
369 | /** |
||
370 | * Convert Cell to Item. |
||
371 | * |
||
372 | * Convert the CSV item into a DOMElement and adds the item offset |
||
373 | * as attribute to the returned DOMElement |
||
374 | * |
||
375 | * @param DOMDocument $doc |
||
376 | * @param string $value Record item value |
||
377 | * @param int|string $name Record item offset |
||
378 | * |
||
379 | * @return DOMElement |
||
380 | */ |
||
381 | 2 | protected function itemToElementWithAttribute(DOMDocument $doc, string $value, $name): DOMElement |
|
388 | |||
389 | /** |
||
390 | * Convert Cell to Item |
||
391 | * |
||
392 | * @param DOMDocument $doc |
||
393 | * @param string $value Record item value |
||
394 | * |
||
395 | * @return DOMElement |
||
396 | */ |
||
397 | 4 | protected function itemToElement(DOMDocument $doc, string $value): DOMElement |
|
404 | } |
||
405 |