1 | <?php |
||
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 string |
||
58 | */ |
||
59 | public static function convert(array $array, $rootElementName = '', $replaceSpacesByUnderScoresInKeyNames = true) |
||
65 | |||
66 | /** |
||
67 | * Return as XML. |
||
68 | * |
||
69 | * @return string |
||
70 | */ |
||
71 | public function toXml() |
||
75 | |||
76 | /** |
||
77 | * Return as DOM object. |
||
78 | * |
||
79 | * @return DOMDocument |
||
80 | */ |
||
81 | public function toDom() |
||
85 | |||
86 | /** |
||
87 | * Parse individual element. |
||
88 | * |
||
89 | * @param \DOMElement $element |
||
90 | * @param string|string[] $value |
||
91 | */ |
||
92 | private function convertElement(DOMElement $element, $value) |
||
118 | |||
119 | /** |
||
120 | * Add node. |
||
121 | * |
||
122 | * @param \DOMElement $element |
||
123 | * @param string $key |
||
124 | * @param string|string[] $value |
||
125 | */ |
||
126 | protected function addNode(DOMElement $element, $key, $value) |
||
136 | |||
137 | /** |
||
138 | * Add collection node. |
||
139 | * |
||
140 | * @param \DOMElement $element |
||
141 | * @param string|string[] $value |
||
142 | * |
||
143 | * @internal param string $key |
||
144 | */ |
||
145 | protected function addCollectionNode(DOMElement $element, $value) |
||
157 | |||
158 | /** |
||
159 | * Add sequential node. |
||
160 | * |
||
161 | * @param \DOMElement $element |
||
162 | * @param string|string[] $value |
||
163 | * |
||
164 | * @internal param string $key |
||
165 | */ |
||
166 | protected function addSequentialNode(DOMElement $element, $value) |
||
178 | |||
179 | /** |
||
180 | * Check if array are all sequential. |
||
181 | * |
||
182 | * @param array|string $value |
||
183 | * |
||
184 | * @return bool |
||
185 | */ |
||
186 | protected function isArrayAllKeySequential($value) |
||
198 | |||
199 | /** |
||
200 | * Add attributes. |
||
201 | * |
||
202 | * @param \DOMElement $element |
||
203 | * @param string[] $data |
||
204 | */ |
||
205 | protected function addAttributes($element, $data) |
||
211 | } |
||
212 |
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.