Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 501212b

Browse files
committed
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 #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
2 parents 8651758 + 8f8230a commit 501212b

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,9 @@ private function buildXml(\DOMNode $parentNode, $data, string $xmlRootNodeName =
378378
} elseif ('#' === $key) {
379379
$append = $this->selectNodeType($parentNode, $data);
380380
} elseif ('#comment' === $key) {
381-
$append = $this->appendComment($parentNode, $data);
381+
if (!\in_array(XML_COMMENT_NODE, $this->encoderIgnoredNodeTypes, true)) {
382+
$append = $this->appendComment($parentNode, $data);
383+
}
382384
} elseif (\is_array($data) && false === is_numeric($key)) {
383385
// Is this array fully numeric keys?
384386
if (ctype_digit(implode('', array_keys($data)))) {

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,21 @@ public function testEncodeWithoutPI()
782782
$this->assertEquals($expected, $encoder->encode(array(), 'xml'));
783783
}
784784

785+
public function testEncodeWithoutComment()
786+
{
787+
$encoder = new XmlEncoder('response', null, array(), array(XML_COMMENT_NODE));
788+
789+
$expected = <<<'XML'
790+
<?xml version="1.0"?>
791+
<response/>
792+
793+
XML;
794+
795+
$data = array('#comment' => ' foo ');
796+
797+
$this->assertEquals($expected, $encoder->encode($data, 'xml'));
798+
}
799+
785800
/**
786801
* @return XmlEncoder
787802
*/

0 commit comments

Comments
 (0)