Allow to normalize \Traversable
This commit is contained in:
parent
36cb46a71a
commit
97c5d27c59
@ -305,7 +305,7 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
|
||||
{
|
||||
$append = true;
|
||||
|
||||
if (is_array($data) || $data instanceof \Traversable) {
|
||||
if (is_array($data) || ($data instanceof \Traversable && !$this->serializer->supportsNormalization($data, $this->format))) {
|
||||
foreach ($data as $key => $data) {
|
||||
//Ah this is the magic @ attribute types.
|
||||
if (0 === strpos($key, '@') && is_scalar($data) && $this->isElementNameValid($attributeName = substr($key, 1))) {
|
||||
|
@ -12,6 +12,7 @@
|
||||
namespace Symfony\Component\Serializer\Tests\Encoder;
|
||||
|
||||
use Symfony\Component\Serializer\Tests\Fixtures\Dummy;
|
||||
use Symfony\Component\Serializer\Tests\Fixtures\NormalizableTraversableDummy;
|
||||
use Symfony\Component\Serializer\Tests\Fixtures\ScalarDummy;
|
||||
use Symfony\Component\Serializer\Encoder\XmlEncoder;
|
||||
use Symfony\Component\Serializer\Serializer;
|
||||
@ -203,6 +204,21 @@ class XmlEncoderTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals($expected, $serializer->serialize($array, 'xml', $options));
|
||||
}
|
||||
|
||||
public function testEncodeTraversableWhenNormalizable() {
|
||||
$this->encoder = new XmlEncoder();
|
||||
$serializer = new Serializer(array(new CustomNormalizer()), array('xml' => new XmlEncoder()));
|
||||
$this->encoder->setSerializer($serializer);
|
||||
|
||||
$expected = <<<XML
|
||||
<?xml version="1.0"?>
|
||||
<response><foo>normalizedFoo</foo><bar>normalizedBar</bar></response>
|
||||
|
||||
XML;
|
||||
|
||||
$this->assertEquals($expected, $serializer->serialize(new NormalizableTraversableDummy(), 'xml'));
|
||||
|
||||
}
|
||||
|
||||
public function testDecode()
|
||||
{
|
||||
$source = $this->getXmlSource();
|
||||
|
Reference in New Issue
Block a user