|
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 DOMDocument; |
|
18
|
|
|
use DOMElement; |
|
19
|
|
|
use Iterator; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* A class to convert CSV records into a DOMDOcument object |
|
23
|
|
|
* |
|
24
|
|
|
* @package League.csv |
|
25
|
|
|
* @since 9.0.0 |
|
26
|
|
|
* @author Ignace Nyamagana Butera <[email protected]> |
|
27
|
|
|
* @internal used internally to returns the DOMDocument object representation of a CSV document |
|
28
|
|
|
* |
|
29
|
|
|
*/ |
|
30
|
|
|
class XMLEncoder |
|
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 $item_name = 'cell'; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* XML column attribute name |
|
55
|
|
|
* |
|
56
|
|
|
* @var string |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $column_attr = 'name'; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* XML offset attribute name |
|
62
|
|
|
* |
|
63
|
|
|
* @var string |
|
64
|
|
|
*/ |
|
65
|
|
|
protected $offset_attr = 'offset'; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Tell whether to preserve item offset |
|
69
|
|
|
* |
|
70
|
|
|
* @var bool |
|
71
|
|
|
*/ |
|
72
|
|
|
protected $preserve_item_offset = false; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Tell whether to preserve record offset |
|
76
|
|
|
* |
|
77
|
|
|
* @var bool |
|
78
|
|
|
*/ |
|
79
|
|
|
protected $preserve_record_offset = false; |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Conversion method list |
|
83
|
|
|
* |
|
84
|
|
|
* @var array |
|
85
|
|
|
*/ |
|
86
|
|
|
protected $encoder = [ |
|
87
|
|
|
'item' => [ |
|
88
|
|
|
true => 'itemToElementWithAttribute', |
|
89
|
|
|
false => 'itemToElement', |
|
90
|
|
|
], |
|
91
|
|
|
'record' => [ |
|
92
|
|
|
true => 'recordToElementWithAttribute', |
|
93
|
|
|
false => 'recordToElement', |
|
94
|
|
|
], |
|
95
|
|
|
]; |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* XML Root name setter |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $root_name |
|
101
|
|
|
* |
|
102
|
|
|
* @return self |
|
103
|
|
|
*/ |
|
104
|
6 |
|
public function rootName(string $root_name): self |
|
105
|
|
|
{ |
|
106
|
6 |
|
if ($root_name === $this->root_name) { |
|
107
|
2 |
|
return $this; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
4 |
|
$clone = clone $this; |
|
111
|
4 |
|
$clone->root_name = $root_name; |
|
112
|
|
|
|
|
113
|
4 |
|
return $clone; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* XML Record name setter |
|
118
|
|
|
* |
|
119
|
|
|
* @param string $record_name |
|
120
|
|
|
* |
|
121
|
|
|
* @return self |
|
122
|
|
|
*/ |
|
123
|
6 |
|
public function recordName(string $record_name): self |
|
124
|
|
|
{ |
|
125
|
6 |
|
if ($record_name === $this->record_name) { |
|
126
|
2 |
|
return $this; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
4 |
|
$clone = clone $this; |
|
130
|
4 |
|
$clone->record_name = $record_name; |
|
131
|
|
|
|
|
132
|
4 |
|
return $clone; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* XML Item name setter |
|
137
|
|
|
* |
|
138
|
|
|
* @param string $item_name |
|
139
|
|
|
* |
|
140
|
|
|
* @return self |
|
141
|
|
|
*/ |
|
142
|
6 |
|
public function itemName(string $item_name): self |
|
143
|
|
|
{ |
|
144
|
6 |
|
if ($item_name === $this->item_name) { |
|
145
|
2 |
|
return $this; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
4 |
|
$clone = clone $this; |
|
149
|
4 |
|
$clone->item_name = $item_name; |
|
150
|
|
|
|
|
151
|
4 |
|
return $clone; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* XML Column attribute name setter |
|
156
|
|
|
* |
|
157
|
|
|
* @param string $column_attr |
|
158
|
|
|
* |
|
159
|
|
|
* @return self |
|
160
|
|
|
*/ |
|
161
|
6 |
|
public function columnAttributeName(string $column_attr): self |
|
162
|
|
|
{ |
|
163
|
6 |
|
if ($column_attr === $this->column_attr) { |
|
164
|
2 |
|
return $this; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
4 |
|
$clone = clone $this; |
|
168
|
4 |
|
$clone->column_attr = $column_attr; |
|
169
|
|
|
|
|
170
|
4 |
|
return $clone; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* XML Offset attribute name setter |
|
175
|
|
|
* |
|
176
|
|
|
* @param string $offset_attr |
|
177
|
|
|
* |
|
178
|
|
|
* @return self |
|
179
|
|
|
*/ |
|
180
|
6 |
|
public function offsetAttributeName(string $offset_attr): self |
|
181
|
|
|
{ |
|
182
|
6 |
|
if ($offset_attr === $this->offset_attr) { |
|
183
|
2 |
|
return $this; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
4 |
|
$clone = clone $this; |
|
187
|
4 |
|
$clone->offset_attr = $offset_attr; |
|
188
|
|
|
|
|
189
|
4 |
|
return $clone; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Whether we should preserve the CSV records keys. |
|
194
|
|
|
* |
|
195
|
|
|
* If set to true CSV document record keys will added to |
|
196
|
|
|
* the conversion output . |
|
197
|
|
|
* |
|
198
|
|
|
* @param bool $status |
|
199
|
|
|
* |
|
200
|
|
|
* @return self |
|
201
|
|
|
*/ |
|
202
|
6 |
|
public function preserveItemOffset(bool $status): self |
|
203
|
|
|
{ |
|
204
|
6 |
|
if ($status === $this->preserve_item_offset) { |
|
205
|
4 |
|
return $this; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
4 |
|
$clone = clone $this; |
|
209
|
4 |
|
$clone->preserve_item_offset = $status; |
|
210
|
|
|
|
|
211
|
4 |
|
return $clone; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* Whether we should preserve the CSV records keys. |
|
216
|
|
|
* |
|
217
|
|
|
* If set to true CSV document record keys will added to |
|
218
|
|
|
* the conversion output . |
|
219
|
|
|
* |
|
220
|
|
|
* @param bool $status |
|
221
|
|
|
* |
|
222
|
|
|
* @return self |
|
223
|
|
|
*/ |
|
224
|
6 |
|
public function preserveRecordOffset(bool $status): self |
|
225
|
|
|
{ |
|
226
|
6 |
|
if ($status === $this->preserve_record_offset) { |
|
227
|
6 |
|
return $this; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
2 |
|
$clone = clone $this; |
|
231
|
2 |
|
$clone->preserve_record_offset = $status; |
|
232
|
|
|
|
|
233
|
2 |
|
return $clone; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Convert an Record collection into a DOMDocument |
|
238
|
|
|
* |
|
239
|
|
|
* @param Iterator $iterator the CSV records iterator |
|
240
|
|
|
* |
|
241
|
|
|
* @return DOMDocument |
|
242
|
|
|
*/ |
|
243
|
6 |
|
public function encode(Iterator $iterator): DOMDocument |
|
244
|
|
|
{ |
|
245
|
6 |
|
$item_encoder = $this->encoder['item'][$this->preserve_item_offset]; |
|
246
|
6 |
|
$record_encoder = $this->encoder['record'][$this->preserve_record_offset]; |
|
247
|
|
|
|
|
248
|
6 |
|
$doc = new DOMDocument('1.0', 'UTF-8'); |
|
249
|
6 |
|
$root = $doc->createElement($this->root_name); |
|
250
|
6 |
|
foreach ($iterator as $offset => $record) { |
|
251
|
6 |
|
$node = $this->{$record_encoder}($doc, $record, $item_encoder, $offset); |
|
252
|
6 |
|
$root->appendChild($node); |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
6 |
|
$doc->appendChild($root); |
|
256
|
|
|
|
|
257
|
6 |
|
return $doc; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* Convert a CSV record into a DOMElement and |
|
262
|
|
|
* adds its offset as DOMElement attribute |
|
263
|
|
|
* |
|
264
|
|
|
* @param DOMDocument $doc |
|
265
|
|
|
* @param array $record CSV record |
|
266
|
|
|
* @param string $item_encoder CSV Cell encoder method name |
|
267
|
|
|
* @param int $offset CSV record offset |
|
268
|
|
|
* |
|
269
|
|
|
* @return DOMElement |
|
270
|
|
|
*/ |
|
271
|
2 |
|
protected function recordToElementWithAttribute(DOMDocument $doc, array $record, string $item_encoder, int $offset): DOMElement |
|
272
|
|
|
{ |
|
273
|
2 |
|
$node = $this->recordToElement($doc, $record, $item_encoder); |
|
274
|
2 |
|
$node->setAttribute($this->offset_attr, (string) $offset); |
|
275
|
|
|
|
|
276
|
2 |
|
return $node; |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
/** |
|
280
|
|
|
* Convert a CSV record into a DOMElement |
|
281
|
|
|
* |
|
282
|
|
|
* @param DOMDocument $doc |
|
283
|
|
|
* @param array $record CSV record |
|
284
|
|
|
* @param string $item_encoder CSV Cell encoder method name |
|
285
|
|
|
* |
|
286
|
|
|
* @return DOMElement |
|
287
|
|
|
*/ |
|
288
|
6 |
|
protected function recordToElement(DOMDocument $doc, array $record, string $item_encoder): DOMElement |
|
289
|
|
|
{ |
|
290
|
6 |
|
$node = $doc->createElement($this->record_name); |
|
291
|
6 |
|
foreach ($record as $name => $value) { |
|
292
|
6 |
|
$item = $this->{$item_encoder}($doc, $value, $name); |
|
293
|
6 |
|
$node->appendChild($item); |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
6 |
|
return $node; |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
/** |
|
300
|
|
|
* Convert Cell to Item. |
|
301
|
|
|
* |
|
302
|
|
|
* Convert the CSV item into a DOMElement and adds the item offset |
|
303
|
|
|
* as attribute to the returned DOMElement |
|
304
|
|
|
* |
|
305
|
|
|
* @param DOMDocument $doc |
|
306
|
|
|
* @param string $value Record item value |
|
307
|
|
|
* @param int|string $name Record item offset |
|
308
|
|
|
* |
|
309
|
|
|
* @return DOMElement |
|
310
|
|
|
*/ |
|
311
|
4 |
|
protected function itemToElementWithAttribute(DOMDocument $doc, string $value, $name): DOMElement |
|
312
|
|
|
{ |
|
313
|
4 |
|
$item = $this->itemToElement($doc, $value); |
|
314
|
4 |
|
$item->setAttribute($this->column_attr, (string) $name); |
|
315
|
|
|
|
|
316
|
4 |
|
return $item; |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
/** |
|
320
|
|
|
* Convert Cell to Item |
|
321
|
|
|
* |
|
322
|
|
|
* @param DOMDocument $doc |
|
323
|
|
|
* @param string $value Record item value |
|
324
|
|
|
* |
|
325
|
|
|
* @return DOMElement |
|
326
|
|
|
*/ |
|
327
|
6 |
|
protected function itemToElement(DOMDocument $doc, string $value): DOMElement |
|
328
|
|
|
{ |
|
329
|
6 |
|
$item = $doc->createElement($this->item_name); |
|
330
|
6 |
|
$item->appendChild($doc->createTextNode($value)); |
|
331
|
|
|
|
|
332
|
6 |
|
return $item; |
|
333
|
|
|
} |
|
334
|
|
|
} |
|
335
|
|
|
|