diff --git a/src/Symfony/Component/Serializer/CHANGELOG.md b/src/Symfony/Component/Serializer/CHANGELOG.md index 3342ada2fea86..d7799bb50236f 100644 --- a/src/Symfony/Component/Serializer/CHANGELOG.md +++ b/src/Symfony/Component/Serializer/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +6.3 +--- + + * Add `XmlEncoder::SAVE_OPTIONS` context option + 6.2 --- diff --git a/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php b/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php index 0132f1ebf3c83..2207012ca6225 100644 --- a/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php +++ b/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php @@ -44,9 +44,15 @@ class XmlEncoder implements EncoderInterface, DecoderInterface, NormalizationAwa public const FORMAT_OUTPUT = 'xml_format_output'; /** - * A bit field of LIBXML_* constants. + * A bit field of LIBXML_* constants for loading XML documents. */ public const LOAD_OPTIONS = 'load_options'; + + /** + * A bit field of LIBXML_* constants for saving XML documents. + */ + public const SAVE_OPTIONS = 'save_options'; + public const REMOVE_EMPTY_TAGS = 'remove_empty_tags'; public const ROOT_NODE_NAME = 'xml_root_node_name'; public const STANDALONE = 'xml_standalone'; @@ -58,6 +64,7 @@ class XmlEncoder implements EncoderInterface, DecoderInterface, NormalizationAwa self::DECODER_IGNORED_NODE_TYPES => [\XML_PI_NODE, \XML_COMMENT_NODE], self::ENCODER_IGNORED_NODE_TYPES => [], self::LOAD_OPTIONS => \LIBXML_NONET | \LIBXML_NOBLANKS, + self::SAVE_OPTIONS => 0, self::REMOVE_EMPTY_TAGS => false, self::ROOT_NODE_NAME => 'response', self::TYPE_CAST_ATTRIBUTES => true, @@ -88,7 +95,7 @@ public function encode(mixed $data, string $format, array $context = []): string $this->appendNode($dom, $data, $format, $context, $xmlRootNodeName); } - return $dom->saveXML($ignorePiNode ? $dom->documentElement : null); + return $dom->saveXML($ignorePiNode ? $dom->documentElement : null, $context[self::SAVE_OPTIONS] ?? $this->defaultContext[self::SAVE_OPTIONS]); } public function decode(string $data, string $format, array $context = []): mixed diff --git a/src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php b/src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php index 77b220d585346..11e7bef3e5f17 100644 --- a/src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php +++ b/src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php @@ -189,12 +189,13 @@ public function testEncodeNotRemovingEmptyTags() public function testContext() { - $array = ['person' => ['name' => 'George Abitbol']]; + $array = ['person' => ['name' => 'George Abitbol', 'age' => null]]; $expected = <<<'XML' George Abitbol + @@ -202,6 +203,7 @@ public function testContext() $context = [ 'xml_format_output' => true, + 'save_options' => \LIBXML_NOEMPTYTAG, ]; $this->assertSame($expected, $this->encoder->encode($array, 'xml', $context));