merged branch fabpot/serializer (PR #6302)

This PR was submitted for the master branch but it was merged into the 2.1 branch instead (closes #6302).

Commits
-------

d9b75a3 [Serializer] - Test undefined index #
1a3b985 Maintain array structure
0b9a831 Check if key # is defined in $value

Discussion
----------

[Serializer] - undefined index #

replaces #6293
This commit is contained in:
Fabien Potencier 2012-12-12 18:17:17 +01:00
commit e0cefd7f7e
2 changed files with 39 additions and 2 deletions

View File

@ -240,11 +240,15 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
if ($key === 'item') {
if (isset($value['@key'])) {
$data[(string) $value['@key']] = $value['#'];
if (isset($value['#'])) {
$data[(string) $value['@key']] = $value['#'];
} else {
$data[(string) $value['@key']] = $value;
}
} else {
$data['item'][] = $value;
}
} elseif (array_key_exists($key, $data)) {
} elseif (array_key_exists($key, $data) || $key == "entry") {
if ((false === is_array($data[$key])) || (false === isset($data[$key][0]))) {
$data[$key] = array($data[$key]);
}

View File

@ -251,6 +251,39 @@ class XmlEncoderTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($expected, $this->encoder->decode($source, 'xml'));
}
public function testDecodeWithoutItemHash()
{
$obj = new ScalarDummy;
$obj->xmlFoo = array(
'foo-bar' => array(
'@key' => "value",
'item' => array("@key" => 'key', "key-val" => 'val')
),
'Foo' => array(
'Bar' => "Test",
'@Type' => 'test'
),
'föo_bär' => 'a',
"Bar" => array(1,2,3),
'a' => 'b',
);
$expected = array(
'foo-bar' => array(
'@key' => "value",
'key' => array('@key' => 'key', "key-val" => 'val')
),
'Foo' => array(
'Bar' => "Test",
'@Type' => 'test'
),
'föo_bär' => 'a',
"Bar" => array(1,2,3),
'a' => 'b',
);
$xml = $this->encoder->encode($obj, 'xml');
$this->assertEquals($expected, $this->encoder->decode($xml, 'xml'));
}
public function testPreventsComplexExternalEntities()
{
$oldCwd = getcwd();