Skip to content

Commit a087156

Browse files
committed
feature #48164 [Serializer] Add encoder option for saving options (ihmels)
This PR was merged into the 6.3 branch. Discussion ---------- [Serializer] Add encoder option for saving options | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | n/a | License | MIT | Doc PR | symfony/symfony-docs#17418 This changes add a new `SAVE_OPTIONS` to `XmlEncoder`. This new option is the counterpart to the `LOAD_OPTIONS`. Commits ------- f95f1f9770 Add encoder option for saving options
2 parents e3c6f21 + f5a54dd commit a087156

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.3
5+
---
6+
7+
* Add `XmlEncoder::SAVE_OPTIONS` context option
8+
49
6.2
510
---
611

Encoder/XmlEncoder.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,15 @@ class XmlEncoder implements EncoderInterface, DecoderInterface, NormalizationAwa
4444
public const FORMAT_OUTPUT = 'xml_format_output';
4545

4646
/**
47-
* A bit field of LIBXML_* constants.
47+
* A bit field of LIBXML_* constants for loading XML documents.
4848
*/
4949
public const LOAD_OPTIONS = 'load_options';
50+
51+
/**
52+
* A bit field of LIBXML_* constants for saving XML documents.
53+
*/
54+
public const SAVE_OPTIONS = 'save_options';
55+
5056
public const REMOVE_EMPTY_TAGS = 'remove_empty_tags';
5157
public const ROOT_NODE_NAME = 'xml_root_node_name';
5258
public const STANDALONE = 'xml_standalone';
@@ -58,6 +64,7 @@ class XmlEncoder implements EncoderInterface, DecoderInterface, NormalizationAwa
5864
self::DECODER_IGNORED_NODE_TYPES => [\XML_PI_NODE, \XML_COMMENT_NODE],
5965
self::ENCODER_IGNORED_NODE_TYPES => [],
6066
self::LOAD_OPTIONS => \LIBXML_NONET | \LIBXML_NOBLANKS,
67+
self::SAVE_OPTIONS => 0,
6168
self::REMOVE_EMPTY_TAGS => false,
6269
self::ROOT_NODE_NAME => 'response',
6370
self::TYPE_CAST_ATTRIBUTES => true,
@@ -88,7 +95,7 @@ public function encode(mixed $data, string $format, array $context = []): string
8895
$this->appendNode($dom, $data, $format, $context, $xmlRootNodeName);
8996
}
9097

91-
return $dom->saveXML($ignorePiNode ? $dom->documentElement : null);
98+
return $dom->saveXML($ignorePiNode ? $dom->documentElement : null, $context[self::SAVE_OPTIONS] ?? $this->defaultContext[self::SAVE_OPTIONS]);
9299
}
93100

94101
public function decode(string $data, string $format, array $context = []): mixed

Tests/Encoder/XmlEncoderTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,19 +189,21 @@ public function testEncodeNotRemovingEmptyTags()
189189

190190
public function testContext()
191191
{
192-
$array = ['person' => ['name' => 'George Abitbol']];
192+
$array = ['person' => ['name' => 'George Abitbol', 'age' => null]];
193193
$expected = <<<'XML'
194194
<?xml version="1.0"?>
195195
<response>
196196
<person>
197197
<name>George Abitbol</name>
198+
<age></age>
198199
</person>
199200
</response>
200201

201202
XML;
202203

203204
$context = [
204205
'xml_format_output' => true,
206+
'save_options' => \LIBXML_NOEMPTYTAG,
205207
];
206208

207209
$this->assertSame($expected, $this->encoder->encode($array, 'xml', $context));

0 commit comments

Comments
 (0)