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 type |
||
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 | * 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) |
||
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) |
||
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) |
||
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) |
||
188 | |||
189 | /** |
||
190 | * Add attributes. |
||
191 | * |
||
192 | * @param \DOMElement $element |
||
193 | * @param string[] $data |
||
194 | */ |
||
195 | protected function addAttributes($element, $data) |
||
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.