|
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
|
|
|
* Construct a new instance. |
|
27
|
|
|
* |
|
28
|
|
|
* @param string[] $array |
|
29
|
|
|
* @param string $rootElementName |
|
30
|
|
|
* @param bool $replaceSpacesByUnderScoresInKeyNames |
|
31
|
|
|
* |
|
32
|
|
|
* @throws DOMException |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct(array $array, $rootElementName = '', $replaceSpacesByUnderScoresInKeyNames = true) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->document = new DOMDocument(); |
|
37
|
|
|
$this->replaceSpacesByUnderScoresInKeyNames = $replaceSpacesByUnderScoresInKeyNames; |
|
38
|
|
|
|
|
39
|
|
|
if ($this->isArrayAllKeySequential($array) && !empty($array)) { |
|
40
|
|
|
throw new DOMException('Invalid Character Error'); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$root = $this->document->createElement($rootElementName == '' ? 'root' : $rootElementName); |
|
44
|
|
|
|
|
45
|
|
|
$this->document->appendChild($root); |
|
46
|
|
|
|
|
47
|
|
|
$this->convertElement($root, $array); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Convert the given array to an xml string. |
|
52
|
|
|
* |
|
53
|
|
|
* @param string[] $array |
|
54
|
|
|
* @param string $rootElementName |
|
55
|
|
|
* @param bool $replaceSpacesByUnderScoresInKeyNames |
|
56
|
|
|
* |
|
57
|
|
|
* @return type |
|
58
|
|
|
*/ |
|
59
|
|
|
public static function convert(array $array, $rootElementName = '', $replaceSpacesByUnderScoresInKeyNames = true) |
|
60
|
|
|
{ |
|
61
|
|
|
$converter = new static($array, $rootElementName, $replaceSpacesByUnderScoresInKeyNames); |
|
62
|
|
|
|
|
63
|
|
|
return $converter->toXml(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Return as XML. |
|
68
|
|
|
* |
|
69
|
|
|
* @return string |
|
70
|
|
|
*/ |
|
71
|
|
|
public function toXml() |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->document->saveXML(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Parse individual element. |
|
78
|
|
|
* |
|
79
|
|
|
* @param \DOMElement $element |
|
80
|
|
|
* @param string|string[] $value |
|
81
|
|
|
*/ |
|
82
|
|
|
private function convertElement(DOMElement $element, $value) |
|
83
|
|
|
{ |
|
84
|
|
|
$sequential = $this->isArrayAllKeySequential($value); |
|
85
|
|
|
|
|
86
|
|
|
if (!is_array($value)) { |
|
87
|
|
|
$element->nodeValue = htmlspecialchars($value); |
|
88
|
|
|
|
|
89
|
|
|
return; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
foreach ($value as $key => $data) { |
|
93
|
|
|
if (!$sequential) { |
|
94
|
|
|
if ($key === '_attributes') { |
|
95
|
|
|
$this->addAttributes($element, $data); |
|
96
|
|
|
} elseif ($key === '_value' && is_string($data)) { |
|
97
|
|
|
$element->nodeValue = $data; |
|
98
|
|
|
} else { |
|
99
|
|
|
$this->addNode($element, $key, $data); |
|
100
|
|
|
} |
|
101
|
|
|
} elseif (is_array($data)) { |
|
102
|
|
|
$this->addCollectionNode($element, $data); |
|
103
|
|
|
} else { |
|
104
|
|
|
$this->addSequentualNode($element, $data); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Add node. |
|
111
|
|
|
* |
|
112
|
|
|
* @param \DOMElement $element |
|
113
|
|
|
* @param string $key |
|
114
|
|
|
* @param string|string[] $value |
|
115
|
|
|
*/ |
|
116
|
|
|
protected function addNode(DOMElement $element, $key, $value) |
|
117
|
|
|
{ |
|
118
|
|
|
if ($this->replaceSpacesByUnderScoresInKeyNames) { |
|
119
|
|
|
$key = str_replace(' ', '_', $key); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
$child = $this->document->createElement($key); |
|
123
|
|
|
$element->appendChild($child); |
|
124
|
|
|
$this->convertElement($child, $value); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Add collection node. |
|
129
|
|
|
* |
|
130
|
|
|
* @param \DOMElement $element |
|
131
|
|
|
* @param string|string[] $value |
|
132
|
|
|
* |
|
133
|
|
|
* @internal param string $key |
|
134
|
|
|
*/ |
|
135
|
|
|
protected function addCollectionNode(DOMElement $element, $value) |
|
136
|
|
|
{ |
|
137
|
|
|
if ($element->childNodes->length == 0) { |
|
138
|
|
|
$this->convertElement($element, $value); |
|
139
|
|
|
|
|
140
|
|
|
return; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
$child = $element->cloneNode(); |
|
144
|
|
|
$element->parentNode->appendChild($child); |
|
145
|
|
|
$this->convertElement($child, $value); |
|
|
|
|
|
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Add sequential node. |
|
150
|
|
|
* |
|
151
|
|
|
* @param \DOMElement $element |
|
152
|
|
|
* @param string|string[] $value |
|
153
|
|
|
* |
|
154
|
|
|
* @internal param string $key |
|
155
|
|
|
*/ |
|
156
|
|
|
protected function addSequentualNode(DOMElement $element, $value) |
|
157
|
|
|
{ |
|
158
|
|
|
if (empty($element->nodeValue)) { |
|
159
|
|
|
$element->nodeValue = htmlspecialchars($value); |
|
160
|
|
|
|
|
161
|
|
|
return; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
$child = $element->cloneNode(); |
|
165
|
|
|
$child->nodeValue = htmlspecialchars($value); |
|
166
|
|
|
$element->parentNode->appendChild($child); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Check if array are all sequential. |
|
171
|
|
|
* |
|
172
|
|
|
* @param array|string $value |
|
173
|
|
|
* |
|
174
|
|
|
* @return bool |
|
175
|
|
|
*/ |
|
176
|
|
|
protected function isArrayAllKeySequential($value) |
|
177
|
|
|
{ |
|
178
|
|
|
if (!is_array($value)) { |
|
179
|
|
|
return false; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
if (count($value) <= 0) { |
|
183
|
|
|
return true; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
return array_unique(array_map('is_int', array_keys($value))) === array(true); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Add attributes. |
|
191
|
|
|
* |
|
192
|
|
|
* @param \DOMElement $element |
|
193
|
|
|
* @param string[] $data |
|
194
|
|
|
*/ |
|
195
|
|
|
protected function addAttributes($element, $data) |
|
196
|
|
|
{ |
|
197
|
|
|
foreach ($data as $attrKey => $attrVal) { |
|
198
|
|
|
$element->setAttribute($attrKey, $attrVal); |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.