diff --git a/composer.json b/composer.json index 70e090c..3c86b76 100644 --- a/composer.json +++ b/composer.json @@ -13,10 +13,10 @@ ], "require": { - "php": "^7.0", + "php": "^7.1.3", "ext-xmlreader": "*", "maxakawizard/json-collection-parser": "^1.1", - "tightenco/collect": "^5.5.33" + "tightenco/collect": "^5.7" }, "require-dev": { diff --git a/src/Parsers/XMLParser.php b/src/Parsers/XMLParser.php index 80cd083..ba4d3b9 100644 --- a/src/Parsers/XMLParser.php +++ b/src/Parsers/XMLParser.php @@ -54,19 +54,23 @@ private function extractElement(String $elementName, $couldBeAnElementsList = fa { $elementCollection = (new Collection())->merge($this->getCurrentElementAttributes()); - while($this->reader->read()){ - if($this->isEndElement($elementName)){ + if($this->isEmptyElement($elementName)) { + return $elementCollection; + } + + while($this->reader->read()) { + if($this->isEndElement($elementName)) { break; } - if($this->isValue()){ - if($elementCollection->isEmpty()){ + if($this->isValue()) { + if($elementCollection->isEmpty()) { return trim($this->reader->value); } else { return $elementCollection->put($elementName, trim($this->reader->value)); } } - if($this->isElement()){ - if($couldBeAnElementsList){ + if($this->isElement()) { + if($couldBeAnElementsList) { $foundElementName = $this->reader->name; $elementCollection->push(new Collection($this->extractElement($foundElementName))); } else { @@ -132,4 +136,12 @@ private function isEndElement(String $elementName = null){ private function isValue(){ return $this->reader->nodeType == XMLReader::TEXT || $this->reader->nodeType === XMLReader::CDATA; } + + private function isEmptyElement(String $elementName = null){ + if($elementName) { + return $this->reader->isEmptyElement && $this->reader->name === $elementName; + } else { + return false; + } + } } diff --git a/tests/Parsers/XMLParserTest.php b/tests/Parsers/XMLParserTest.php index 3af756d..257e155 100644 --- a/tests/Parsers/XMLParserTest.php +++ b/tests/Parsers/XMLParserTest.php @@ -88,4 +88,13 @@ public function test_elements_lists_are_managed() $this->assertEquals($totalComments, $countedComments); } + + public function test_element_is_empty() + { + StreamParser::xml($this->stub)->each(function($book) { + if($book->has('reviews')) { + $this->assertEmpty($book->get('reviews')); + } + }); + } } diff --git a/tests/Stubs/sample.xml b/tests/Stubs/sample.xml index 8df9c81..bc86280 100644 --- a/tests/Stubs/sample.xml +++ b/tests/Stubs/sample.xml @@ -3,6 +3,7 @@ <book ISBN="10-000000-001"> <title>The Iliad and The Odyssey</title> <price>12.95</price> + <reviews/> <comments> <userComment rating="4"> Best translation I've read. @@ -15,6 +16,7 @@ <book ISBN="10-000000-999"> <title>Anthology of World Literature</title> <price>24.95</price> + <reviews/> <comments> <userComment rating="3"> Needs more modern literature. @@ -39,6 +41,7 @@ <comments> <userComment rating="4">Delicious!</userComment> </comments> + <reviews/> </book> <book ISBN="11-000000-004"> <title>Great Works of Art</title>