Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9ef362e

Browse filesBrowse files
committed
feature #27926 [Serializer] XmlEncoder doesn't ignore PI nodes while encoding (maidmaid)
This PR was merged into the 4.2-dev branch. Discussion ---------- [Serializer] XmlEncoder doesn't ignore PI nodes while encoding | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | no | Fixed tickets | / | License | MIT | Doc PR | / It's sometimes important to get only the XML body without the processing instructions (like the `<?xml version="1.0" ?>` on the top of your XML doc). At the moment, it's possible to ignore them while decoding, but not while encoding. ```php $encoder = new XmlEncoder('response', null, $ignoredNodeTypes = [XML_PI_NODE]); echo $encoder->encode([], 'xml'); ``` ``` Expected: <response/> Actual: <?xml version="1.0"?> <response/> ``` So, a new `$encoderIgnoredNodeTypes` arg is added to the constructor which will be: ```php public function __construct(string $rootNodeName = 'response', int $loadOptions = null, array $decoderIgnoredNodeTypes = array(XML_PI_NODE, XML_COMMENT_NODE), array $encoderIgnoredNodeTypes = array()) ``` Commits ------- 2223fcc Allow to ignore PI while encoding
2 parents 4e4b216 + 2223fcc commit 9ef362e
Copy full SHA for 9ef362e

File tree

Expand file treeCollapse file tree

2 files changed

+21
-9
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+21
-9
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Encoder/XmlEncoder.php
+12-9Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,22 @@ class XmlEncoder implements EncoderInterface, DecoderInterface, NormalizationAwa
3838
private $context;
3939
private $rootNodeName = 'response';
4040
private $loadOptions;
41-
private $ignoredNodeTypes;
41+
private $decoderIgnoredNodeTypes;
42+
private $encoderIgnoredNodeTypes;
4243

4344
/**
4445
* Construct new XmlEncoder and allow to change the root node element name.
4546
*
46-
* @param int|null $loadOptions A bit field of LIBXML_* constants
47-
* @param int[] $ignoredNodeTypes an array of ignored XML node types, each one of the DOM Predefined XML_* Constants
47+
* @param int|null $loadOptions A bit field of LIBXML_* constants
48+
* @param int[] $decoderIgnoredNodeTypes an array of ignored XML node types while decoding, each one of the DOM Predefined XML_* Constants
49+
* @param int[] $encoderIgnoredNodeTypes an array of ignored XML node types while encoding, each one of the DOM Predefined XML_* Constants
4850
*/
49-
public function __construct(string $rootNodeName = 'response', int $loadOptions = null, array $ignoredNodeTypes = array(XML_PI_NODE, XML_COMMENT_NODE))
51+
public function __construct(string $rootNodeName = 'response', int $loadOptions = null, array $decoderIgnoredNodeTypes = array(XML_PI_NODE, XML_COMMENT_NODE), array $encoderIgnoredNodeTypes = array())
5052
{
5153
$this->rootNodeName = $rootNodeName;
5254
$this->loadOptions = null !== $loadOptions ? $loadOptions : LIBXML_NONET | LIBXML_NOBLANKS;
53-
$this->ignoredNodeTypes = $ignoredNodeTypes;
55+
$this->decoderIgnoredNodeTypes = $decoderIgnoredNodeTypes;
56+
$this->encoderIgnoredNodeTypes = $encoderIgnoredNodeTypes;
5457
}
5558

5659
/**
@@ -59,7 +62,7 @@ public function __construct(string $rootNodeName = 'response', int $loadOptions
5962
public function encode($data, $format, array $context = array())
6063
{
6164
if ($data instanceof \DOMDocument) {
62-
return $data->saveXML();
65+
return $data->saveXML(\in_array(XML_PI_NODE, $this->encoderIgnoredNodeTypes, true) ? $data->documentElement : null);
6366
}
6467

6568
$xmlRootNodeName = $this->resolveXmlRootName($context);
@@ -76,7 +79,7 @@ public function encode($data, $format, array $context = array())
7679
$this->appendNode($this->dom, $data, $xmlRootNodeName);
7780
}
7881

79-
return $this->dom->saveXML();
82+
return $this->dom->saveXML(\in_array(XML_PI_NODE, $this->encoderIgnoredNodeTypes, true) ? $this->dom->documentElement : null);
8083
}
8184

8285
/**
@@ -109,7 +112,7 @@ public function decode($data, $format, array $context = array())
109112
if (XML_DOCUMENT_TYPE_NODE === $child->nodeType) {
110113
throw new NotEncodableValueException('Document types are not allowed.');
111114
}
112-
if (!$rootNode && !\in_array($child->nodeType, $this->ignoredNodeTypes, true)) {
115+
if (!$rootNode && !\in_array($child->nodeType, $this->decoderIgnoredNodeTypes, true)) {
113116
$rootNode = $child;
114117
}
115118
}
@@ -327,7 +330,7 @@ private function parseXmlValue(\DOMNode $node, array $context = array())
327330
$value = array();
328331

329332
foreach ($node->childNodes as $subnode) {
330-
if (\in_array($subnode->nodeType, $this->ignoredNodeTypes, true)) {
333+
if (\in_array($subnode->nodeType, $this->decoderIgnoredNodeTypes, true)) {
331334
continue;
332335
}
333336

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,15 @@ public function testEncodeComment()
773773
$this->assertEquals($expected, $this->encoder->encode($data, 'xml'));
774774
}
775775

776+
public function testEncodeWithoutPI()
777+
{
778+
$encoder = new XmlEncoder('response', null, array(), array(XML_PI_NODE));
779+
780+
$expected = '<response/>';
781+
782+
$this->assertEquals($expected, $encoder->encode(array(), 'xml'));
783+
}
784+
776785
/**
777786
* @return XmlEncoder
778787
*/

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.