Merge remote branch 'Brouznouf/patch-2'
* Brouznouf/patch-2: [Serializer] [XmlEncoder] Add unit test for decoding / encoding root with attributes [Seriliazer] [XmlEncoder] Optimize conditions [Serializer] [XmlEncoder] Allow decoder to extract attributes in root element
This commit is contained in:
commit
9e23189eb1
@ -55,7 +55,15 @@ class XmlEncoder extends AbstractEncoder implements DecoderInterface
|
|||||||
{
|
{
|
||||||
$xml = simplexml_load_string($data);
|
$xml = simplexml_load_string($data);
|
||||||
if (!$xml->count()) {
|
if (!$xml->count()) {
|
||||||
return (string) $xml;
|
if (!$xml->attributes()) {
|
||||||
|
return (string) $xml;
|
||||||
|
}
|
||||||
|
$data = array();
|
||||||
|
foreach ($xml->attributes() as $attrkey => $attr) {
|
||||||
|
$data['@'.$attrkey] = (string) $attr;
|
||||||
|
}
|
||||||
|
$data['#'] = (string) $xml;
|
||||||
|
return $data;
|
||||||
}
|
}
|
||||||
return $this->parseXml($xml);
|
return $this->parseXml($xml);
|
||||||
}
|
}
|
||||||
@ -157,14 +165,14 @@ class XmlEncoder extends AbstractEncoder implements DecoderInterface
|
|||||||
private function parseXml($node)
|
private function parseXml($node)
|
||||||
{
|
{
|
||||||
$data = array();
|
$data = array();
|
||||||
|
if ($node->attributes()) {
|
||||||
|
foreach ($node->attributes() as $attrkey => $attr) {
|
||||||
|
$data['@'.$attrkey] = (string) $attr;
|
||||||
|
}
|
||||||
|
}
|
||||||
foreach ($node->children() as $key => $subnode) {
|
foreach ($node->children() as $key => $subnode) {
|
||||||
if ($subnode->count()) {
|
if ($subnode->count()) {
|
||||||
$value = $this->parseXml($subnode);
|
$value = $this->parseXml($subnode);
|
||||||
if ($subnode->attributes()) {
|
|
||||||
foreach ($subnode->attributes() as $attrkey => $attr) {
|
|
||||||
$value['@'.$attrkey] = (string) $attr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} elseif ($subnode->attributes()) {
|
} elseif ($subnode->attributes()) {
|
||||||
$value = array();
|
$value = array();
|
||||||
foreach ($subnode->attributes() as $attrkey => $attr) {
|
foreach ($subnode->attributes() as $attrkey => $attr) {
|
||||||
|
@ -111,6 +111,32 @@ class XmlEncoderTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
$this->assertEquals($expected, $this->encoder->encode($array, 'xml'));
|
$this->assertEquals($expected, $this->encoder->encode($array, 'xml'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testEncodeScalarRootAttributes()
|
||||||
|
{
|
||||||
|
$array = array(
|
||||||
|
'#' => 'Paul',
|
||||||
|
'@gender' => 'm'
|
||||||
|
);
|
||||||
|
|
||||||
|
$expected = '<?xml version="1.0"?>'."\n".
|
||||||
|
'<response gender="m"><![CDATA[Paul]]></response>'."\n";
|
||||||
|
|
||||||
|
$this->assertEquals($expected, $this->encoder->encode($array, 'xml'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testEncodeRootAttributes()
|
||||||
|
{
|
||||||
|
$array = array(
|
||||||
|
'firstname' => 'Paul',
|
||||||
|
'@gender' => 'm'
|
||||||
|
);
|
||||||
|
|
||||||
|
$expected = '<?xml version="1.0"?>'."\n".
|
||||||
|
'<response gender="m"><firstname><![CDATA[Paul]]></firstname></response>'."\n";
|
||||||
|
|
||||||
|
$this->assertEquals($expected, $this->encoder->encode($array, 'xml'));
|
||||||
|
}
|
||||||
|
|
||||||
public function testEncodeScalarWithAttribute()
|
public function testEncodeScalarWithAttribute()
|
||||||
{
|
{
|
||||||
@ -159,6 +185,33 @@ class XmlEncoderTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
$this->assertEquals($expected, $this->encoder->decode($source, 'xml'));
|
$this->assertEquals($expected, $this->encoder->decode($source, 'xml'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testDecodeScalarRootAttributes()
|
||||||
|
{
|
||||||
|
$source = '<?xml version="1.0"?>'."\n".
|
||||||
|
'<person gender="M">Peter</person>'."\n";
|
||||||
|
|
||||||
|
$expected = array(
|
||||||
|
'#' => 'Peter',
|
||||||
|
'@gender' => 'M'
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals($expected, $this->encoder->decode($source, 'xml'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testDecodeRootAttributes()
|
||||||
|
{
|
||||||
|
$source = '<?xml version="1.0"?>'."\n".
|
||||||
|
'<person gender="M"><firstname>Peter</firstname><lastname>Mac Calloway</lastname></person>'."\n";
|
||||||
|
|
||||||
|
$expected = array(
|
||||||
|
'firstname' => 'Peter',
|
||||||
|
'lastname' => 'Mac Calloway',
|
||||||
|
'@gender' => 'M'
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals($expected, $this->encoder->decode($source, 'xml'));
|
||||||
|
}
|
||||||
|
|
||||||
public function testDecodeArray()
|
public function testDecodeArray()
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user