8000 Allow to ignore PI while encoding · symfony/symfony@9c182a2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9c182a2

Browse files
committed
Allow to ignore PI while encoding
1 parent df26fea commit 9c182a2

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,22 @@ class XmlEncoder implements EncoderInterface, DecoderInterface, NormalizationAwa
3737
private $context;
3838
private $rootNodeName = 'response';
3939
private $loadOptions;
40-
private $ignoredNodeTypes;
40+
private $decoderIgnoredNodeTypes;
41+
private $encoderIgnoredNodeTypes;
4142

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

5558
/**
@@ -58,7 +61,7 @@ public function __construct(string $rootNodeName = 'response', int $loadOptions
5861
public function encode($data, $format, array $context = array())
5962
{
6063
if ($data instanceof \DOMDocument) {
61-
return $data->saveXML();
64+
return $data->saveXML(in_array(XML_PI_NODE, $this->encoderIgnoredNodeTypes, true) ? $data->documentElement : null);
6265
}
6366

6467
$xmlRootNodeName = $this->resolveXmlRootName($context);
@@ -75,7 +78,7 @@ public function encode($data, $format, array $context = array())
7578
$this->appendNode($this->dom, $data, $xmlRootNodeName);
7679
}
7780

78-
return $this->dom->saveXML();
81+
return $this->dom->saveXML(in_array(XML_PI_NODE, $this->encoderIgnoredNodeTypes, true) ? $this->dom->documentElement : null);
7982
}
8083

8184
/**
@@ -108,7 +111,7 @@ public function decode($data, $format, array $context = array())
108111
if (XML_DOCUMENT_TYPE_NODE === $child->nodeType) {
109112
throw new NotEncodableValueException('Document types are not allowed.');
110113
}
111-
if (!$rootNode && !\in_array($child->nodeType, $this->ignoredNodeTypes, true)) {
114+
if (!$rootNode && !\in_array($child->nodeType, $this->decoderIgnoredNodeTypes, true)) {
112115
$rootNode = $child;
113116
}
114117
}
@@ -319,7 +322,7 @@ private function parseXmlValue(\DOMNode $node, array $context = array())
319322
$value = array();
320323

321324
foreach ($node->childNodes as $subnode) {
322-
if (\in_array($subnode->nodeType, $this->ignoredNodeTypes, true)) {
325+
if (\in_array($subnode->nodeType, $this->decoderIgnoredNodeTypes, true)) {
323326
continue;
324327
}
325328

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,15 @@ public function testEncodeXmlWithDateTimeObjectField()
760760
$this->assertEquals($this->createXmlWithDateTimeField(), $actualXml);
761761
}
762762

763+
public function testEncodeWithoutPI()
764+
{
765+
$encoder = new XmlEncoder('response', null, array(), array(XML_PI_NODE));
766+
767+
$expected = '<response/>';
768+
769+
$this->assertEquals($expected, $encoder->encode(array(), 'xml'));
770+
}
771+
763772
/**
764773
* @return XmlEncoder
765774
*/

0 commit comments

Comments
 (0)
0