1 | <?php |
||
30 | class Converter |
||
31 | { |
||
32 | use ValidatorTrait; |
||
33 | |||
34 | /** |
||
35 | * XML Root name |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $root_name = 'csv'; |
||
40 | |||
41 | /** |
||
42 | * XML Node name |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | protected $record_name = 'row'; |
||
47 | |||
48 | /** |
||
49 | * XML Item name |
||
50 | * |
||
51 | * @var string |
||
52 | */ |
||
53 | protected $item_name = 'cell'; |
||
54 | |||
55 | /** |
||
56 | * XML column attribute name |
||
57 | * |
||
58 | * @var string |
||
59 | */ |
||
60 | protected $column_attr = ''; |
||
61 | |||
62 | /** |
||
63 | * XML offset attribute name |
||
64 | * |
||
65 | * @var string |
||
66 | */ |
||
67 | protected $offset_attr = ''; |
||
68 | |||
69 | /** |
||
70 | * Tell whether to preserve offset for json |
||
71 | * |
||
72 | * @var bool |
||
73 | */ |
||
74 | protected $json_preserve_offset = false; |
||
75 | |||
76 | /** |
||
77 | * json_encode options |
||
78 | * |
||
79 | * @var int |
||
80 | */ |
||
81 | protected $json_options = 0; |
||
82 | |||
83 | /** |
||
84 | * json_encode depth |
||
85 | * |
||
86 | * @var int |
||
87 | */ |
||
88 | protected $json_depth = 512; |
||
89 | |||
90 | /** |
||
91 | * Charset Encoding for the CSV |
||
92 | * |
||
93 | * This information is used when converting the CSV to XML or JSON |
||
94 | * |
||
95 | * @var string |
||
96 | */ |
||
97 | protected $input_encoding = 'UTF-8'; |
||
98 | |||
99 | /** |
||
100 | * Conversion method list |
||
101 | * |
||
102 | * @var array |
||
103 | */ |
||
104 | protected $encoder = [ |
||
105 | 'item' => [ |
||
106 | true => 'itemToElementWithAttribute', |
||
107 | false => 'itemToElement', |
||
108 | ], |
||
109 | 'record' => [ |
||
110 | true => 'recordToElementWithAttribute', |
||
111 | false => 'recordToElement', |
||
112 | ], |
||
113 | ]; |
||
114 | |||
115 | /** |
||
116 | * Sets the CSV encoding charset |
||
117 | * |
||
118 | * @param string $input_encoding |
||
119 | * |
||
120 | * @throws InvalidArgumentException if the charset is empty |
||
121 | * |
||
122 | * @return static |
||
123 | */ |
||
124 | 12 | public function inputEncoding(string $input_encoding): self |
|
132 | |||
133 | /** |
||
134 | * Filter XML element name |
||
135 | * |
||
136 | * @param string $value Element name |
||
137 | * @param string $type Element type |
||
138 | * @param string $method Method call |
||
139 | * |
||
140 | * @throws InvalidArgumentException If the Element name is empty |
||
141 | * |
||
142 | * @return string |
||
143 | */ |
||
144 | 14 | protected function filterElementName(string $value, string $type, string $method): string |
|
154 | |||
155 | /** |
||
156 | * XML Element names setter |
||
157 | * |
||
158 | * @param string $root_name XML root element name |
||
159 | * @param string $record_name XML record element name |
||
160 | * @param string $item_name XML record item element name |
||
161 | * |
||
162 | * @return self |
||
163 | */ |
||
164 | 8 | public function xmlElements( |
|
176 | |||
177 | /** |
||
178 | * XML Element attributes name setter |
||
179 | * |
||
180 | * @param string $offset_attr XML record element offset attribute name |
||
181 | * @param string $column_attr XML record item field attribute name |
||
182 | * |
||
183 | * @return self |
||
184 | */ |
||
185 | 4 | public function xmlAttributes(string $offset_attr = '', string $column_attr = ''): self |
|
194 | |||
195 | /** |
||
196 | * Json encode Options |
||
197 | * |
||
198 | * @param bool $preserve_offset |
||
199 | * @param int $options |
||
200 | * @param int $depth |
||
201 | * |
||
202 | * @return self |
||
203 | */ |
||
204 | 4 | public function jsonOptions(bool $preserve_offset, int $options = 0, int $depth = 512): self |
|
213 | |||
214 | /** |
||
215 | * Convert Csv file into UTF-8 |
||
216 | * |
||
217 | * @param array|Traversable $records the CSV records collection |
||
218 | * |
||
219 | * @return array|Iterator |
||
220 | */ |
||
221 | 6 | protected function convertToUtf8($records) |
|
239 | |||
240 | /** |
||
241 | * Convert an Record collection into a DOMDocument |
||
242 | * |
||
243 | * @param array|Traversable $records the CSV records collection |
||
244 | * |
||
245 | * @return DOMDocument |
||
246 | */ |
||
247 | 4 | public function toXML($records): DOMDocument |
|
262 | |||
263 | /** |
||
264 | * Convert an Record collection into a Json string |
||
265 | * |
||
266 | * @param array|Traversable $records the CSV records collection |
||
267 | * |
||
268 | * @return string |
||
269 | */ |
||
270 | 2 | public function toJson($records): string |
|
288 | |||
289 | /** |
||
290 | * Convert an Record collection into a HTML table |
||
291 | * |
||
292 | * @param array|Traversable $records the CSV records collection |
||
293 | * |
||
294 | * @return DOMDocument |
||
295 | */ |
||
296 | 2 | public function toHTML($records, string $class_attr = 'table-csv-data'): string |
|
303 | |||
304 | /** |
||
305 | * Convert a CSV record into a DOMElement and |
||
306 | * adds its offset as DOMElement attribute |
||
307 | * |
||
308 | * @param DOMDocument $doc |
||
309 | * @param array $record CSV record |
||
310 | * @param string $item_encoder CSV Cell encoder method name |
||
311 | * @param int $offset CSV record offset |
||
312 | * |
||
313 | * @return DOMElement |
||
314 | */ |
||
315 | 2 | protected function recordToElementWithAttribute(DOMDocument $doc, array $record, string $item_encoder, int $offset): DOMElement |
|
322 | |||
323 | /** |
||
324 | * Convert a CSV record into a DOMElement |
||
325 | * |
||
326 | * @param DOMDocument $doc |
||
327 | * @param array $record CSV record |
||
328 | * @param string $item_encoder CSV Cell encoder method name |
||
329 | * |
||
330 | * @return DOMElement |
||
331 | */ |
||
332 | 4 | protected function recordToElement(DOMDocument $doc, array $record, string $item_encoder): DOMElement |
|
342 | |||
343 | /** |
||
344 | * Convert Cell to Item. |
||
345 | * |
||
346 | * Convert the CSV item into a DOMElement and adds the item offset |
||
347 | * as attribute to the returned DOMElement |
||
348 | * |
||
349 | * @param DOMDocument $doc |
||
350 | * @param string $value Record item value |
||
351 | * @param int|string $name Record item offset |
||
352 | * |
||
353 | * @return DOMElement |
||
354 | */ |
||
355 | 2 | protected function itemToElementWithAttribute(DOMDocument $doc, string $value, $name): DOMElement |
|
362 | |||
363 | /** |
||
364 | * Convert Cell to Item |
||
365 | * |
||
366 | * @param DOMDocument $doc |
||
367 | * @param string $value Record item value |
||
368 | * |
||
369 | * @return DOMElement |
||
370 | */ |
||
371 | 4 | protected function itemToElement(DOMDocument $doc, string $value): DOMElement |
|
378 | } |
||
379 |