From ec08d17e3a0e7ebaab32312acedbbb0fef4c744f Mon Sep 17 00:00:00 2001 From: leomunsa Date: Thu, 3 Jul 2025 12:20:14 -0300 Subject: [PATCH] includes the old xml conversion toArray --- src/XmlNode.php | 42 +++++++++++++++++++++++++++++++++++++----- tests/XmlUtilTest.php | 25 +++++++++++++++++++++---- 2 files changed, 58 insertions(+), 9 deletions(-) diff --git a/src/XmlNode.php b/src/XmlNode.php index 84b921c..3cfaad1 100644 --- a/src/XmlNode.php +++ b/src/XmlNode.php @@ -260,14 +260,13 @@ public function removeTagName(string $tagName): bool } } - - public function toArray(Closure $func = null): array + public function toFullArray(): array { $sxml = simplexml_import_dom($this->DOMNode()); - return [$sxml->getName() => $this->_toArray($sxml)]; + return [$sxml->getName() => $this->_toFullArray($sxml)]; } - protected function _toArray(SimpleXMLElement $node): array|string + protected function _toFullArray(SimpleXMLElement $node): array|string { $hasAttributes = (count($node->attributes()) > 0); $hasChildren = (count($node->children()) > 0); @@ -289,7 +288,7 @@ protected function _toArray(SimpleXMLElement $node): array|string if ($hasChildren) { foreach ($node->children() as $child) { $childName = $child->getName(); - $childData = $this->_toArray($child); + $childData = $this->_toFullArray($child); if (isset($output[$childName])) { if (!is_array($output[$childName]) || !isset($output[$childName][0])) { @@ -309,6 +308,39 @@ protected function _toArray(SimpleXMLElement $node): array|string return $output; } + public function toArray(Closure $func = null): array + { + return $this->_toArray($this->DOMNode(), $func); + } + + protected function _toArray(SimpleXMLElement|DOMNode|array $arr, Closure|null $func = null): array + { + if ($arr instanceof SimpleXMLElement) { + return $this->_toArray((array) $arr, $func); + } + + if ($arr instanceof DOMNode) { + return $this->_toArray((array) simplexml_import_dom($arr), $func); + } + + $newArr = array(); + if (!empty($arr)) { + foreach ($arr as $key => $value) { + $newArr[$key] = + ( + is_array($value) + || ($value instanceof DOMNode) + || ($value instanceof SimpleXMLElement) + ? $this->_toArray($value, $func) + : (!empty($func) ? $func($value) : $value) + ); + } + } + + return $newArr; + } + + /** * @param string|null $prefix * @param string $uri diff --git a/tests/XmlUtilTest.php b/tests/XmlUtilTest.php index 43cc3ac..a11eb27 100644 --- a/tests/XmlUtilTest.php +++ b/tests/XmlUtilTest.php @@ -310,19 +310,36 @@ public function testRemoveNode(): void } - public function testXml2Array(): void + public function testXml2Array1(): void { $file = new File(__DIR__ . '/buggy.xml'); $xml = new XmlDocument($file, preserveWhiteSpace: false); $array = $xml->toArray(); + $this->assertEquals([ "node" => [ "subnode" => "value"]], $array); + } + + public function testXml2Array2(): void + { + $xml = new XmlDocument('value'); + + $array = $xml->toArray(); + $this->assertEquals([ "node" => "value"], $array); + } + + public function testXml2FullArray(): void + { + $file = new File(__DIR__ . '/buggy.xml'); + $xml = new XmlDocument($file, preserveWhiteSpace: false); + + $array = $xml->toFullArray(); $this->assertEquals(['root' => [ "node" => [ "subnode" => "value"]]], $array); } public function testXmlToArrayWithAttributeAndNoText(): void { $xml = new XmlDocument(''); - $array = $xml->toArray(); + $array = $xml->toFullArray(); $expected = [ 'root' => [ @@ -338,7 +355,7 @@ public function testXmlToArrayWithAttributeAndNoText(): void public function testXmlToArrayWithAttributeAndText(): void { $xml = new XmlDocument('value'); - $array = $xml->toArray(); + $array = $xml->toFullArray(); $expected = [ 'root' => [ @@ -356,7 +373,7 @@ public function testXmlToArrayWithMixedContent(): void { $xmlString = 'valuevalue'; $xml = new XmlDocument($xmlString); - $array = $xml->toArray(); + $array = $xml->toFullArray(); $expected = [ 'root' => [