|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\ArrayToXml; |
|
4
|
|
|
|
|
5
|
|
|
use DOMElement; |
|
6
|
|
|
use DOMDocument; |
|
7
|
|
|
use DOMException; |
|
8
|
|
|
use Exception; |
|
9
|
|
|
|
|
10
|
|
|
class ArrayToXml |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* The root DOM Document. |
|
14
|
|
|
* |
|
15
|
|
|
* @var DOMDocument |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $document; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Set to enable replacing space with underscore. |
|
21
|
|
|
* |
|
22
|
|
|
* @var bool |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $replaceSpacesByUnderScoresInKeyNames = true; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Prefix for the tags with numeric names. |
|
28
|
|
|
* |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $numericTagNamePrefix = 'numeric_'; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Construct a new instance. |
|
35
|
|
|
* |
|
36
|
|
|
* @param string[] $array |
|
37
|
|
|
* @param string|array $rootElement |
|
38
|
|
|
* @param bool $replaceSpacesByUnderScoresInKeyNames |
|
39
|
|
|
* @param string $xmlEncoding |
|
40
|
|
|
* @param string $xmlVersion |
|
41
|
|
|
* @param array $domProperties |
|
42
|
|
|
* |
|
43
|
|
|
* @throws DOMException |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct(array $array, $rootElement = '', $replaceSpacesByUnderScoresInKeyNames = true, $xmlEncoding = null, $xmlVersion = '1.0', $domProperties = []) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->document = new DOMDocument($xmlVersion, $xmlEncoding); |
|
48
|
|
|
|
|
49
|
|
|
if (! empty($domProperties)) { |
|
50
|
|
|
$this->setDomProperties($domProperties); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$this->replaceSpacesByUnderScoresInKeyNames = $replaceSpacesByUnderScoresInKeyNames; |
|
54
|
|
|
|
|
55
|
|
|
if ($this->isArrayAllKeySequential($array) && ! empty($array)) { |
|
56
|
|
|
throw new DOMException('Invalid Character Error'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$root = $this->createRootElement($rootElement); |
|
60
|
|
|
|
|
61
|
|
|
$this->document->appendChild($root); |
|
62
|
|
|
|
|
63
|
|
|
$this->convertElement($root, $array); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function setNumericTagNamePrefix(string $prefix) |
|
67
|
|
|
{ |
|
68
|
|
|
$this->numericTagNamePrefix = $prefix; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Convert the given array to an xml string. |
|
73
|
|
|
* |
|
74
|
|
|
* @param array $array |
|
75
|
|
|
* @param string $rootElement |
|
76
|
|
|
* @param bool $replaceSpacesByUnderScoresInKeyNames |
|
77
|
|
|
* @param null $xmlEncoding |
|
78
|
|
|
* @param string $xmlVersion |
|
79
|
|
|
* @param array $domProperties |
|
80
|
|
|
* @return string |
|
81
|
|
|
* |
|
82
|
|
|
* @throws DOMException |
|
83
|
|
|
*/ |
|
84
|
|
|
public static function convert(array $array, $rootElement = '', $replaceSpacesByUnderScoresInKeyNames = true, $xmlEncoding = null, $xmlVersion = '1.0', $domProperties = []) |
|
85
|
|
|
{ |
|
86
|
|
|
$converter = new static($array, $rootElement, $replaceSpacesByUnderScoresInKeyNames, $xmlEncoding, $xmlVersion, $domProperties); |
|
87
|
|
|
|
|
88
|
|
|
return $converter->toXml(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Return as XML. |
|
93
|
|
|
* |
|
94
|
|
|
* @return string |
|
95
|
|
|
*/ |
|
96
|
|
|
public function toXml() |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->document->saveXML(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Return as DOM object. |
|
103
|
|
|
* |
|
104
|
|
|
* @return DOMDocument |
|
105
|
|
|
*/ |
|
106
|
|
|
public function toDom() |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->document; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Ensure valid dom properties |
|
113
|
|
|
* |
|
114
|
|
|
* @param array $domProperties |
|
115
|
|
|
* @throws Exception |
|
116
|
|
|
*/ |
|
117
|
|
|
protected function ensureValidDomProperties($domProperties) { |
|
118
|
|
|
foreach ($domProperties as $key => $value) { |
|
119
|
|
|
if (! property_exists($this->document, $key)) { |
|
120
|
|
|
throw new Exception($key.' is not a valid property of DOMDocument'); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Sets dom properties on $this->document. |
|
127
|
|
|
* |
|
128
|
|
|
* @param array $domProperties |
|
129
|
|
|
* @throws Exception |
|
130
|
|
|
*/ |
|
131
|
|
|
public function setDomProperties($domProperties) |
|
132
|
|
|
{ |
|
133
|
|
|
$this->ensureValidDomProperties($domProperties); |
|
134
|
|
|
foreach ($domProperties as $key => $value) { |
|
135
|
|
|
$this->document->{$key} = $value; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Parse individual element. |
|
141
|
|
|
* |
|
142
|
|
|
* @param DOMElement $element |
|
143
|
|
|
* @param string|string[] $value |
|
144
|
|
|
*/ |
|
145
|
|
|
private function convertElement(DOMElement $element, $value) |
|
146
|
|
|
{ |
|
147
|
|
|
$sequential = $this->isArrayAllKeySequential($value); |
|
148
|
|
|
|
|
149
|
|
|
if (! is_array($value)) { |
|
150
|
|
|
$value = htmlspecialchars($value); |
|
151
|
|
|
|
|
152
|
|
|
$value = $this->removeControlCharacters($value); |
|
153
|
|
|
|
|
154
|
|
|
$element->nodeValue = $value; |
|
155
|
|
|
|
|
156
|
|
|
return; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
foreach ($value as $key => $data) { |
|
160
|
|
|
if (! $sequential) { |
|
161
|
|
|
if (($key === '_attributes') || ($key === '@attributes')) { |
|
162
|
|
|
$this->addAttributes($element, $data); |
|
163
|
|
|
} elseif ((($key === '_value') || ($key === '@value')) && is_string($data)) { |
|
164
|
|
|
$element->nodeValue = htmlspecialchars($data); |
|
165
|
|
|
} elseif ((($key === '_cdata') || ($key === '@cdata')) && is_string($data)) { |
|
166
|
|
|
$element->appendChild($this->document->createCDATASection($data)); |
|
167
|
|
|
} elseif ((($key === '_mixed') || ($key === '@mixed')) && is_string($data)) { |
|
168
|
|
|
$fragment = $this->document->createDocumentFragment(); |
|
169
|
|
|
$fragment->appendXML($data); |
|
170
|
|
|
$element->appendChild($fragment); |
|
171
|
|
|
} elseif ($key === '__numeric') { |
|
172
|
|
|
$this->addNumericNode($element, $data); |
|
173
|
|
|
} else { |
|
174
|
|
|
$this->addNode($element, $key, $data); |
|
175
|
|
|
} |
|
176
|
|
|
} elseif (is_array($data)) { |
|
177
|
|
|
$this->addCollectionNode($element, $data); |
|
178
|
|
|
} else { |
|
179
|
|
|
$this->addSequentialNode($element, $data); |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Add node with numeric keys. |
|
186
|
|
|
* |
|
187
|
|
|
* @param DOMElement $element |
|
188
|
|
|
* @param string|string[] $value |
|
189
|
|
|
*/ |
|
190
|
|
|
protected function addNumericNode(DOMElement $element, $value) |
|
191
|
|
|
{ |
|
192
|
|
|
foreach ($value as $key => $item) { |
|
|
|
|
|
|
193
|
|
|
$this->convertElement($element, [$this->numericTagNamePrefix.$key => $value]); |
|
|
|
|
|
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Add node. |
|
199
|
|
|
* |
|
200
|
|
|
* @param DOMElement $element |
|
201
|
|
|
* @param string $key |
|
202
|
|
|
* @param string|string[] $value |
|
203
|
|
|
*/ |
|
204
|
|
|
protected function addNode(DOMElement $element, $key, $value) |
|
205
|
|
|
{ |
|
206
|
|
|
if ($this->replaceSpacesByUnderScoresInKeyNames) { |
|
207
|
|
|
$key = str_replace(' ', '_', $key); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
$child = $this->document->createElement($key); |
|
211
|
|
|
$element->appendChild($child); |
|
212
|
|
|
$this->convertElement($child, $value); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* Add collection node. |
|
217
|
|
|
* |
|
218
|
|
|
* @param DOMElement $element |
|
219
|
|
|
* @param string|string[] $value |
|
220
|
|
|
* |
|
221
|
|
|
* @internal param string $key |
|
222
|
|
|
*/ |
|
223
|
|
|
protected function addCollectionNode(DOMElement $element, $value) |
|
224
|
|
|
{ |
|
225
|
|
|
if ($element->childNodes->length === 0 && $element->attributes->length === 0) { |
|
|
|
|
|
|
226
|
|
|
$this->convertElement($element, $value); |
|
227
|
|
|
|
|
228
|
|
|
return; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
$child = $this->document->createElement($element->tagName); |
|
232
|
|
|
$element->parentNode->appendChild($child); |
|
233
|
|
|
$this->convertElement($child, $value); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Add sequential node. |
|
238
|
|
|
* |
|
239
|
|
|
* @param DOMElement $element |
|
240
|
|
|
* @param string|string[] $value |
|
241
|
|
|
* |
|
242
|
|
|
* @internal param string $key |
|
243
|
|
|
*/ |
|
244
|
|
|
protected function addSequentialNode(DOMElement $element, $value) |
|
245
|
|
|
{ |
|
246
|
|
|
if (empty($element->nodeValue)) { |
|
247
|
|
|
$element->nodeValue = htmlspecialchars($value); |
|
248
|
|
|
|
|
249
|
|
|
return; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
$child = new DOMElement($element->tagName); |
|
253
|
|
|
$child->nodeValue = htmlspecialchars($value); |
|
254
|
|
|
$element->parentNode->appendChild($child); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* Check if array are all sequential. |
|
259
|
|
|
* |
|
260
|
|
|
* @param array|string $value |
|
261
|
|
|
* |
|
262
|
|
|
* @return bool |
|
263
|
|
|
*/ |
|
264
|
|
|
protected function isArrayAllKeySequential($value) |
|
265
|
|
|
{ |
|
266
|
|
|
if (! is_array($value)) { |
|
267
|
|
|
return false; |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
if (count($value) <= 0) { |
|
271
|
|
|
return true; |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
if (\key($value) === '__numeric') { |
|
275
|
|
|
return false; |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
return array_unique(array_map('is_int', array_keys($value))) === [true]; |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
/** |
|
282
|
|
|
* Add attributes. |
|
283
|
|
|
* |
|
284
|
|
|
* @param DOMElement $element |
|
285
|
|
|
* @param string[] $data |
|
286
|
|
|
*/ |
|
287
|
|
|
protected function addAttributes($element, $data) |
|
288
|
|
|
{ |
|
289
|
|
|
foreach ($data as $attrKey => $attrVal) { |
|
290
|
|
|
$element->setAttribute($attrKey, $attrVal); |
|
291
|
|
|
} |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
|
|
/** |
|
295
|
|
|
* Create the root element. |
|
296
|
|
|
* |
|
297
|
|
|
* @param string|array $rootElement |
|
298
|
|
|
* @return DOMElement |
|
299
|
|
|
*/ |
|
300
|
|
|
protected function createRootElement($rootElement) |
|
301
|
|
|
{ |
|
302
|
|
|
if (is_string($rootElement)) { |
|
303
|
|
|
$rootElementName = $rootElement ?: 'root'; |
|
304
|
|
|
|
|
305
|
|
|
return $this->document->createElement($rootElementName); |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
$rootElementName = $rootElement['rootElementName'] ?? 'root'; |
|
309
|
|
|
|
|
310
|
|
|
$element = $this->document->createElement($rootElementName); |
|
311
|
|
|
|
|
312
|
|
|
foreach ($rootElement as $key => $value) { |
|
313
|
|
|
if ($key !== '_attributes' && $key !== '@attributes') { |
|
314
|
|
|
continue; |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
$this->addAttributes($element, $rootElement[$key]); |
|
318
|
|
|
} |
|
319
|
|
|
|
|
320
|
|
|
return $element; |
|
321
|
|
|
} |
|
322
|
|
|
|
|
323
|
|
|
/** |
|
324
|
|
|
* @param $valuet |
|
325
|
|
|
* @return string |
|
326
|
|
|
*/ |
|
327
|
|
|
protected function removeControlCharacters($value) |
|
328
|
|
|
{ |
|
329
|
|
|
return preg_replace('/[\x00-\x09\x0B\x0C\x0E-\x1F\x7F]/', '', $value); |
|
330
|
|
|
} |
|
331
|
|
|
} |
|
332
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.