8000 [Serializer] Add support for ignoring comments while XML encoding by maidmaid · Pull Request #28289 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Serializer] Add support for ignoring comments while XML encoding #28289

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Symfony/Component/Serializer/Encoder/XmlEncoder.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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