Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Serializer] Add XmlEncoder::CDATA_WRAPPING context option #49893

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

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
1 change: 1 addition & 0 deletions src/Symfony/Component/Serializer/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ CHANGELOG
* Allow the `Groups` attribute/annotation on classes
* JsonDecode: Add `json_decode_detailed_errors` option
* Make `ProblemNormalizer` give details about Messenger's `ValidationFailedException`
* Add `XmlEncoder::CDATA_WRAPPING` context option

6.3
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,12 @@ public function withVersion(?string $version): static
{
return $this->with(XmlEncoder::VERSION, $version);
}

/**
* Configures whether to wrap strings within CDATA sections.
*/
public function withCdataWrapping(?bool $cdataWrapping): static
{
return $this->with(XmlEncoder::CDATA_WRAPPING, $cdataWrapping);
}
}
8 changes: 5 additions & 3 deletions src/Symfony/Component/Serializer/Encoder/XmlEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class XmlEncoder implements EncoderInterface, DecoderInterface, NormalizationAwa
public const STANDALONE = 'xml_standalone';
public const TYPE_CAST_ATTRIBUTES = 'xml_type_cast_attributes';
public const VERSION = 'xml_version';
public const CDATA_WRAPPING = 'cdata_wrapping';

private array $defaultContext = [
self::AS_COLLECTION => false,
Expand All @@ -68,6 +69,7 @@ class XmlEncoder implements EncoderInterface, DecoderInterface, NormalizationAwa
self::REMOVE_EMPTY_TAGS => false,
self::ROOT_NODE_NAME => 'response',
self::TYPE_CAST_ATTRIBUTES => true,
self::CDATA_WRAPPING => true,
];

public function __construct(array $defaultContext = [])
Expand Down Expand Up @@ -424,9 +426,9 @@ private function appendNode(\DOMNode $parentNode, mixed $data, string $format, a
/**
* Checks if a value contains any characters which would require CDATA wrapping.
*/
private function needsCdataWrapping(string $val): bool
private function needsCdataWrapping(string $val, array $context): bool
{
return preg_match('/[<>&]/', $val);
return ($context[self::CDATA_WRAPPING] ?? $this->defaultContext[self::CDATA_WRAPPING]) && preg_match('/[<>&]/', $val);
}

/**
Expand Down Expand Up @@ -454,7 +456,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)) {
} elseif (\is_string($val) && $this->needsCdataWrapping($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 @@ -29,8 +29,6 @@ protected function setUp(): void

/**
* @dataProvider withersDataProvider
*
* @param array<string, mixed> $values
*/
public function testWithers(array $values)
{
Expand All @@ -47,14 +45,12 @@ public function testWithers(array $values)
->withStandalone($values[XmlEncoder::STANDALONE])
->withTypeCastAttributes($values[XmlEncoder::TYPE_CAST_ATTRIBUTES])
->withVersion($values[XmlEncoder::VERSION])
->withCdataWrapping($values[XmlEncoder::CDATA_WRAPPING])
->toArray();

$this->assertSame($values, $context);
}

/**
* @return iterable<array{0: array<string, mixed>|}>
*/
public static function withersDataProvider(): iterable
{
yield 'With values' => [[
Expand All @@ -70,6 +66,7 @@ public static function withersDataProvider(): iterable
XmlEncoder::STANDALONE => false,
XmlEncoder::TYPE_CAST_ATTRIBUTES => true,
XmlEncoder::VERSION => '1.0',
XmlEncoder::CDATA_WRAPPING => false,
]];

yield 'With null values' => [[
Expand All @@ -85,6 +82,7 @@ public static function withersDataProvider(): iterable
XmlEncoder::STANDALONE => null,
XmlEncoder::TYPE_CAST_ATTRIBUTES => null,
XmlEncoder::VERSION => null,
XmlEncoder::CDATA_WRAPPING => null,
]];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -234,15 +234,39 @@ public function testEncodeRootAttributes()
public function testEncodeCdataWrapping()
{
$array = [
'firstname' => 'Paul <or Me>',
'firstname' => 'Paul & Martha <or Me>',
];

$expected = '<?xml version="1.0"?>'."\n".
'<response><firstname><![CDATA[Paul <or Me>]]></firstname></response>'."\n";
'<response><firstname><![CDATA[Paul & Martha <or Me>]]></firstname></response>'."\n";

$this->assertEquals($expected, $this->encoder->encode($array, 'xml'));
}

public function testEnableCdataWrapping()
{
$array = [
'firstname' => 'Paul & Martha <or Me>',
];

$expected = '<?xml version="1.0"?>'."\n".
'<response><firstname><![CDATA[Paul & Martha <or Me>]]></firstname></response>'."\n";

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

public function testDisableCdataWrapping()
{
$array = [
'firstname' => 'Paul & Martha <or Me>',
];

$expected = '<?xml version="1.0"?>'."\n".
'<response><firstname>Paul &amp; Martha &lt;or Me&gt;</firstname></response>'."\n";

$this->assertEquals($expected, $this->encoder->encode($array, 'xml', ['cdata_wrapping' => false]));
}

public function testEncodeScalarWithAttribute()
{
$array = [
Expand Down