1 | <?php |
||
28 | class XMLConverter implements ConverterInterface |
||
29 | { |
||
30 | use ConverterTrait; |
||
31 | |||
32 | /** |
||
33 | * XML Root name |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $root_name = 'csv'; |
||
38 | |||
39 | /** |
||
40 | * XML Node name |
||
41 | * |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $record_name = 'row'; |
||
45 | |||
46 | /** |
||
47 | * XML Item name |
||
48 | * |
||
49 | * @var string |
||
50 | */ |
||
51 | protected $field_name = 'cell'; |
||
52 | |||
53 | /** |
||
54 | * XML column attribute name |
||
55 | * |
||
56 | * @var string |
||
57 | */ |
||
58 | protected $column_attr = ''; |
||
59 | |||
60 | /** |
||
61 | * XML offset attribute name |
||
62 | * |
||
63 | * @var string |
||
64 | */ |
||
65 | protected $offset_attr = ''; |
||
66 | |||
67 | /** |
||
68 | * Conversion method list |
||
69 | * |
||
70 | * @var array |
||
71 | */ |
||
72 | protected $encoder = [ |
||
73 | 'field' => [ |
||
74 | true => 'fieldToElementWithAttribute', |
||
75 | false => 'fieldToElement', |
||
76 | ], |
||
77 | 'record' => [ |
||
78 | true => 'recordToElementWithAttribute', |
||
79 | false => 'recordToElement', |
||
80 | ], |
||
81 | ]; |
||
82 | |||
83 | 4 | public function rootElement(string $node_name): self |
|
90 | |||
91 | 4 | public function recordElement(string $node_name, string $offset_attribute = ''): self |
|
101 | |||
102 | 2 | public function fieldElement(string $node_name, string $fieldname_attribute = '') |
|
112 | |||
113 | /** |
||
114 | * Convert an Record collection into a DOMDocument |
||
115 | * |
||
116 | * @param array|Traversable $records the CSV records collection |
||
117 | * |
||
118 | * @return DOMDocument |
||
119 | */ |
||
120 | 4 | public function convert($records) |
|
135 | |||
136 | /** |
||
137 | * Convert a CSV record into a DOMElement and |
||
138 | * adds its offset as DOMElement attribute |
||
139 | * |
||
140 | * @param DOMDocument $doc |
||
141 | * @param array $record CSV record |
||
142 | * @param string $field_encoder CSV Cell encoder method name |
||
143 | * @param int $offset CSV record offset |
||
144 | * |
||
145 | * @return DOMElement |
||
146 | */ |
||
147 | 4 | protected function recordToElementWithAttribute(DOMDocument $doc, array $record, string $field_encoder, int $offset): DOMElement |
|
154 | |||
155 | /** |
||
156 | * Convert a CSV record into a DOMElement |
||
157 | * |
||
158 | * @param DOMDocument $doc |
||
159 | * @param array $record CSV record |
||
160 | * @param string $field_encoder CSV Cell encoder method name |
||
161 | * |
||
162 | * @return DOMElement |
||
163 | */ |
||
164 | 4 | protected function recordToElement(DOMDocument $doc, array $record, string $field_encoder): DOMElement |
|
174 | |||
175 | /** |
||
176 | * Convert Cell to Item. |
||
177 | * |
||
178 | * Convert the CSV item into a DOMElement and adds the item offset |
||
179 | * as attribute to the returned DOMElement |
||
180 | * |
||
181 | * @param DOMDocument $doc |
||
182 | * @param string $value Record item value |
||
183 | * @param int|string $node_name Record item offset |
||
184 | * |
||
185 | * @return DOMElement |
||
186 | */ |
||
187 | 2 | protected function fieldToElementWithAttribute(DOMDocument $doc, string $value, $node_name): DOMElement |
|
194 | |||
195 | /** |
||
196 | * Convert Cell to Item |
||
197 | * |
||
198 | * @param DOMDocument $doc |
||
199 | * @param string $value Record item value |
||
200 | * |
||
201 | * @return DOMElement |
||
202 | */ |
||
203 | 4 | protected function fieldToElement(DOMDocument $doc, string $value): DOMElement |
|
210 | } |
||
211 |