Skip to content

Commit 81e19e8

Browse files
committed
feature symfony#25369 [Serializer] add a context key to return always as collection for XmlEncoder (Simperfit)
This PR was merged into the 4.1-dev branch. Discussion ---------- [Serializer] add a context key to return always as collection for XmlEncoder | Q | A | ------------- | --- | Branch? | 4.1 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no <!-- don't forget to update UPGRADE-*.md files --> | Tests pass? | yes | Fixed tickets | symfony#25227 | License | MIT | Doc PR | This PR add a new `as_collection` context key in order to return always as a collection instead of returning a single elements when there are only one array. there are only one PR for the CsvEncoder don't wanted to have only One PR containing the two changes. It feel better to have two PR that fix the behaviour on two different things. it's easy to review and to revert if it break something (which should not since we are testing the behaviour). Commits ------- adb428d [Serializer] add a context key to return always as collection for XmlEncoder
2 parents 3303355 + adb428d commit 81e19e8

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ private function parseXmlValue(\DOMNode $node, array $context = array())
335335
}
336336

337337
foreach ($value as $key => $val) {
338-
if (\is_array($val) && 1 === \count($val)) {
338+
if (empty($context['as_collection']) && \is_array($val) && 1 === \count($val)) {
339339
$value[$key] = current($val);
340340
}
341341
}

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,33 @@ public function testDecodeIgnoreWhiteSpace()
515515
$this->assertEquals($expected, $this->encoder->decode($source, 'xml'));
516516
}
517517

518+
public function testDecodeAlwaysAsCollection()
519+
{
520+
$this->encoder = new XmlEncoder('response', null);
521+
$serializer = new Serializer(array(new CustomNormalizer()), array('xml' => new XmlEncoder()));
522+
$this->encoder->setSerializer($serializer);
523+
524+
$source = <<<'XML'
525+
<?xml version="1.0"?>
526+
<order_rows nodeType="order_row" virtualEntity="true">
527+
<order_row>
528+
<id><![CDATA[16]]></id>
529+
<test><![CDATA[16]]></test>
530+
</order_row>
531+
</order_rows>
532+
XML;
533+
$expected = array(
534+
'@nodeType' => 'order_row',
535+
'@virtualEntity' => 'true',
536+
'order_row' => array(array(
537+
'id' => array(16),
538+
'test' => array(16),
539+
)),
540+
);
541+
542+
$this->assertEquals($expected, $this->encoder->decode($source, 'xml', array('as_collection' => true)));
543+
}
544+
518545
public function testDecodeWithoutItemHash()
519546
{
520547
$obj = new ScalarDummy();

0 commit comments

Comments
 (0)