From a0303ac61e5951ca6c4a984f1c6c2dc0ad25eda4 Mon Sep 17 00:00:00 2001 From: Sammyjo20 <29132017+Sammyjo20@users.noreply.github.com> Date: Sun, 9 May 2021 16:52:37 +0100 Subject: [PATCH 1/5] Fixed same name duplicate check by using array_count_values instead --- src/XmlToArray.php | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/src/XmlToArray.php b/src/XmlToArray.php index b0ccba5..7a2032c 100755 --- a/src/XmlToArray.php +++ b/src/XmlToArray.php @@ -44,29 +44,20 @@ protected function convertAttributes(DOMNamedNodeMap $nodeMap): ?array return ['_attributes' => $result]; } - protected function isHomogenous(array $arr) - { - $firstValue = current($arr); - foreach ($arr as $val) { - if ($firstValue !== $val) { - return false; - } - } - - return true; - } - protected function convertDomElement(DOMElement $element) { - $sameNames = false; $result = $this->convertAttributes($element->attributes); + $sameNamesOccurrences = []; + if ($element->childNodes->length > 1) { $childNodeNames = []; - foreach ($element->childNodes as $key => $node) { + + foreach ($element->childNodes as $node) { $childNodeNames[] = $node->nodeName; } - $sameNames = $this->isHomogenous($childNodeNames); + + $sameNamesOccurrences = array_count_values($childNodeNames); } foreach ($element->childNodes as $key => $node) { @@ -81,10 +72,13 @@ protected function convertDomElement(DOMElement $element) continue; } if ($node instanceof DOMElement) { - if ($sameNames) { - $result[$node->nodeName][$key] = $this->convertDomElement($node); + $nodeName = $node->nodeName; + $hasSameName = array_key_exists($nodeName, $sameNamesOccurrences) && $sameNamesOccurrences[$nodeName] > 1; + + if ($hasSameName) { + $result[$nodeName][$key] = $this->convertDomElement($node); } else { - $result[$node->nodeName] = $this->convertDomElement($node); + $result[$nodeName] = $this->convertDomElement($node); } continue; From 1688131c4620ab7f258c620bd9f917cb768011a7 Mon Sep 17 00:00:00 2001 From: Sammyjo20 <29132017+Sammyjo20@users.noreply.github.com> Date: Sun, 9 May 2021 17:15:07 +0100 Subject: [PATCH 2/5] Fixed bug with keys --- src/XmlToArray.php | 20 ++++++++++++++++---- tests/XmlToArrayTest.php | 17 +++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/XmlToArray.php b/src/XmlToArray.php index 7a2032c..747245c 100755 --- a/src/XmlToArray.php +++ b/src/XmlToArray.php @@ -49,6 +49,7 @@ protected function convertDomElement(DOMElement $element) $result = $this->convertAttributes($element->attributes); $sameNamesOccurrences = []; + $sameNodeNameIndexes = []; if ($element->childNodes->length > 1) { $childNodeNames = []; @@ -60,7 +61,7 @@ protected function convertDomElement(DOMElement $element) $sameNamesOccurrences = array_count_values($childNodeNames); } - foreach ($element->childNodes as $key => $node) { + foreach ($element->childNodes as $node) { if ($node instanceof DOMCdataSection) { $result['_cdata'] = $node->data; @@ -75,12 +76,23 @@ protected function convertDomElement(DOMElement $element) $nodeName = $node->nodeName; $hasSameName = array_key_exists($nodeName, $sameNamesOccurrences) && $sameNamesOccurrences[$nodeName] > 1; - if ($hasSameName) { - $result[$nodeName][$key] = $this->convertDomElement($node); - } else { + if ($hasSameName === false) { $result[$nodeName] = $this->convertDomElement($node); + continue; + } + + // If we already have a child node with the same name, we need to increment + // and keep track of their index. + + if (isset($sameNodeNameIndexes[$nodeName])) { + $key = $sameNodeNameIndexes[$nodeName] + 1; + } else { + $key = 0; } + $result[$nodeName][$key] = $this->convertDomElement($node); + $sameNodeNameIndexes[$nodeName] = $key; + continue; } } diff --git a/tests/XmlToArrayTest.php b/tests/XmlToArrayTest.php index 20b81c5..0f7cc84 100755 --- a/tests/XmlToArrayTest.php +++ b/tests/XmlToArrayTest.php @@ -72,6 +72,9 @@ public function sameNameTest(array $array) { $xml = ArrayToXml::convert($array, 'items'); $convertedArr = XmlToArray::convert($xml); + + dd($convertedArr); + $this->assertSame(['items' => $array], XmlToArray::convert($xml)); } @@ -95,6 +98,20 @@ public function sameNameData() '_cdata' => 'Room Service from 18:00 to 21:00', ], ], + 'Factories' => [ + [ + '_attributes' => ['Code'=>'*EC'], + '_cdata' => 'Earliest check-in at 14:00', + ], + [ + '_attributes' => ['Code'=>'*LF'], + '_cdata' => '1 lift', + ], + [ + '_attributes' => ['Code'=>'*RS'], + '_cdata' => 'Room Service from 18:00 to 21:00', + ], + ], ], ], ], From 220da5c4068027c1abcfa4ddd80215bf35ded81a Mon Sep 17 00:00:00 2001 From: Sammyjo20 <29132017+Sammyjo20@users.noreply.github.com> Date: Sun, 9 May 2021 17:17:47 +0100 Subject: [PATCH 3/5] Updated tests --- tests/XmlToArrayTest.php | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/tests/XmlToArrayTest.php b/tests/XmlToArrayTest.php index 0f7cc84..fc17043 100755 --- a/tests/XmlToArrayTest.php +++ b/tests/XmlToArrayTest.php @@ -71,10 +71,6 @@ public function data() public function sameNameTest(array $array) { $xml = ArrayToXml::convert($array, 'items'); - $convertedArr = XmlToArray::convert($xml); - - dd($convertedArr); - $this->assertSame(['items' => $array], XmlToArray::convert($xml)); } @@ -86,30 +82,30 @@ public function sameNameData() 'Facilities' => [ 'Facility' => [ [ - '_attributes' => ['Code'=>'*EC'], - '_cdata' => 'Earliest check-in at 14:00', + '_attributes' => ['Code' => '*EC'], + '_cdata' => 'Earliest check-in at 14:00', ], [ - '_attributes' => ['Code'=>'*LF'], - '_cdata' => '1 lift', + '_attributes' => ['Code' => '*LF'], + '_cdata' => '1 lift', ], [ - '_attributes' => ['Code'=>'*RS'], - '_cdata' => 'Room Service from 18:00 to 21:00', + '_attributes' => ['Code' => '*RS'], + '_cdata' => 'Room Service from 18:00 to 21:00', ], ], - 'Factories' => [ + 'Locations' => [ [ - '_attributes' => ['Code'=>'*EC'], - '_cdata' => 'Earliest check-in at 14:00', + '_attributes' => ['ShortCode' => 'GB'], + '_cdata' => 'United Kingdom', ], [ - '_attributes' => ['Code'=>'*LF'], - '_cdata' => '1 lift', + '_attributes' => ['ShortCode' => 'USA'], + '_cdata' => 'United States of America', ], [ - '_attributes' => ['Code'=>'*RS'], - '_cdata' => 'Room Service from 18:00 to 21:00', + '_attributes' => ['ShortCode' => 'AMS'], + '_cdata' => 'Amsterdam', ], ], ], From 722a4438d9db97c670c812ba01b01689677a2a11 Mon Sep 17 00:00:00 2001 From: Sammyjo20 <29132017+Sammyjo20@users.noreply.github.com> Date: Sun, 9 May 2021 22:23:11 +0100 Subject: [PATCH 4/5] Removed unnecesary code --- src/XmlToArray.php | 17 +++-------------- tests/XmlToArrayTest.php | 3 +++ 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/src/XmlToArray.php b/src/XmlToArray.php index 747245c..29371b2 100755 --- a/src/XmlToArray.php +++ b/src/XmlToArray.php @@ -49,7 +49,6 @@ protected function convertDomElement(DOMElement $element) $result = $this->convertAttributes($element->attributes); $sameNamesOccurrences = []; - $sameNodeNameIndexes = []; if ($element->childNodes->length > 1) { $childNodeNames = []; @@ -67,11 +66,13 @@ protected function convertDomElement(DOMElement $element) continue; } + if ($node instanceof DOMText) { $result = $node->textContent; continue; } + if ($node instanceof DOMElement) { $nodeName = $node->nodeName; $hasSameName = array_key_exists($nodeName, $sameNamesOccurrences) && $sameNamesOccurrences[$nodeName] > 1; @@ -81,19 +82,7 @@ protected function convertDomElement(DOMElement $element) continue; } - // If we already have a child node with the same name, we need to increment - // and keep track of their index. - - if (isset($sameNodeNameIndexes[$nodeName])) { - $key = $sameNodeNameIndexes[$nodeName] + 1; - } else { - $key = 0; - } - - $result[$nodeName][$key] = $this->convertDomElement($node); - $sameNodeNameIndexes[$nodeName] = $key; - - continue; + $result[$nodeName][] = $this->convertDomElement($node); } } diff --git a/tests/XmlToArrayTest.php b/tests/XmlToArrayTest.php index fc17043..059bee7 100755 --- a/tests/XmlToArrayTest.php +++ b/tests/XmlToArrayTest.php @@ -71,6 +71,9 @@ public function data() public function sameNameTest(array $array) { $xml = ArrayToXml::convert($array, 'items'); + + dd(XmlToArray::convert($xml)); + $this->assertSame(['items' => $array], XmlToArray::convert($xml)); } From 5c354081a9085a202b71b1fde1ac08001ba8bb4d Mon Sep 17 00:00:00 2001 From: Sammyjo20 <29132017+Sammyjo20@users.noreply.github.com> Date: Sun, 9 May 2021 22:23:56 +0100 Subject: [PATCH 5/5] Removed dd() --- tests/XmlToArrayTest.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/XmlToArrayTest.php b/tests/XmlToArrayTest.php index 059bee7..fc17043 100755 --- a/tests/XmlToArrayTest.php +++ b/tests/XmlToArrayTest.php @@ -71,9 +71,6 @@ public function data() public function sameNameTest(array $array) { $xml = ArrayToXml::convert($array, 'items'); - - dd(XmlToArray::convert($xml)); - $this->assertSame(['items' => $array], XmlToArray::convert($xml)); }