File tree 2 files changed +32
-4
lines changed
Filter options
src/Symfony/Component/Serializer
2 files changed +32
-4
lines changed
Original file line number Diff line number Diff line change @@ -37,17 +37,18 @@ class XmlEncoder implements EncoderInterface, DecoderInterface, NormalizationAwa
37
37
private $ context ;
38
38
private $ rootNodeName = 'response ' ;
39
39
private $ loadOptions ;
40
+ private $ alwaysCollection ;
40
41
41
42
/**
42
43
* Construct new XmlEncoder and allow to change the root node element name.
43
44
*
44
- * @param string $rootNodeName
45
- * @param int|null $loadOptions A bit field of LIBXML_* constants
45
+ * @param int|null $loadOptions A bit field of LIBXML_* constants
46
46
*/
47
- public function __construct (string $ rootNodeName = 'response ' , int $ loadOptions = null )
47
+ public function __construct (string $ rootNodeName = 'response ' , int $ loadOptions = null , bool $ alwaysCollection = false )
48
48
{
49
49
$ this ->rootNodeName = $ rootNodeName ;
50
50
$ this ->loadOptions = null !== $ loadOptions ? $ loadOptions : LIBXML_NONET | LIBXML_NOBLANKS ;
51
+ $ this ->alwaysCollection = $ alwaysCollection ;
51
52
}
52
53
53
54
/**
@@ -335,7 +336,7 @@ private function parseXmlValue(\DOMNode $node, array $context = array())
335
336
}
336
337
337
338
foreach ($ value as $ key => $ val ) {
338
- if (is_array ($ val ) && 1 === count ($ val )) {
339
+ if (is_array ($ val ) && 1 === count ($ val ) && (! $ this -> alwaysCollection || ! is_array ( $ val [ 0 ])) ) {
339
340
$ value [$ key ] = current ($ val );
340
341
}
341
342
}
Original file line number Diff line number Diff line change @@ -515,6 +515,33 @@ public function testDecodeIgnoreWhiteSpace()
515
515
$ this ->assertEquals ($ expected , $ this ->encoder ->decode ($ source , 'xml ' ));
516
516
}
517
517
518
+ public function testDecodeAlwaysAsCollection ()
519
+ {
520
+ $ this ->encoder = new XmlEncoder ('response ' , null , true );
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 ' ));
543
+ }
544
+
518
545
public function testDecodeWithoutItemHash ()
519
546
{
520
547
$ obj = new ScalarDummy ();
You can’t perform that action at this time.
0 commit comments