Skip to content

Commit 72bae95

Browse files
author
Maximilian Ruta
committed
Allow serialization of arrays to item elements
1 parent 01c26aa commit 72bae95

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class XmlEncoder implements EncoderInterface, DecoderInterface, NormalizationAwa
5959
public const TYPE_CAST_ATTRIBUTES = 'xml_type_cast_attributes';
6060
public const VERSION = 'xml_version';
6161
public const CDATA_WRAPPING = 'cdata_wrapping';
62+
public const ARRAY_AS_ITEM = 'array_as_item';
6263
public const CDATA_WRAPPING_PATTERN = 'cdata_wrapping_pattern';
6364
public const IGNORE_EMPTY_ATTRIBUTES = 'ignore_empty_attributes';
6465

@@ -71,6 +72,7 @@ class XmlEncoder implements EncoderInterface, DecoderInterface, NormalizationAwa
7172
self::REMOVE_EMPTY_TAGS => false,
7273
self::ROOT_NODE_NAME => 'response',
7374
self::TYPE_CAST_ATTRIBUTES => true,
75+
self::ARRAY_AS_ITEM => false,
7476
self::CDATA_WRAPPING => true,
7577
self::CDATA_WRAPPING_PATTERN => '/[<>&]/',
7678
self::IGNORE_EMPTY_ATTRIBUTES => false,
@@ -345,6 +347,7 @@ private function buildXml(\DOMNode $parentNode, mixed $data, string $format, arr
345347
{
346348
$append = true;
347349
$removeEmptyTags = $context[self::REMOVE_EMPTY_TAGS] ?? $this->defaultContext[self::REMOVE_EMPTY_TAGS] ?? false;
350+
$listAsChildren = $context[self::ARRAY_AS_ITEM] ?? $this->defaultContext[self::ARRAY_AS_ITEM] ?? false;
348351
$encoderIgnoredNodeTypes = $context[self::ENCODER_IGNORED_NODE_TYPES] ?? $this->defaultContext[self::ENCODER_IGNORED_NODE_TYPES];
349352

350353
if (\is_array($data) || ($data instanceof \Traversable && (null === $this->serializer || !$this->serializer->supportsNormalization($data, $format)))) {
@@ -373,7 +376,7 @@ private function buildXml(\DOMNode $parentNode, mixed $data, string $format, arr
373376
}
374377
} elseif (\is_array($data) && false === is_numeric($key)) {
375378
// Is this array fully numeric keys?
376-
if (ctype_digit(implode('', array_keys($data)))) {
379+
if (!$listAsChildren && ctype_digit(implode('', array_keys($data)))) {
377380
/*
378381
* Create nodes to append to $parentNode based on the $key of this array
379382
* Produces <xml><item>0</item><item>1</item></xml>

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,4 +1017,48 @@ public function testEncodeIgnoringEmptyAttribute()
10171017

10181018
$this->assertEquals($expected, $this->encoder->encode($data, 'xml', ['ignore_empty_attributes' => true]));
10191019
}
1020+
1021+
public function testEncodeArrayAsItem()
1022+
{
1023+
$expected = <<<'XML'
1024+
<?xml version="1.0"?>
1025+
<response><person><item key="0"><firstname>Benjamin</firstname><lastname>Alexandre</lastname></item><item key="1"><firstname>Damien</firstname><lastname>Clay</lastname></item></person></response>
1026+
1027+
XML;
1028+
$source = ['person' => [
1029+
['firstname' => 'Benjamin', 'lastname' => 'Alexandre'],
1030+
['firstname' => 'Damien', 'lastname' => 'Clay'],
1031+
]];
1032+
1033+
$this->assertEquals($expected, $this->encoder->encode($source, 'xml', [
1034+
XmlEncoder::ARRAY_AS_ITEM => true,
1035+
]));
1036+
}
1037+
1038+
public function testDecodeArrayAsItem()
1039+
{
1040+
$source = <<<'XML'
1041+
<?xml version="1.0"?>
1042+
<response>
1043+
<person>
1044+
<item key="0">
1045+
<firstname>Benjamin</firstname>
1046+
<lastname>Alexandre</lastname>
1047+
</item>
1048+
<item key="1">
1049+
<firstname>Damien</firstname>
1050+
<lastname>Clay</lastname>
1051+
</item>
1052+
</person>
1053+
</response>
1054+
XML;
1055+
$expected = ['person' => [
1056+
['firstname' => 'Benjamin', 'lastname' => 'Alexandre', '@key' => 0],
1057+
['firstname' => 'Damien', 'lastname' => 'Clay', '@key' => 1],
1058+
]];
1059+
1060+
$this->assertEquals($expected, $this->encoder->decode($source, 'xml', [
1061+
XmlEncoder::ARRAY_AS_ITEM => true,
1062+
]));
1063+
}
10201064
}

0 commit comments

Comments
 (0)