Skip to content

[Serializer] Add Support for Encoding Arrays as Child <item> Elements in XML Encoder #60228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 7.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Symfony/Component/Serializer/Encoder/XmlEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class XmlEncoder implements EncoderInterface, DecoderInterface, NormalizationAwa
public const TYPE_CAST_ATTRIBUTES = 'xml_type_cast_attributes';
public const VERSION = 'xml_version';
public const CDATA_WRAPPING = 'cdata_wrapping';
public const ARRAY_AS_ITEM = 'array_as_item';
public const CDATA_WRAPPING_PATTERN = 'cdata_wrapping_pattern';
public const IGNORE_EMPTY_ATTRIBUTES = 'ignore_empty_attributes';

Expand All @@ -71,6 +72,7 @@ class XmlEncoder implements EncoderInterface, DecoderInterface, NormalizationAwa
self::REMOVE_EMPTY_TAGS => false,
self::ROOT_NODE_NAME => 'response',
self::TYPE_CAST_ATTRIBUTES => true,
self::ARRAY_AS_ITEM => false,
self::CDATA_WRAPPING => true,
self::CDATA_WRAPPING_PATTERN => '/[<>&]/',
self::IGNORE_EMPTY_ATTRIBUTES => false,
Expand Down Expand Up @@ -345,6 +347,7 @@ private function buildXml(\DOMNode $parentNode, mixed $data, string $format, arr
{
$append = true;
$removeEmptyTags = $context[self::REMOVE_EMPTY_TAGS] ?? $this->defaultContext[self::REMOVE_EMPTY_TAGS] ?? false;
$arrayAsItem = $context[self::ARRAY_AS_ITEM] ?? $this->defaultContext[self::ARRAY_AS_ITEM] ?? false;
$encoderIgnoredNodeTypes = $context[self::ENCODER_IGNORED_NODE_TYPES] ?? $this->defaultContext[self::ENCODER_IGNORED_NODE_TYPES];

if (\is_array($data) || ($data instanceof \Traversable && (null === $this->serializer || !$this->serializer->supportsNormalization($data, $format)))) {
Expand Down Expand Up @@ -373,7 +376,7 @@ private function buildXml(\DOMNode $parentNode, mixed $data, string $format, arr
}
} elseif (\is_array($data) && false === is_numeric($key)) {
// Is this array fully numeric keys?
if (ctype_digit(implode('', array_keys($data)))) {
if (!$arrayAsItem && ctype_digit(implode('', array_keys($data)))) {
/*
* Create nodes to append to $parentNode based on the $key of this array
* Produces <xml><item>0</item><item>1</item></xml>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1017,4 +1017,48 @@ public function testEncodeIgnoringEmptyAttribute()

$this->assertEquals($expected, $this->encoder->encode($data, 'xml', ['ignore_empty_attributes' => true]));
}

public function testEncodeArrayAsItem()
{
$expected = <<<'XML'
<?xml version="1.0"?>
<response><person><item key="0"><firstname>Benjamin</firstname><lastname>Alexandre</lastname></item><item key="1"><firstname>Damien</firstname><lastname>Clay</lastname></item></person></response>

XML;
$source = ['person' => [
['firstname' => 'Benjamin', 'lastname' => 'Alexandre'],
['firstname' => 'Damien', 'lastname' => 'Clay'],
]];

$this->assertEquals($expected, $this->encoder->encode($source, 'xml', [
XmlEncoder::ARRAY_AS_ITEM => true,
]));
}

public function testDecodeArrayAsItem()
{
$source = <<<'XML'
<?xml version="1.0"?>
<response>
<person>
<item key="0">
<firstname>Benjamin</firstname>
<lastname>Alexandre</lastname>
</item>
<item key="1">
<firstname>Damien</firstname>
<lastname>Clay</lastname>
</item>
</person>
</response>
XML;
$expected = ['person' => [
['firstname' => 'Benjamin', 'lastname' => 'Alexandre', '@key' => 0],
['firstname' => 'Damien', 'lastname' => 'Clay', '@key' => 1],
]];

$this->assertEquals($expected, $this->encoder->decode($source, 'xml', [
XmlEncoder::ARRAY_AS_ITEM => true,
]));
}
}
Loading