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|array $rootElement |
||
30 | * @param bool $replaceSpacesByUnderScoresInKeyNames |
||
31 | * @param string $xmlEncoding |
||
32 | * @param string $xmlVersion |
||
33 | * |
||
34 | * @throws DOMException |
||
35 | */ |
||
36 | public function __construct(array $array, $rootElement = '', $replaceSpacesByUnderScoresInKeyNames = true, $xmlEncoding = null, $xmlVersion = '1.0') |
||
37 | { |
||
38 | $this->document = new DOMDocument($xmlVersion, $xmlEncoding); |
||
39 | $this->replaceSpacesByUnderScoresInKeyNames = $replaceSpacesByUnderScoresInKeyNames; |
||
40 | |||
41 | if ($this->isArrayAllKeySequential($array) && ! empty($array)) { |
||
42 | throw new DOMException('Invalid Character Error'); |
||
43 | } |
||
44 | |||
45 | $root = $this->createRootElement($rootElement); |
||
46 | |||
47 | $this->document->appendChild($root); |
||
48 | |||
49 | $this->convertElement($root, $array); |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * Convert the given array to an xml string. |
||
54 | * |
||
55 | * @param string[] $array |
||
56 | * @param string $rootElementName |
||
57 | * @param bool $replaceSpacesByUnderScoresInKeyNames |
||
58 | * @param string $xmlEncoding |
||
59 | * @param string $xmlVersion |
||
60 | * |
||
61 | * @return string |
||
62 | */ |
||
63 | public static function convert(array $array, $rootElementName = '', $replaceSpacesByUnderScoresInKeyNames = true, $xmlEncoding = null, $xmlVersion = '1.0') |
||
64 | { |
||
65 | $converter = new static($array, $rootElementName, $replaceSpacesByUnderScoresInKeyNames, $xmlEncoding, $xmlVersion); |
||
66 | |||
67 | return $converter->toXml(); |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Return as XML. |
||
72 | * |
||
73 | * @return string |
||
74 | */ |
||
75 | public function toXml() |
||
79 | |||
80 | /** |
||
81 | * Return as DOM object. |
||
82 | * |
||
83 | * @return DOMDocument |
||
84 | */ |
||
85 | public function toDom() |
||
89 | |||
90 | /** |
||
91 | * Parse individual element. |
||
92 | * |
||
93 | * @param \DOMElement $element |
||
94 | * @param string|string[] $value |
||
95 | */ |
||
96 | private function convertElement(DOMElement $element, $value) |
||
122 | |||
123 | /** |
||
124 | * Add node. |
||
125 | * |
||
126 | * @param \DOMElement $element |
||
127 | * @param string $key |
||
128 | * @param string|string[] $value |
||
129 | */ |
||
130 | protected function addNode(DOMElement $element, $key, $value) |
||
140 | |||
141 | /** |
||
142 | * Add collection node. |
||
143 | * |
||
144 | * @param \DOMElement $element |
||
145 | * @param string|string[] $value |
||
146 | * |
||
147 | * @internal param string $key |
||
148 | */ |
||
149 | protected function addCollectionNode(DOMElement $element, $value) |
||
161 | |||
162 | /** |
||
163 | * Add sequential node. |
||
164 | * |
||
165 | * @param \DOMElement $element |
||
166 | * @param string|string[] $value |
||
167 | * |
||
168 | * @internal param string $key |
||
169 | */ |
||
170 | protected function addSequentialNode(DOMElement $element, $value) |
||
182 | |||
183 | /** |
||
184 | * Check if array are all sequential. |
||
185 | * |
||
186 | * @param array|string $value |
||
187 | * |
||
188 | * @return bool |
||
189 | */ |
||
190 | protected function isArrayAllKeySequential($value) |
||
202 | |||
203 | /** |
||
204 | * Add attributes. |
||
205 | * |
||
206 | * @param \DOMElement $element |
||
207 | * @param string[] $data |
||
208 | */ |
||
209 | protected function addAttributes($element, $data) |
||
215 | |||
216 | /** |
||
217 | * Create the root element. |
||
218 | * |
||
219 | * @param string|array $rootElement |
||
220 | * @return DOMElement |
||
221 | */ |
||
222 | protected function createRootElement($rootElement) |
||
242 | } |
||
243 |
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.