Skip to content

Commit

Permalink
Add withSeparatedParametersList() to XMLParser
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiorodenas committed Jan 24, 2021
1 parent 6eade98 commit 08deb01
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ array(2) {
}
}
```
Additionally, you could make use of ```->withSeparatedParametersList()``` to get the params of each element separated on the ```__params``` property.

### JSON
```json
Expand Down
16 changes: 15 additions & 1 deletion src/Parsers/XMLParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class XMLParser implements StreamParserInterface
protected $reader,$source;

protected $skipFirstElement = true;
protected $separateParameters = false;

public function from(String $source): StreamParserInterface
{
Expand All @@ -28,6 +29,12 @@ public function from(String $source): StreamParserInterface
return $this;
}

public function withSeparatedParametersList(){
$this->separateParameters = true;

return $this;
}

public function withoutSkippingFirstElement(){
$this->skipFirstElement = false;

Expand All @@ -54,7 +61,14 @@ private function extractElement(String $elementName, $couldBeAnElementsList = fa
{
$emptyElement = $this->isEmptyElement($elementName);

$elementCollection = (new Collection())->merge($this->getCurrentElementAttributes());
$elementCollection = new Collection();

$elementParameters = $this->getCurrentElementAttributes();
if($this->separateParameters){
$elementCollection->put('__params', $elementParameters);
} else {
$elementCollection = $elementCollection->merge($elementParameters);
}

if($emptyElement) {
return $elementCollection;
Expand Down
19 changes: 19 additions & 0 deletions tests/Parsers/XMLParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,5 +124,24 @@ public function test_element_name_in_sub_element()
$this->assertEquals($author->get('name'), "Test");
}
});
}

public function test_separate_parameters_list()
{
$ISBNList = [
"10-000000-001",
"11-000000-002",
"11-000000-003",
"11-000000-004",
"10-000000-999",
"11-000000-005",
"11-000000-006"
];

StreamParser::xml($this->stub)->withSeparatedParametersList()->each(function($book) use ($ISBNList) {
if ($book->has('__params')) {
$this->assertContains($book->get('__params')->get('ISBN'), $ISBNList);
}
});
}
}

0 comments on commit 08deb01

Please sign in to comment.