Skip to content

Commit 1b3f4a7

Browse files
committed
[Serializer] Add xml_format_output context option. Close #12517.
1 parent da70acf commit 1b3f4a7

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

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 <[email protected]>
2020
* @author John Wards <[email protected]>
2121
* @author Fabian Vogler <[email protected]>
22+
* @author Kévin Dunglas <[email protected]>
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

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)