bug #17984 Allow to normalize \Traversable when serializing xml (Ener-Getick)
This PR was merged into the 2.3 branch.
Discussion
----------
Allow to normalize \Traversable when serializing xml
| Q | A
| ------------- | ---
| Bug fix? | yes
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | no
| Fixed tickets |
| License | MIT
It's impossible to normalize an object implementing ``\Traversable`` when using the ``XMLEncoder``. For example we can't customize the serializer output when serializing a ``FormInterface`` instance.
So my proposition is to fix this by using the default XML encoder output only when the serializer can't normalize the data.
Commits
-------
97c5d27
Allow to normalize \Traversable
This commit is contained in:
commit
9a5ea71c22
@ -305,7 +305,7 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
|
|||||||
{
|
{
|
||||||
$append = true;
|
$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) {
|
foreach ($data as $key => $data) {
|
||||||
//Ah this is the magic @ attribute types.
|
//Ah this is the magic @ attribute types.
|
||||||
if (0 === strpos($key, '@') && is_scalar($data) && $this->isElementNameValid($attributeName = substr($key, 1))) {
|
if (0 === strpos($key, '@') && is_scalar($data) && $this->isElementNameValid($attributeName = substr($key, 1))) {
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
namespace Symfony\Component\Serializer\Tests\Encoder;
|
namespace Symfony\Component\Serializer\Tests\Encoder;
|
||||||
|
|
||||||
use Symfony\Component\Serializer\Tests\Fixtures\Dummy;
|
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\Tests\Fixtures\ScalarDummy;
|
||||||
use Symfony\Component\Serializer\Encoder\XmlEncoder;
|
use Symfony\Component\Serializer\Encoder\XmlEncoder;
|
||||||
use Symfony\Component\Serializer\Serializer;
|
use Symfony\Component\Serializer\Serializer;
|
||||||
@ -203,6 +204,21 @@ class XmlEncoderTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals($expected, $serializer->serialize($array, 'xml', $options));
|
$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()
|
public function testDecode()
|
||||||
{
|
{
|
||||||
$source = $this->getXmlSource();
|
$source = $this->getXmlSource();
|
||||||
|
Reference in New Issue
Block a user