[Serializer] fix decoding float XML attributes starting with 0
This commit is contained in:
parent
1eead3f2af
commit
97b4306c30
@ -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;
|
||||
|
@ -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
|
||||
|
Reference in New Issue
Block a user