1 | <?php |
||
30 | class XMLConverter |
||
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 $field_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 | * Conversion method list |
||
71 | * |
||
72 | * @var array |
||
73 | */ |
||
74 | protected $encoder = [ |
||
75 | 'field' => [ |
||
76 | true => 'fieldToElementWithAttribute', |
||
77 | false => 'fieldToElement', |
||
78 | ], |
||
79 | 'record' => [ |
||
80 | true => 'recordToElementWithAttribute', |
||
81 | false => 'recordToElement', |
||
82 | ], |
||
83 | ]; |
||
84 | |||
85 | /** |
||
86 | * Convert an Record collection into a DOMDocument |
||
87 | * |
||
88 | * @param array|Traversable $records the CSV records collection |
||
89 | * |
||
90 | * @return DOMDocument |
||
91 | */ |
||
92 | 4 | public function convert($records): DOMDocument |
|
106 | |||
107 | /** |
||
108 | * Convert a CSV record into a DOMElement and |
||
109 | * adds its offset as DOMElement attribute |
||
110 | * |
||
111 | * @param DOMDocument $doc |
||
112 | * @param array $record CSV record |
||
113 | * @param string $field_encoder CSV Cell encoder method name |
||
114 | * @param int $offset CSV record offset |
||
115 | * |
||
116 | * @return DOMElement |
||
117 | */ |
||
118 | 4 | protected function recordToElementWithAttribute( |
|
129 | |||
130 | /** |
||
131 | * Convert a CSV record into a DOMElement |
||
132 | * |
||
133 | * @param DOMDocument $doc |
||
134 | * @param array $record CSV record |
||
135 | * @param string $field_encoder CSV Cell encoder method name |
||
136 | * |
||
137 | * @return DOMElement |
||
138 | */ |
||
139 | 4 | protected function recordToElement(DOMDocument $doc, array $record, string $field_encoder): DOMElement |
|
149 | |||
150 | /** |
||
151 | * Convert Cell to Item. |
||
152 | * |
||
153 | * Convert the CSV item into a DOMElement and adds the item offset |
||
154 | * as attribute to the returned DOMElement |
||
155 | * |
||
156 | * @param DOMDocument $doc |
||
157 | * @param string $value Record item value |
||
158 | * @param int|string $node_name Record item offset |
||
159 | * |
||
160 | * @return DOMElement |
||
161 | */ |
||
162 | 4 | protected function fieldToElementWithAttribute(DOMDocument $doc, string $value, $node_name): DOMElement |
|
169 | |||
170 | /** |
||
171 | * Convert Cell to Item |
||
172 | * |
||
173 | * @param DOMDocument $doc |
||
174 | * @param string $value Record item value |
||
175 | * |
||
176 | * @return DOMElement |
||
177 | */ |
||
178 | 4 | protected function fieldToElement(DOMDocument $doc, string $value): DOMElement |
|
185 | |||
186 | /** |
||
187 | * XML root element setter |
||
188 | * |
||
189 | * @param string $node_name |
||
190 | * |
||
191 | * @return self |
||
192 | */ |
||
193 | 8 | public function rootElement(string $node_name): self |
|
200 | |||
201 | /** |
||
202 | * Filter XML element name |
||
203 | * |
||
204 | * @param string $value Element name |
||
205 | * |
||
206 | * @throws DOMException If the Element name is invalid |
||
207 | * |
||
208 | * @return string |
||
209 | */ |
||
210 | 8 | protected function filterElementName(string $value): string |
|
214 | |||
215 | /** |
||
216 | * XML Record element setter |
||
217 | * |
||
218 | * @param string $node_name |
||
219 | * @param string $record_offset_attribute_name |
||
220 | * |
||
221 | * @return self |
||
222 | */ |
||
223 | 6 | public function recordElement(string $node_name, string $record_offset_attribute_name = ''): self |
|
231 | |||
232 | /** |
||
233 | * Filter XML attribute name |
||
234 | * |
||
235 | * @param string $value Element name |
||
236 | * |
||
237 | * @throws DOMException If the Element attribute name is invalid |
||
238 | * |
||
239 | * @return string |
||
240 | */ |
||
241 | 6 | protected function filterAttributeName(string $value): string |
|
249 | |||
250 | /** |
||
251 | * XML Field element setter |
||
252 | * |
||
253 | * @param string $node_name |
||
254 | * @param string $fieldname_attribute_name |
||
255 | * |
||
256 | * @return self |
||
257 | */ |
||
258 | 6 | public function fieldElement(string $node_name, string $fieldname_attribute_name = ''): self |
|
266 | } |
||
267 |