Add support for ignoring comments while XML encoding

This commit is contained in:
Dany Maillard 2018-08-28 00:11:30 +02:00
parent da0ef24744
commit 8f8230ac3a
2 changed files with 18 additions and 1 deletions

View File

@ -378,7 +378,9 @@ class XmlEncoder implements EncoderInterface, DecoderInterface, NormalizationAwa
} elseif ('#' === $key) {
$append = $this->selectNodeType($parentNode, $data);
} elseif ('#comment' === $key) {
$append = $this->appendComment($parentNode, $data);
if (!\in_array(XML_COMMENT_NODE, $this->encoderIgnoredNodeTypes, true)) {
$append = $this->appendComment($parentNode, $data);
}
} elseif (\is_array($data) && false === is_numeric($key)) {
// Is this array fully numeric keys?
if (ctype_digit(implode('', array_keys($data)))) {

View File

@ -782,6 +782,21 @@ XML;
$this->assertEquals($expected, $encoder->encode(array(), 'xml'));
}
public function testEncodeWithoutComment()
{
$encoder = new XmlEncoder('response', null, array(), array(XML_COMMENT_NODE));
$expected = <<<'XML'
<?xml version="1.0"?>
<response/>
XML;
$data = array('#comment' => ' foo ');
$this->assertEquals($expected, $encoder->encode($data, 'xml'));
}
/**
* @return XmlEncoder
*/