Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 7c8709e

Browse filesBrowse files
author
Amrouche Hamza
committed
[Serializer] add a context key to return always as collection for XmlEncoder
1 parent 1df45e4 commit 7c8709e
Copy full SHA for 7c8709e

File tree

2 files changed

+29
-3
lines changed
Filter options

2 files changed

+29
-3
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Encoder/XmlEncoder.php
+2-3Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ class XmlEncoder implements EncoderInterface, DecoderInterface, NormalizationAwa
4141
/**
4242
* Construct new XmlEncoder and allow to change the root node element name.
4343
*
44-
* @param string $rootNodeName
45-
* @param int|null $loadOptions A bit field of LIBXML_* constants
44+
* @param int|null $loadOptions A bit field of LIBXML_* constants
4645
*/
4746
public function __construct(string $rootNodeName = 'response', int $loadOptions = null)
4847
{
@@ -335,7 +334,7 @@ private function parseXmlValue(\DOMNode $node, array $context = array())
335334
}
336335

337336
foreach ($value as $key => $val) {
338-
if (is_array($val) && 1 === count($val)) {
337+
if (is_array($val) && 1 === count($val) && (!(isset($context['as_collection']) && $context['as_collection']) || !is_array($val[0]))) {
339338
$value[$key] = current($val);
340339
}
341340
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php
+27Lines 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' => 16,
538+
'test' => 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.