|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the League.csv library |
|
4
|
|
|
* |
|
5
|
|
|
* @license http://opensource.org/licenses/MIT |
|
6
|
|
|
* @link https://github.com/thephpleague/csv/ |
|
7
|
|
|
* @version 9.0.0 |
|
8
|
|
|
* @package League.csv |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
declare(strict_types=1); |
|
14
|
|
|
|
|
15
|
|
|
namespace League\Csv; |
|
16
|
|
|
|
|
17
|
|
|
use DOMAttr; |
|
18
|
|
|
use DOMDocument; |
|
19
|
|
|
use DOMElement; |
|
20
|
|
|
use DOMException; |
|
21
|
|
|
use Traversable; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* A class to convert CSV records into a DOMDOcument object |
|
25
|
|
|
* |
|
26
|
|
|
* @package League.csv |
|
27
|
|
|
* @since 9.0.0 |
|
28
|
|
|
* @author Ignace Nyamagana Butera <[email protected]> |
|
29
|
|
|
*/ |
|
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 |
|
93
|
|
|
{ |
|
94
|
4 |
|
$field_encoder = $this->encoder['field']['' !== $this->column_attr]; |
|
95
|
4 |
|
$record_encoder = $this->encoder['record']['' !== $this->offset_attr]; |
|
96
|
4 |
|
$doc = new DOMDocument('1.0'); |
|
97
|
4 |
|
$root = $doc->createElement($this->root_name); |
|
98
|
4 |
|
foreach ($this->filterIterable($records) as $offset => $record) { |
|
99
|
4 |
|
$node = $this->{$record_encoder}($doc, $record, $field_encoder, $offset); |
|
100
|
4 |
|
$root->appendChild($node); |
|
101
|
|
|
} |
|
102
|
4 |
|
$doc->appendChild($root); |
|
103
|
|
|
|
|
104
|
4 |
|
return $doc; |
|
105
|
|
|
} |
|
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( |
|
119
|
|
|
DOMDocument $doc, |
|
120
|
|
|
array $record, |
|
121
|
|
|
string $field_encoder, |
|
122
|
|
|
int $offset |
|
123
|
|
|
): DOMElement { |
|
124
|
4 |
|
$node = $this->recordToElement($doc, $record, $field_encoder); |
|
125
|
4 |
|
$node->setAttribute($this->offset_attr, (string) $offset); |
|
126
|
|
|
|
|
127
|
4 |
|
return $node; |
|
128
|
|
|
} |
|
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 |
|
140
|
|
|
{ |
|
141
|
4 |
|
$node = $doc->createElement($this->record_name); |
|
142
|
4 |
|
foreach ($record as $node_name => $value) { |
|
143
|
4 |
|
$item = $this->{$field_encoder}($doc, $value, $node_name); |
|
144
|
4 |
|
$node->appendChild($item); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
4 |
|
return $node; |
|
148
|
|
|
} |
|
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 |
|
163
|
|
|
{ |
|
164
|
4 |
|
$item = $this->fieldToElement($doc, $value); |
|
165
|
4 |
|
$item->setAttribute($this->column_attr, (string) $node_name); |
|
166
|
|
|
|
|
167
|
4 |
|
return $item; |
|
168
|
|
|
} |
|
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 |
|
179
|
|
|
{ |
|
180
|
4 |
|
$item = $doc->createElement($this->field_name); |
|
181
|
4 |
|
$item->appendChild($doc->createTextNode($value)); |
|
182
|
|
|
|
|
183
|
4 |
|
return $item; |
|
184
|
|
|
} |
|
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 |
|
194
|
|
|
{ |
|
195
|
8 |
|
$clone = clone $this; |
|
196
|
8 |
|
$clone->root_name = $this->filterElementName($node_name); |
|
197
|
|
|
|
|
198
|
6 |
|
return $clone; |
|
199
|
|
|
} |
|
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 |
|
211
|
|
|
{ |
|
212
|
8 |
|
return (new DOMElement($value))->tagName; |
|
213
|
|
|
} |
|
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 |
|
224
|
|
|
{ |
|
225
|
6 |
|
$clone = clone $this; |
|
226
|
6 |
|
$clone->record_name = $this->filterElementName($node_name); |
|
227
|
6 |
|
$clone->offset_attr = $this->filterAttributeName($record_offset_attribute_name); |
|
228
|
|
|
|
|
229
|
6 |
|
return $clone; |
|
230
|
|
|
} |
|
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 |
|
242
|
|
|
{ |
|
243
|
6 |
|
if ('' === $value) { |
|
244
|
4 |
|
return $value; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
4 |
|
return (new DOMAttr($value))->name; |
|
248
|
|
|
} |
|
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 |
|
259
|
|
|
{ |
|
260
|
6 |
|
$clone = clone $this; |
|
261
|
6 |
|
$clone->field_name = $this->filterElementName($node_name); |
|
262
|
6 |
|
$clone->column_attr = $this->filterAttributeName($fieldname_attribute_name); |
|
263
|
|
|
|
|
264
|
6 |
|
return $clone; |
|
265
|
|
|
} |
|
266
|
|
|
} |
|
267
|
|
|
|