Skip to content

Commit

Permalink
feature #28289 [Serializer] Add support for ignoring comments while X…
Browse files Browse the repository at this point in the history
…ML 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 #27926 which allowed to ignore XML processing instructions, this PR allows to ignore the XML comments while encoding.

Commits
-------

8f8230a Add support for ignoring comments while XML encoding
  • Loading branch information
fabpot committed Aug 30, 2018
2 parents 8651758 + 8f8230a commit 501212b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Symfony/Component/Serializer/Encoder/XmlEncoder.php
Expand Up @@ -378,7 +378,9 @@ private function buildXml(\DOMNode $parentNode, $data, string $xmlRootNodeName =
} 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)))) {
Expand Down
15 changes: 15 additions & 0 deletions src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php
Expand Up @@ -782,6 +782,21 @@ public function testEncodeWithoutPI()
$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
*/
Expand Down

0 comments on commit 501212b

Please sign in to comment.