8000 [Serializer] XmlEncoder: fix negative int and large numbers handling · symfony/symfony@5a74492 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5a74492

Browse files
committed
[Serializer] XmlEncoder: fix negative int and large numbers handling
1 parent 5742958 commit 5a74492

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

src/Symfony/Component/Serializer/Encoder/XmlEncoder.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -301,11 +301,19 @@ private function parseXmlAttributes(\DOMNode $node)
301301
$data = array();
302302

303303
foreach ($node->attributes as $attr) {
304-
if (ctype_digit($attr->nodeValue)) {
305-
$data['@'.$attr->nodeName] = (int) $attr->nodeValue;
306-
} else {
307-
$data['@'.$attr->nodeName] = $attr->nodeValue;
304+
if (!is_numeric($attr->nodeValue)) {
305+
$data['@' . $attr->nodeName] = $attr->nodeValue;
306+
307+
continue;
308+
}
309+
310+
if (false !== $val = filter_var($attr->nodeValue, FILTER_VALIDATE_INT)) {
311+
$data['@' . $attr->nodeName] = $val;
312+
313+
continue;
308314
}
315+
316+
$data['@' . $attr->nodeName] = (float) $attr->nodeValue;
309317
}
310318

311319
return $data;

src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,46 @@ public function testDecodeScalar()
222222
$this->assertEquals('foo', $this->encoder->decode($source, 'xml'));
223223
}
224224

225+
public function testDecodeBigDigitAttributes()
226+
{
227+
$source = <<<XML
228+
<?xml version="1.0"?>
229+
<document index="182077241760011681341821060401202210011000045913000000017100">Name</document>
230+
XML;
231+
232+
$this->assertSame(array('@index' => 182077241760011681341821060401202210011000045913000000017100, '#' => 'Name'), $this->encoder->decode($source, 'xml'));
233+
}
234+
235+
public function testDecodeNegativeIntAttribute()
236+
{
237+
$source = <<<XML
238+
<?xml version="1.0"?>
239+
<document index="-1234">Name</document>
240+
XML;
241+
242+
$this->assertSame(array('@index' => -1234, '#' => 'Name'), $this->encoder->decode($source, 'xml'));
243+
}
244+
245+
public function testDecodeFloatAttribute()
246+
{
247+
$source = <<<XML
248+
<?xml version="1.0"?>
249+
<document index="-12.11">Name</document>
250+
XML;
251+
252+
$this->assertSame(array('@index' => -12.11, '#' => 'Name'), $this->encoder->decode($source, 'xml'));
253+
}
254+
255+
public function testDecodeNegativeFloatAttribute()
256+
{
257+
$source = <<<XML
258+
<?xml version="1.0"?>
259+
<document index="-12.11">Name</document>
260+
XML;
261+
262+
$this->assertSame(array('@index' => -12.11, '#' => 'Name'), $this->encoder->decode($source, 'xml'));
263+
}
264+
225265
public function testEncode()
226266
{
227267
$source = $this->getXmlSource();
@@ -575,6 +615,7 @@ public function testEncodeXmlWithBoolValue()
575615
$this->assertEquals($expectedXml, $actualXml);
576616
}
577617

618+
578619
public function testEncodeXmlWithDateTimeObjectValue()
579620
{
580621
$xmlEncoder = $this->createXmlEncoderWithDateTimeNormalizer();

0 commit comments

Comments
 (0)
0