Skip to content

Commit fbc9082

Browse files
committed
merged branch StanAngeloff/issue-8424-refactoring (PR #8514)
This PR was merged into the master branch. Discussion ---------- [Serializer] Added XML attributes support in XmlEncoder This is a rebase and refactoring of #8424. --- | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #8424 | License | MIT | Doc PR | - --- | New Code | Result | --- | --- | Code coverage | 100% | PSR-2 | No violations | PHP-CS-Fixer | No changes --- ### TODO - [ ] **Q**: I looked through `symfony-docs` for any mention of `xml_root_node_name` which is already implemented, but failed to find any. How to best document those new additions? Commits ------- 21218cc [Serializer] Added XML attributes support for DomDocument in XmlEncoder.
2 parents 6d68740 + 21218cc commit fbc9082

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function encode($data, $format, array $context = array())
4747

4848
$xmlRootNodeName = $this->resolveXmlRootName($context);
4949

50-
$this->dom = new \DOMDocument();
50+
$this->dom = $this->createDomDocument($context);
5151
$this->format = $format;
5252

5353
if (null !== $data && !is_scalar($data)) {
@@ -426,4 +426,32 @@ private function resolveXmlRootName(array $context = array())
426426
: $this->rootNodeName;
427427
}
428428

429+
/**
430+
* Create a DOM document, taking serializer options into account.
431+
*
432+
* @param array $context options that the encoder has access to.
433+
*
434+
* @return \DOMDocument
435+
*/
436+
private function createDomDocument(array $context)
437+
{
438+
$document = new \DOMDocument();
439+
440+
// Set an attribute on the DOM document specifying, as part of the XML declaration,
441+
$xmlOptions = array(
442+
// the version number of the document
443+
'xml_version' => 'xmlVersion',
444+
// the encoding of the document
445+
'xml_encoding' => 'encoding',
446+
// whether the document is standalone
447+
'xml_standalone' => 'xmlStandalone',
448+
);
449+
foreach ($xmlOptions as $xmlOption => $documentProperty) {
450+
if (isset($context[$xmlOption])) {
451+
$document->$documentProperty = $context[$xmlOption];
452+
}
453+
}
454+
455+
return $document;
456+
}
429457
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,23 @@ public function testEncodeSimpleXML()
118118
$this->assertEquals($expected, $this->encoder->encode($array, 'xml'));
119119
}
120120

121+
public function testEncodeXmlAttributes()
122+
{
123+
$xml = simplexml_load_string('<firstname>Peter</firstname>');
124+
$array = array('person' => $xml);
125+
126+
$expected = '<?xml version="1.1" encoding="utf-8" standalone="yes"?>'."\n".
127+
'<response><person><firstname>Peter</firstname></person></response>'."\n";
128+
129+
$context = array(
130+
'xml_version' => '1.1',
131+
'xml_encoding' => 'utf-8',
132+
'xml_standalone' => true,
133+
);
134+
135+
$this->assertSame($expected, $this->encoder->encode($array, 'xml', $context));
136+
}
137+
121138
public function testEncodeScalarRootAttributes()
122139
{
123140
$array = array(

0 commit comments

Comments
 (0)