Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit658fb0e

Browse files
author
Amrouche Hamza
committed
[Serializer] add a contruct key to return always as collection for XmlEncoder
1 parent3ce9c29 commit658fb0e

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,18 @@ class XmlEncoder implements EncoderInterface, DecoderInterface, NormalizationAwa
3737
private$context;
3838
private$rootNodeName ='response';
3939
private$loadOptions;
40+
private$alwaysCollection;
4041

4142
/**
4243
* Construct new XmlEncoder and allow to change the root node element name.
4344
*
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
4646
*/
47-
publicfunction__construct(string$rootNodeName ='response',int$loadOptions =null)
47+
publicfunction__construct(string$rootNodeName ='response',int$loadOptions =null,bool$alwaysCollection =false)
4848
{
4949
$this->rootNodeName =$rootNodeName;
5050
$this->loadOptions =null !==$loadOptions ?$loadOptions :LIBXML_NONET |LIBXML_NOBLANKS;
51+
$this->alwaysCollection =$alwaysCollection;
5152
}
5253

5354
/**
@@ -335,7 +336,7 @@ private function parseXmlValue(\DOMNode $node, array $context = array())
335336
}
336337

337338
foreach ($valueas$key =>$val) {
338-
if (is_array($val) &&1 ===count($val)) {
339+
if (is_array($val) &&1 ===count($val) && (!$this->alwaysCollection || !is_array($val[0]))) {
339340
$value[$key] =current($val);
340341
}
341342
}

‎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+
publicfunctiontestDecodeAlwaysAsCollection()
519+
{
520+
$this->encoder =newXmlEncoder('response',null,true);
521+
$serializer =newSerializer(array(newCustomNormalizer()),array('xml' =>newXmlEncoder()));
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+
518545
publicfunctiontestDecodeWithoutItemHash()
519546
{
520547
$obj =newScalarDummy();

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp