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:
commit
501212b934
@ -378,7 +378,9 @@ class XmlEncoder implements EncoderInterface, DecoderInterface, NormalizationAwa
|
|||||||
} elseif ('#' === $key) {
|
} elseif ('#' === $key) {
|
||||||
$append = $this->selectNodeType($parentNode, $data);
|
$append = $this->selectNodeType($parentNode, $data);
|
||||||
} elseif ('#comment' === $key) {
|
} 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)) {
|
} elseif (\is_array($data) && false === is_numeric($key)) {
|
||||||
// Is this array fully numeric keys?
|
// Is this array fully numeric keys?
|
||||||
if (ctype_digit(implode('', array_keys($data)))) {
|
if (ctype_digit(implode('', array_keys($data)))) {
|
||||||
|
@ -782,6 +782,21 @@ XML;
|
|||||||
$this->assertEquals($expected, $encoder->encode(array(), '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
|
* @return XmlEncoder
|
||||||
*/
|
*/
|
||||||
|
Reference in New Issue
Block a user