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