[Serializer] fix decoding float XML attributes starting with 0

This commit is contained in:
Marcin Kruk 2020-10-21 20:47:39 +00:00 committed by Fabien Potencier
parent 1eead3f2af
commit 97b4306c30
2 changed files with 13 additions and 3 deletions

View File

@ -304,7 +304,7 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
$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;

View File

@ -268,10 +268,10 @@ XML;
{
$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()
@ -284,6 +284,16 @@ XML;
$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