Skip to content

Commit

Permalink
Merge pull request #17 from andresnijder/hotfix/xml-empty-element
Browse files Browse the repository at this point in the history
Fix XML empty elements parsing
  • Loading branch information
sergiorodenas authored Oct 29, 2018
2 parents 698c63c + 40dd4bf commit a160e49
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
24 changes: 18 additions & 6 deletions src/Parsers/XMLParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
}
}
9 changes: 9 additions & 0 deletions tests/Parsers/XMLParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
});
}
}
3 changes: 3 additions & 0 deletions tests/Stubs/sample.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -39,6 +41,7 @@
<comments>
<userComment rating="4">Delicious!</userComment>
</comments>
<reviews/>
</book>
<book ISBN="11-000000-004">
<title>Great Works of Art</title>
Expand Down

0 comments on commit a160e49

Please sign in to comment.