diff --git a/src/Symfony/Component/Serializer/CHANGELOG.md b/src/Symfony/Component/Serializer/CHANGELOG.md index 4f456f3f82d1..3118834d8017 100644 --- a/src/Symfony/Component/Serializer/CHANGELOG.md +++ b/src/Symfony/Component/Serializer/CHANGELOG.md @@ -10,6 +10,7 @@ CHANGELOG * Add `AbstractNormalizer::FILTER_BOOL` context option * Add `CamelCaseToSnakeCaseNameConverter::REQUIRE_SNAKE_CASE_PROPERTIES` context option * Deprecate `AbstractNormalizerContextBuilder::withDefaultContructorArguments(?array $defaultContructorArguments)`, use `withDefaultConstructorArguments(?array $defaultConstructorArguments)` instead (note the missing `s` character in Contructor word in deprecated method) + * Add `XmlEncoder::CDATA_WRAPPING_PATTERN` context option 7.0 --- diff --git a/src/Symfony/Component/Serializer/Context/Encoder/XmlEncoderContextBuilder.php b/src/Symfony/Component/Serializer/Context/Encoder/XmlEncoderContextBuilder.php index 34cf78198ca4..0fd1f2f44c36 100644 --- a/src/Symfony/Component/Serializer/Context/Encoder/XmlEncoderContextBuilder.php +++ b/src/Symfony/Component/Serializer/Context/Encoder/XmlEncoderContextBuilder.php @@ -152,4 +152,12 @@ public function withCdataWrapping(?bool $cdataWrapping): static { return $this->with(XmlEncoder::CDATA_WRAPPING, $cdataWrapping); } + + /** + * Configures the pattern used to evaluate if a CDATA section should be added. + */ + public function withCdataWrappingPattern(?string $cdataWrappingPattern): static + { + return $this->with(XmlEncoder::CDATA_WRAPPING_PATTERN, $cdataWrappingPattern); + } } diff --git a/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php b/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php index ad4d87c207dd..5dcb2ba7e991 100644 --- a/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php +++ b/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php @@ -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_PATTERN = 'cdata_wrapping_pattern'; private array $defaultContext = [ self::AS_COLLECTION => false, @@ -70,6 +71,7 @@ class XmlEncoder implements EncoderInterface, DecoderInterface, NormalizationAwa self::ROOT_NODE_NAME => 'response', self::TYPE_CAST_ATTRIBUTES => true, self::CDATA_WRAPPING => true, + self::CDATA_WRAPPING_PATTERN => '/[<>&]/', ]; public function __construct(array $defaultContext = []) @@ -433,7 +435,7 @@ private function appendNode(\DOMNode $parentNode, mixed $data, string $format, a */ private function needsCdataWrapping(string $val, array $context): bool { - return ($context[self::CDATA_WRAPPING] ?? $this->defaultContext[self::CDATA_WRAPPING]) && preg_match('/[<>&]/', $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); } /** diff --git a/src/Symfony/Component/Serializer/Tests/Context/Encoder/XmlEncoderContextBuilderTest.php b/src/Symfony/Component/Serializer/Tests/Context/Encoder/XmlEncoderContextBuilderTest.php index d1ea307a9205..2f71c6012b22 100644 --- a/src/Symfony/Component/Serializer/Tests/Context/Encoder/XmlEncoderContextBuilderTest.php +++ b/src/Symfony/Component/Serializer/Tests/Context/Encoder/XmlEncoderContextBuilderTest.php @@ -46,6 +46,7 @@ public function testWithers(array $values) ->withTypeCastAttributes($values[XmlEncoder::TYPE_CAST_ATTRIBUTES]) ->withVersion($values[XmlEncoder::VERSION]) ->withCdataWrapping($values[XmlEncoder::CDATA_WRAPPING]) + ->withCdataWrappingPattern($values[XmlEncoder::CDATA_WRAPPING_PATTERN]) ->toArray(); $this->assertSame($values, $context); @@ -67,6 +68,7 @@ public static function withersDataProvider(): iterable XmlEncoder::TYPE_CAST_ATTRIBUTES => true, XmlEncoder::VERSION => '1.0', XmlEncoder::CDATA_WRAPPING => false, + XmlEncoder::CDATA_WRAPPING_PATTERN => '/[<>&"\']/', ]]; yield 'With null values' => [[ @@ -83,6 +85,7 @@ public static function withersDataProvider(): iterable XmlEncoder::TYPE_CAST_ATTRIBUTES => null, XmlEncoder::VERSION => null, XmlEncoder::CDATA_WRAPPING => null, + XmlEncoder::CDATA_WRAPPING_PATTERN => null, ]]; } } diff --git a/src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php b/src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php index f0ff1c5b9436..5be6be232dc6 100644 --- a/src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php +++ b/src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php @@ -231,16 +231,56 @@ public function testEncodeRootAttributes() $this->assertEquals($expected, $this->encoder->encode($array, 'xml')); } - public function testEncodeCdataWrapping() + /** + * @dataProvider encodeCdataWrappingWithDefaultPattern + */ + public function testEncodeCdataWrappingWithDefaultPattern($input, $expected) { - $array = [ - 'firstname' => 'Paul & Martha ', + $this->assertEquals($expected, $this->encoder->encode($input, 'xml')); + } + + public static function encodeCdataWrappingWithDefaultPattern() + { + return [ + [ + ['firstname' => 'Paul and Martha'], + ''."\n".'Paul and Martha'."\n", + ], + [ + ['lastname' => 'O\'Donnel'], + ''."\n".'O\'Donnel'."\n", + ], + [ + ['firstname' => 'Paul & Martha '], + ''."\n".']]>'."\n", + ], ]; + } - $expected = ''."\n". - ']]>'."\n"; + /** + * @dataProvider encodeCdataWrappingWithCustomPattern + */ + public function testEncodeCdataWrappingWithCustomPattern($input, $expected) + { + $this->assertEquals($expected, $this->encoder->encode($input, 'xml', ['cdata_wrapping_pattern' => '/[<>&"\']/'])); + } - $this->assertEquals($expected, $this->encoder->encode($array, 'xml')); + public static function encodeCdataWrappingWithCustomPattern() + { + return [ + [ + ['firstname' => 'Paul and Martha'], + ''."\n".'Paul and Martha'."\n", + ], + [ + ['lastname' => 'O\'Donnel'], + ''."\n".''."\n", + ], + [ + ['firstname' => 'Paul & Martha '], + ''."\n".']]>'."\n", + ], + ]; } public function testEnableCdataWrapping()