10000 Allow to encode xml comments · symfony/symfony@477df1d · GitHub
[go: up one dir, main page]

Skip to content

Commit 477df1d

Browse files
committed
Allow to encode xml comments
1 parent ea0b508 commit 477df1d

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* @author John Wards <jwards@whiteoctober.co.uk>
2121
* @author Fabian Vogler <fabian@equivalence.ch>
2222
* @author Kévin Dunglas <dunglas@gmail.com>
23+
* @author Dany Maillard <danymaillard93b@gmail.com>
2324
*/
2425
class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, DecoderInterface, NormalizationAwareInterface
2526
{
@@ -235,6 +236,13 @@ final protected function appendDocumentFragment(\DOMNode $node, $fragment)
235236
return false;
236237
}
237238

239+
final protected function appendComment(\DOMNode $node, $data)
240+
{
241+
$node->appendChild($this->dom->createComment($data));
242+
243+
return true;
244+
}
245+
238246
/**
239247
* Checks the name is a valid xml element name.
240248
*
@@ -384,6 +392,8 @@ private function buildXml(\DOMNode $parentNode, $data, $xmlRootNodeName = null)
384392
$parentNode->setAttribute($attributeName, $data);
385393
} elseif ('#' === $key) {
386394
$append = $this->selectNodeType($parentNode, $data);
395+
} elseif ('#comment' === $key) {
396+
$append = $this->appendComment($parentNode, $data);
387397
} elseif (\is_array($data) && false === is_numeric($key)) {
388398
// Is this array fully numeric keys?
389399
if (ctype_digit(implode('', array_keys($data)))) {

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,19 @@ public function testEncodeXmlWithDateTimeObjectField()
633633
$this->assertEquals($this->createXmlWithDateTimeField(), $actualXml);
634634
}
635635

636+
public function testEncodeComment()
637+
{
638+
$expected = <<<'XML'
639+
<?xml version="1.0"?>
640+
<response><!-- foo --></response>
641+
642+
XML;
643+
644+
$data = array('#comment' => ' foo ');
645+
646+
$this->assertEquals($expected, $this->encoder->encode($data, 'xml'));
647+
}
648+
636649
/**
637650
* @return XmlEncoder
638651
*/

0 commit comments

Comments
 (0)
0