Skip to content

Commit d95a743

Browse files
committed
Merge remote branch 'Brouznouf/master'
* Brouznouf/master: [Serializer] Revert DOMElement to SimpleXmlElement [Serializer] Using DOMElement instead of SimpleXmlElement in XmlEncoder to permit some behavior
2 parents 55a8f8d + 86343b4 commit d95a743

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,20 +165,27 @@ private function parseXml($node)
165165
$value['@'.$attrkey] = (string) $attr;
166166
}
167167
}
168+
} elseif ($subnode->attributes()) {
169+
$value = array();
170+
foreach ($subnode->attributes() as $attrkey => $attr) {
171+
$value['@'.$attrkey] = (string) $attr;
172+
}
173+
$value['#'] = (string) $subnode;
168174
} else {
169175
$value = (string) $subnode;
170176
}
177+
171178
if ($key === 'item') {
172-
if (isset($subnode['key'])) {
173-
$data[(string)$subnode['key']] = $value;
179+
if (isset($value['@key'])) {
180+
$data[(string)$value['@key']] = $value['#'];
174181
} elseif (isset($data['item'])) {
175182
$tmp = $data['item'];
176183
unset($data['item']);
177184
$data[] = $tmp;
178185
$data[] = $value;
179186
}
180187
} elseif (key_exists($key, $data)) {
181-
if (false === is_array($data[$key])) {
188+
if ((false === is_array($data[$key])) || (false === isset($data[$key][0]))) {
182189
$data[$key] = array($data[$key]);
183190
}
184191
$data[$key][] = $value;
@@ -205,6 +212,8 @@ private function buildXml($parentNode, $data)
205212
//Ah this is the magic @ attribute types.
206213
if (0 === strpos($key, "@") && is_scalar($data) && $this->isElementNameValid($attributeName = substr($key,1))) {
207214
$parentNode->setAttribute($attributeName, $data);
215+
} elseif ($key === '#') {
216+
$append = $this->selectNodeType($parentNode, $data);
208217
} elseif (is_array($data) && false === is_numeric($key)) {
209218
/**
210219
* Is this array fully numeric keys?

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

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,18 @@ public function testEncodeSimpleXML()
111111

112112
$this->assertEquals($expected, $this->encoder->encode($array, 'xml'));
113113
}
114+
115+
public function testEncodeScalarWithAttribute()
116+
{
117+
$array = array(
118+
'person' => array('@gender' => 'M', '#' => 'Peter'),
119+
);
120+
121+
$expected = '<?xml version="1.0"?>'."\n".
122+
'<response><person gender="M"><![CDATA[Peter]]></person></response>'."\n";
123+
124+
$this->assertEquals($expected, $this->encoder->encode($array, 'xml'));
125+
}
114126

115127
public function testDecodeScalar()
116128
{
@@ -135,6 +147,38 @@ public function testDecode()
135147

136148
$this->assertEquals(get_object_vars($obj), $this->encoder->decode($source, 'xml'));
137149
}
150+
151+
public function testDecodeScalarWithAttribute()
152+
{
153+
$source = '<?xml version="1.0"?>'."\n".
154+
'<response><person gender="M">Peter</person></response>'."\n";
155+
156+
$expected = array(
157+
'person' => array('@gender' => 'M', '#' => 'Peter'),
158+
);
159+
160+
$this->assertEquals($expected, $this->encoder->decode($source, 'xml'));
161+
}
162+
163+
public function testDecodeArray()
164+
{
165+
$source = '<?xml version="1.0"?>'."\n".
166+
'<response>'.
167+
'<people>'.
168+
'<person><firstname>Benjamin</firstname><lastname>Alexandre</lastname></person>'.
169+
'<person><firstname>Damien</firstname><lastname>Clay</lastname></person>'.
170+
'</people>'.
171+
'</response>'."\n";
172+
173+
$expected = array(
174+
'people' => array('person' => array(
175+
array('firstname' => 'Benjamin', 'lastname' => 'Alexandre'),
176+
array('firstname' => 'Damien', 'lastname' => 'Clay')
177+
))
178+
);
179+
180+
$this->assertEquals($expected, $this->encoder->decode($source, 'xml'));
181+
}
138182

139183
protected function getXmlSource()
140184
{
@@ -153,7 +197,7 @@ protected function getObject()
153197
$obj = new Dummy;
154198
$obj->foo = 'foo';
155199
$obj->bar = array('a', 'b');
156-
$obj->baz = array('key' => 'val', 'key2' => 'val', 'A B' => 'bar', "Barry" => array('FooBar' => array("@id"=>1,"Baz"=>"Ed")));
200+
$obj->baz = array('key' => 'val', 'key2' => 'val', 'A B' => 'bar', "Barry" => array('FooBar' => array("Baz"=>"Ed", "@id"=>1)));
157201
$obj->qux = "1";
158202
return $obj;
159203
}

0 commit comments

Comments
 (0)