Skip to content

Commit

Permalink
xml value tag is multiple, so return it as array of values (#34)
Browse files Browse the repository at this point in the history
Co-authored-by: Ondřej Sochůrek <sochurek@evosoft.cz>
  • Loading branch information
BigOHenry and Ondřej Sochůrek authored Nov 17, 2022
1 parent ac12ff5 commit b47d617
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/Xml/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected function getValue($content, ?string $use, ?string $default = null)
}

/**
* Cast value to string only when it is an instance of SimpleXMLElement.
* Cast value to string|array only when it is an instance of SimpleXMLElement.
*
* @param mixed $value
*
Expand All @@ -82,7 +82,11 @@ protected function getValue($content, ?string $use, ?string $default = null)
protected function castValue($value)
{
if ($value instanceof SimpleXMLElement) {
$value = (string) $value;
if ($value->count() > 1) {
$value = (array) $value;
} else {
$value = (string) $value;
}
}

return $value;
Expand Down
6 changes: 5 additions & 1 deletion tests/Xml/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,10 @@ public function testParseValueCollectionMultiLevelsPartTwo()
[
'DataAreaCode' => 'AREA2_CODE',
'Detail' => [
'DynPar' => 'T0019:2,R0109:test2,R0108:,R0107:,T0025:X',
'DynPar' => [
'T0019:2,R0109:test2,R0108:,R0107:,T0025:X',
'T0019:3,R0109:test3,R0108:,R0107:,T0025:Y',
],
],
],
],
Expand Down Expand Up @@ -796,6 +799,7 @@ public function testParseValueCollectionMultiLevelsPartTwo()
<DataAreaCode>AREA2_CODE</DataAreaCode>
<Detail>
<DynPar>T0019:2,R0109:test2,R0108:,R0107:,T0025:X</DynPar>
<DynPar>T0019:3,R0109:test3,R0108:,R0107:,T0025:Y</DynPar>
</Detail>
</Localization>
</Error>
Expand Down

0 comments on commit b47d617

Please sign in to comment.