@@ -43,26 +43,28 @@ final class XmlEncoder implements XmlEncoderInterface
4343 /**
4444 * {@inheritDoc}
4545 */
46- public function encode (
47- array $ data ,
48- ?array $ namespace = null ,
49- ?DOMElement $ parent = null ,
50- ?XmlDocumentInterface $ doc = null
51- ): XmlDocumentInterface {
52- // If there is no complete XML document (from root, not a node), then
53- // it is created, since it will be needed to create the future nodes.
54- if ($ doc === null ) {
55- $ doc = new XmlDocument ();
56- }
46+ public function encode (array $ data ): XmlDocumentInterface
47+ {
48+ $ doc = new XmlDocument ();
5749
58- // If there is no parent element, then it is being requested to create
59- // the XML document from 0 (from the root node).
60- if ($ parent === null ) {
61- $ parent = $ doc ;
62- }
50+ $ this ->encodeNode ($ data , $ doc , $ doc );
6351
64- // Iterate the first level of the array to find the tags that must be
65- // added to the XML document.
52+ return $ doc ;
53+ }
54+
55+ /**
56+ * Recursively encodes an array into XML nodes and appends them to a parent.
57+ *
58+ * @param array $data The array with the data to encode.
59+ * @param XmlDocumentInterface $doc The root XML document.
60+ * @param DOMNode $parent The parent node to append nodes to.
61+ * @return void
62+ */
63+ private function encodeNode (
64+ array $ data ,
65+ XmlDocumentInterface $ doc ,
66+ DOMNode $ parent ,
67+ ): void {
6668 foreach ($ data as $ key => $ value ) {
6769
6870 // If the index is '@attributes' then the value of this index is an
@@ -97,33 +99,18 @@ public function encode(
9799 // Only the node is created if it has child nodes. The node will
98100 // not be created if an empty array (without children) is passed.
99101 if (!empty ($ value )) {
100- $ this ->nodeAddChilds (
101- $ doc ,
102- $ parent ,
103- $ key ,
104- $ value ,
105- $ namespace
106- );
102+ $ this ->nodeAddChilds ($ doc , $ parent , $ key , $ value );
107103 }
108104 }
109105
110106 // The node is a scalar (not an array, not child nodes). So the
111107 // node is created and the value is assigned directly.
112108 else {
113109 if (!$ this ->skipValue ($ value )) {
114- $ this ->nodeAddValue (
115- $ doc ,
116- $ parent ,
117- $ key ,
118- (string ) $ value ,
119- $ namespace
120- );
110+ $ this ->nodeAddValue ($ doc , $ parent , $ key , (string ) $ value );
121111 }
122112 }
123113 }
124-
125- // Return the generated XML document.
126- return $ doc ;
127114 }
128115
129116 /**
@@ -162,7 +149,6 @@ private function nodeAddAttributes(DOMElement $node, array $attributes): void
162149 * @param DOMNode $parent Node parent to which the child nodes will be added.
163150 * @param string $tagName Name of the child node tag.
164151 * @param array $childs Array of data of the child nodes.
165- * @param array|null $namespace XML namespace (URI and prefix).
166152 * @return void
167153 * @throws XmlEncoderException If a child node is not an array.
168154 */
@@ -171,7 +157,6 @@ private function nodeAddChilds(
171157 DOMNode $ parent ,
172158 string $ tagName ,
173159 array $ childs ,
174- ?array $ namespace = null ,
175160 ): void {
176161 $ keys = array_keys ($ childs );
177162 if (!is_int ($ keys [0 ])) {
@@ -196,30 +181,15 @@ private function nodeAddChilds(
196181 ));
197182 }
198183
199- // Add child nodes of the child node (add associative to the
200- // node $tagName).
201- $ Node = $ namespace
202- ? $ doc ->createElementNS (
203- $ namespace [0 ],
204- $ namespace [1 ] . ': ' . $ tagName
205- )
206- : $ doc ->createElement ($ tagName )
207- ;
184+ $ Node = $ doc ->createElement ($ tagName );
208185 $ parent ->appendChild ($ Node );
209- $ this ->encode ($ child , $ namespace , $ Node, $ doc );
186+ $ this ->encodeNode ($ child , $ doc , $ Node );
210187 }
211188 // If the child is not an array, it is simply a duplicate node that
212189 // must be added at the same level as the parent node.
213190 else {
214191 $ value = XmlHelper::sanitize ((string ) $ child );
215- $ Node = $ namespace
216- ? $ doc ->createElementNS (
217- $ namespace [0 ],
218- $ namespace [1 ] . ': ' . $ tagName ,
219- $ value
220- )
221- : $ doc ->createElement ($ tagName , $ value )
222- ;
192+ $ Node = $ doc ->createElement ($ tagName , $ value );
223193 $ parent ->appendChild ($ Node );
224194 }
225195 }
@@ -232,25 +202,16 @@ private function nodeAddChilds(
232202 * @param DOMNode $parent Node parent to which the node will be added.
233203 * @param string $tagName Name of the child node tag.
234204 * @param string $value Value of the child node.
235- * @param array|null $namespace XML namespace (URI and prefix).
236205 * @return void
237206 */
238207 private function nodeAddValue (
239208 XmlDocumentInterface $ doc ,
240209 DOMNode $ parent ,
241210 string $ tagName ,
242211 string $ value ,
243- ?array $ namespace = null ,
244212 ): void {
245213 $ value = XmlHelper::sanitize ($ value );
246- $ Node = $ namespace
247- ? $ doc ->createElementNS (
248- $ namespace [0 ],
249- $ namespace [1 ] . ': ' . $ tagName ,
250- $ value
251- )
252- : $ doc ->createElement ($ tagName , $ value )
253- ;
214+ $ Node = $ doc ->createElement ($ tagName , $ value );
254215 $ parent ->appendChild ($ Node );
255216 }
256217
@@ -268,19 +229,4 @@ private function skipValue(mixed $value): bool
268229 true
269230 );
270231 }
271-
272- /**
273- * Checks if a value must generate an empty XML node.
274- *
275- * @param mixed $value Value to check.
276- * @return bool `true` if the value must generate an empty node, `false` otherwise.
277- */
278- // private function createWithEmptyValue(mixed $value): bool
279- // {
280- // return in_array(
281- // $value,
282- // $this->rules['node_values']['generate_empty'],
283- // true
284- // );
285- // }
286232}
0 commit comments