feature #28289 [Serializer] Add support for ignoring comments while XML encoding (maidmaid)

This PR was merged into the 4.2-dev branch.

Discussion
----------

[Serializer] Add support for ignoring comments while XML encoding

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | /
| License       | MIT
| Doc PR        | /

In addition to https://github.com/symfony/symfony/pull/27926 which allowed to ignore XML processing instructions, this PR allows to ignore the XML comments while encoding.

Commits
-------

8f8230ac3a Add support for ignoring comments while XML encoding
This commit is contained in:
Fabien Potencier 2018-08-30 18:33:29 +02:00
commit 501212b934
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
*/