E5E7 [Serializer] Add `CDATA_WRAPPING_NAME_PATTERN` support to `XmlEncoder` by Deltachaos · Pull Request #60355 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
Merged
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
[Serializer] Add CDATA_WRAPPING_NAME_PATTERN support to XmlEncoder
  • Loading branch information
Maximilian Ruta committed Jul 24, 2025
commit c4f363c2028c4361849ff6b37a76526220da4378
13 changes: 10 additions & 3 deletions 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 CDATA_WRAPPING_NAME_PATTERN = 'cdata_wrapping_name_pattern';
public const CDATA_WRAPPING_PATTERN = 'cdata_wrapping_pattern';
public const IGNORE_EMPTY_ATTRIBUTES = 'ignore_empty_attributes';

Expand All @@ -72,6 +73,7 @@ class XmlEncoder implements EncoderInterface, DecoderInterface, NormalizationAwa
self::ROOT_NODE_NAME => 'response',
self::TYPE_CAST_ATTRIBUTES => true,
self::CDATA_WRAPPING => true,
self::CDATA_WRAPPING_NAME_PATTERN => false,
self::CDATA_WRAPPING_PATTERN => '/[<>&]/',
self::IGNORE_EMPTY_ATTRIBUTES => false,
];
Expand Down Expand Up @@ -440,10 +442,15 @@ private function appendNode(\DOMNode $parentNode, mixed $data, string $format, a

/**
* Checks if a value contains any characters which would require CDATA wrapping.
*
* @param array<string, mixed> $context
*/
private function needsCdataWrapping(string $val, array $context): bool
private function needsCdataWrapping(string $name, string $val, array $context): bool
{
return ($context[self::CDATA_WRAPPING] ?? $this->defaultContext[self::CDATA_WRAPPING]) && preg_match($context[self::CDATA_WRAPPING_PATTERN] ?? $this->defaultContext[self::CDATA_WRAPPING_PATTERN], $val);
return ($context[self::CDATA_WRAPPING] ?? $this->defaultContext[self::CDATA_WRAPPING])
&& (preg_match($context[self::CDATA_WRAPPING_PATTERN] ?? $this->defaultContext[self::CDATA_WRAPPING_PATTERN], $val)
|| (($context[self::CDATA_WRAPPING_NAME_PATTERN] ?? $this->defaultContext[self::CDATA_WRAPPING_NAME_PATTERN]) && preg_match($context[self::CDATA_WRAPPING_NAME_PATTERN] ?? $this->defaultContext[self::CDATA_WRAPPING_NAME_PATTERN], $name))
);
}

/**
Expand Down Expand Up @@ -471,7 +478,7 @@ private function selectNodeType(\DOMNode $node, mixed $val, string $format, arra
return $this->selectNodeType($node, $this->serializer->normalize($val, $format, $context), $format, $context);
} elseif (is_numeric($val)) {
return $this->appendText($node, (string) $val);
} elseif (\is_string($val) && $this->needsCdataWrapping($val, $context)) {
} elseif (\is_string($val) && $this->needsCdataWrapping($node->nodeName, $val, $context)) {
return $this->appendCData($node, $val);
} elseif (\is_string($val)) {
return $this->appendText($node, $val);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,23 @@ public function testDecodeXMLWithProcessInstruction()
$this->assertEquals(get_object_vars($obj), $this->encoder->decode($source, 'xml'));
}

public function testCDataNamePattern()
{
$expected = <<<'XML'
<?xml version="1.0"?>
<response><person><firstname><![CDATA[Benjamin]]></firstname><lastname><![CDATA[Alexandre]]></lastname><other>data</other></person><person><firstname><![CDATA[Damien]]></firstname><lastname><![CDATA[Clay]]></lastname><other>data</other></person></response>

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

$this->assertEquals($expected, $this->encoder->encode($source, 'xml', [
XmlEncoder::CDATA_WRAPPING_NAME_PATTERN => '/(firstname|lastname)/',
]));
}

public function testDecodeIgnoreWhiteSpace()
{
$source = <<<'XML'
Expand Down
0