1 | <?php |
||
24 | class XMLConverter |
||
25 | { |
||
26 | /** |
||
27 | * XML Root name. |
||
28 | * |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $root_name = 'csv'; |
||
32 | |||
33 | /** |
||
34 | * XML Node name. |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $record_name = 'row'; |
||
39 | |||
40 | /** |
||
41 | * XML Item name. |
||
42 | * |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $field_name = 'cell'; |
||
46 | |||
47 | /** |
||
48 | * XML column attribute name. |
||
49 | * |
||
50 | * @var string |
||
51 | */ |
||
52 | protected $column_attr = ''; |
||
53 | |||
54 | /** |
||
55 | * XML offset attribute name. |
||
56 | * |
||
57 | * @var string |
||
58 | */ |
||
59 | protected $offset_attr = ''; |
||
60 | |||
61 | /** |
||
62 | * Conversion method list. |
||
63 | * |
||
64 | * @var array |
||
65 | */ |
||
66 | protected $encoder = [ |
||
67 | 'field' => [ |
||
68 | true => 'fieldToElementWithAttribute', |
||
69 | false => 'fieldToElement', |
||
70 | ], |
||
71 | 'record' => [ |
||
72 | true => 'recordToElementWithAttribute', |
||
73 | false => 'recordToElement', |
||
74 | ], |
||
75 | ]; |
||
76 | |||
77 | /** |
||
78 | * Convert a Record collection into a DOMDocument. |
||
79 | */ |
||
80 | 3 | public function convert(iterable $records): DOMDocument |
|
88 | |||
89 | /** |
||
90 | * Create a new DOMElement related to the given DOMDocument. |
||
91 | * |
||
92 | * **DOES NOT** attach to the DOMDocument |
||
93 | */ |
||
94 | 3 | public function import(iterable $records, DOMDocument $doc): DOMElement |
|
106 | |||
107 | /** |
||
108 | * Convert a CSV record into a DOMElement and |
||
109 | * adds its offset as DOMElement attribute. |
||
110 | */ |
||
111 | 6 | protected function recordToElementWithAttribute( |
|
122 | |||
123 | /** |
||
124 | * Convert a CSV record into a DOMElement. |
||
125 | */ |
||
126 | 6 | protected function recordToElement(DOMDocument $doc, array $record, string $field_encoder): DOMElement |
|
136 | |||
137 | /** |
||
138 | * Convert Cell to Item. |
||
139 | * |
||
140 | * Convert the CSV item into a DOMElement and adds the item offset |
||
141 | * as attribute to the returned DOMElement |
||
142 | * |
||
143 | * @param int|string $node_name |
||
144 | */ |
||
145 | 6 | protected function fieldToElementWithAttribute(DOMDocument $doc, string $value, $node_name): DOMElement |
|
152 | |||
153 | /** |
||
154 | * Convert Cell to Item. |
||
155 | * |
||
156 | * @param string $value Record item value |
||
157 | */ |
||
158 | 6 | protected function fieldToElement(DOMDocument $doc, string $value): DOMElement |
|
165 | |||
166 | /** |
||
167 | * XML root element setter. |
||
168 | */ |
||
169 | 9 | public function rootElement(string $node_name): self |
|
176 | |||
177 | /** |
||
178 | * Filter XML element name. |
||
179 | * |
||
180 | * @throws DOMException If the Element name is invalid |
||
181 | */ |
||
182 | 9 | protected function filterElementName(string $value): string |
|
186 | |||
187 | /** |
||
188 | * XML Record element setter. |
||
189 | */ |
||
190 | 6 | public function recordElement(string $node_name, string $record_offset_attribute_name = ''): self |
|
198 | |||
199 | /** |
||
200 | * Filter XML attribute name. |
||
201 | * |
||
202 | * @param string $value Element name |
||
203 | * |
||
204 | * @throws DOMException If the Element attribute name is invalid |
||
205 | */ |
||
206 | 9 | protected function filterAttributeName(string $value): string |
|
214 | |||
215 | /** |
||
216 | * XML Field element setter. |
||
217 | */ |
||
218 | 6 | public function fieldElement(string $node_name, string $fieldname_attribute_name = ''): self |
|
226 | } |
||
227 |