8000 feature #13031 [Serializer] Add xml_format_output context option. Clo… · symfony/symfony@397dfb2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 397dfb2

Browse files
committed
feature #13031 [Serializer] Add xml_format_output context option. Close #12517. (dunglas)
This PR was merged into the 2.7 branch. Discussion ---------- [Serializer] Add xml_format_output context option. Close #12517. | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #12517 | License | MIT | Doc PR | n/a Allows to pretty print XML encoder output. Commits ------- 37d3f84 [Serializer] Add xml_format_output context option. Close #12517.
2 parents ff1b685 + 37d3f84 commit 397dfb2

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/Symfony/Component/Serializer/Encoder/XmlEncoder.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* @author Jordi Boggiano <j.boggiano@seld.be>
2020
* @author John Wards <jwards@whiteoctober.co.uk>
2121
* @author Fabian Vogler <fabian@equivalence.ch>
22+
* @author Kévin Dunglas <dunglas@gmail.com>
2223
*/
2324
class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, DecoderInterface, NormalizationAwareInterface
2425
{
@@ -514,6 +515,8 @@ private function createDomDocument(array $context)
514515

515516
// Set an attribute on the DOM document specifying, as part of the XML declaration,
516517
$xmlOptions = array(
518+
// nicely formats output with indentation and extra space
519+
'xml_format_output' => 'formatOutput',
517520
// the version number of the document
518521
'xml_version' => 'xmlVersion',
519522
// the encoding of the document

src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,26 @@ public function testEncodeXmlAttributes()
137137
$this->assertSame($expected, $this->encoder->encode($array, 'xml', $context));
138138
}
139139

140+
public function testContext()
141+
{
142+
$array = array('person' => array('name' => 'George Abitbol'));
143+
$expected = <<<XML
144+
<?xml version="1.0"?>
145+
<response>
146+
<person>
147+
<name>George Abitbol</name>
148+
</person>
149+
</response>
150+
151+
XML;
152+
153+
$context = array(
154+
'xml_format_output' => true,
155+
);
156+
157+
$this->assertSame($expected, $this->encoder->encode($array, 'xml', $context));
158+
}
159+
140160
public function testEncodeScalarRootAttributes()
141161
{
142162
$array = array(

0 commit comments

Comments
 (0)
0