Skip to content

Commit

Permalink
[Serializer] fix decoding float XML attributes starting with 0
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcin Kruk authored and fabpot committed Oct 23, 2020
1 parent 3ceb772 commit 0781320
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Encoder/XmlEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ private function parseXmlAttributes(\DOMNode $node, array $context = [])
$typeCastAttributes = $this->resolveXmlTypeCastAttributes($context);

foreach ($node->attributes as $attr) {
if (!is_numeric($attr->nodeValue) || !$typeCastAttributes || (isset($attr->nodeValue[1]) && '0' === $attr->nodeValue[0])) {
if (!is_numeric($attr->nodeValue) || !$typeCastAttributes || (isset($attr->nodeValue[1]) && '0' === $attr->nodeValue[0] && '.' !== $attr->nodeValue[1])) {
$data['@'.$attr->nodeName] = $attr->nodeValue;

continue;
Expand Down
14 changes: 12 additions & 2 deletions Tests/Encoder/XmlEncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,10 @@ public function testDecodeFloatAttribute()
{
$source = <<<XML
<?xml version="1.0"?>
<document index="-12.11">Name</document>
<document index="12.11">Name</document>
XML;

$this->assertSame(['@index' => -12.11, '#' => 'Name'], $this->encoder->decode($source, 'xml'));
$this->assertSame(['@index' => 12.11, '#' => 'Name'], $this->encoder->decode($source, 'xml'));
}

public function testDecodeNegativeFloatAttribute()
Expand All @@ -284,6 +284,16 @@ public function testDecodeNegativeFloatAttribute()
$this->assertSame(['@index' => -12.11, '#' => 'Name'], $this->encoder->decode($source, 'xml'));
}

public function testDecodeFloatAttributeWithZeroWholeNumber()
{
$source = <<<XML
<?xml version="1.0"?>
<document index="0.123">Name</document>
XML;

$this->assertSame(['@index' => 0.123, '#' => 'Name'], $this->encoder->decode($source, 'xml'));
}

public function testNoTypeCastAttribute()
{
$source = <<<XML
Expand Down

0 comments on commit 0781320

Please sign in to comment.