From b6a9935314d4a8c2b43dfbeb729a78f41cdeac59 Mon Sep 17 00:00:00 2001 From: Joel Wurtz Date: Wed, 27 Apr 2011 06:34:41 -0700 Subject: [PATCH 1/3] [Serializer] [XmlEncoder] Allow decoder to extract attributes in root element --- .../Serializer/Encoder/XmlEncoder.php | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php b/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php index e6f5ff9b0e..91d42a49ee 100644 --- a/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php +++ b/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php @@ -54,8 +54,15 @@ class XmlEncoder extends AbstractEncoder implements DecoderInterface public function decode($data, $format) { $xml = simplexml_load_string($data); - if (!$xml->count()) { + if (!$xml->count() && !$xml->attributes()) { return (string) $xml; + } elseif (!$xml->count()) { + $data = array(); + foreach ($xml->attributes() as $attrkey => $attr) { + $data['@'.$attrkey] = (string) $attr; + } + $data['#'] = (string) $xml; + return $data; } return $this->parseXml($xml); } @@ -157,14 +164,14 @@ class XmlEncoder extends AbstractEncoder implements DecoderInterface private function parseXml($node) { $data = array(); + if ($node->attributes()) { + foreach ($node->attributes() as $attrkey => $attr) { + $data['@'.$attrkey] = (string) $attr; + } + } foreach ($node->children() as $key => $subnode) { if ($subnode->count()) { $value = $this->parseXml($subnode); - if ($subnode->attributes()) { - foreach ($subnode->attributes() as $attrkey => $attr) { - $value['@'.$attrkey] = (string) $attr; - } - } } elseif ($subnode->attributes()) { $value = array(); foreach ($subnode->attributes() as $attrkey => $attr) { From f11cc2de0b896644b61095e3319d0a8ea745223d Mon Sep 17 00:00:00 2001 From: Joel Wurtz Date: Wed, 27 Apr 2011 08:00:31 -0700 Subject: [PATCH 2/3] [Seriliazer] [XmlEncoder] Optimize conditions --- src/Symfony/Component/Serializer/Encoder/XmlEncoder.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php b/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php index 91d42a49ee..13da7c0ec7 100644 --- a/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php +++ b/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php @@ -54,9 +54,10 @@ class XmlEncoder extends AbstractEncoder implements DecoderInterface public function decode($data, $format) { $xml = simplexml_load_string($data); - if (!$xml->count() && !$xml->attributes()) { - return (string) $xml; - } elseif (!$xml->count()) { + if (!$xml->count()) { + if (!$xml->attributes()) { + return (string) $xml; + } $data = array(); foreach ($xml->attributes() as $attrkey => $attr) { $data['@'.$attrkey] = (string) $attr; From 5712b3bd0e2b11eda9dbfbfc32836158a7a0335a Mon Sep 17 00:00:00 2001 From: Brouznouf Date: Wed, 27 Apr 2011 22:16:24 +0200 Subject: [PATCH 3/3] [Serializer] [XmlEncoder] Add unit test for decoding / encoding root with attributes --- .../Serializer/Encoder/XmlEncoderTest.php | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/tests/Symfony/Tests/Component/Serializer/Encoder/XmlEncoderTest.php b/tests/Symfony/Tests/Component/Serializer/Encoder/XmlEncoderTest.php index 80df994b61..20197b854d 100644 --- a/tests/Symfony/Tests/Component/Serializer/Encoder/XmlEncoderTest.php +++ b/tests/Symfony/Tests/Component/Serializer/Encoder/XmlEncoderTest.php @@ -111,6 +111,32 @@ class XmlEncoderTest extends \PHPUnit_Framework_TestCase $this->assertEquals($expected, $this->encoder->encode($array, 'xml')); } + + public function testEncodeScalarRootAttributes() + { + $array = array( + '#' => 'Paul', + '@gender' => 'm' + ); + + $expected = ''."\n". + ''."\n"; + + $this->assertEquals($expected, $this->encoder->encode($array, 'xml')); + } + + public function testEncodeRootAttributes() + { + $array = array( + 'firstname' => 'Paul', + '@gender' => 'm' + ); + + $expected = ''."\n". + ''."\n"; + + $this->assertEquals($expected, $this->encoder->encode($array, 'xml')); + } public function testEncodeScalarWithAttribute() { @@ -159,6 +185,33 @@ class XmlEncoderTest extends \PHPUnit_Framework_TestCase $this->assertEquals($expected, $this->encoder->decode($source, 'xml')); } + + public function testDecodeScalarRootAttributes() + { + $source = ''."\n". + 'Peter'."\n"; + + $expected = array( + '#' => 'Peter', + '@gender' => 'M' + ); + + $this->assertEquals($expected, $this->encoder->decode($source, 'xml')); + } + + public function testDecodeRootAttributes() + { + $source = ''."\n". + 'PeterMac Calloway'."\n"; + + $expected = array( + 'firstname' => 'Peter', + 'lastname' => 'Mac Calloway', + '@gender' => 'M' + ); + + $this->assertEquals($expected, $this->encoder->decode($source, 'xml')); + } public function testDecodeArray() {