8000 Allow to encode xml comments · symfony/symfony@26f8e31 · GitHub
[go: up one dir, main page]

Skip to content

Commit 26f8e31

Browse files
committed
Allow to encode xml comments
1 parent df26fea commit 26f8e31

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* @author John Wards <jwards@whiteoctober.co.uk>
2323
* @author Fabian Vogler <fabian@equivalence.ch>
2424
* @author Kévin Dunglas <dunglas@gmail.com>
25+
* @author Dany Maillard <danymaillard93b@gmail.com>
2526
*/
2627
class XmlEncoder implements EncoderInterface, DecoderInterface, NormalizationAwareInterface, SerializerAwareInterface
2728
{
@@ -226,6 +227,13 @@ final protected function appendDocumentFragment(\DOMNode $node, $fragment): bool
226227
return false;
227228
}
228229

230+
private function appendComment(\DOMNode $node, string $data): bool
231+
{
232+
$node->appendChild($this->dom->createComment($data));
233+
234+
return true;
235+
}
236+
229237
/**
230238
* Checks the name is a valid xml element name.
231239
*/
@@ -364,8 +372,8 @@ private function buildXml(\DOMNode $parentNode, $data, string $xmlRootNodeName =
364372
$data = $this->serializer->normalize($data, $this->format, $this->context);
365373
}
366374
$parentNode->setAttribute($attributeName, $data);
367-
} elseif ('#' === $key) {
368-
$append = $this->selectNodeType($parentNode, $data);
375+
} elseif ('#' === $key[0]) { // # or #comment
376+
$append = $this->selectNodeType($parentNode, $data, $key);
369377
} elseif (\is_array($data) && false === is_numeric($key)) {
370378
// Is this array fully numeric keys?
371379
if (ctype_digit(implode('', array_keys($data)))) {
@@ -445,7 +453,7 @@ private function needsCdataWrapping(string $val): bool
445453
*
446454
* @throws NotEncodableValueException
447455
*/
448-
private function selectNodeType(\DOMNode $node, $val): bool
456+
private function selectNodeType(\DOMNode $node, $val, $key = null): bool
449457
{
450458
if (\is_array($val)) {
451459
return $this->buildXml($node, $val);
@@ -460,6 +468,8 @@ private function selectNodeType(\DOMNode $node, $val): bool
460468
return $this->appendText($node, (string) $val);
461469
} elseif (\is_string($val) && $this->needsCdataWrapping($val)) {
462470
return $this->appendCData($node, $val);
471+
} elseif ('#comment' === $key) {
472+
return $this->appendComment($node, $val);
463473
} elseif (\is_string($val)) {
464474
return $this->appendText($node, $val);
465475
} elseif (\is_bool($val)) {

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,19 @@ public function testEncodeXmlWithDateTimeObjectField()
760760
$this->assertEquals($this->createXmlWithDateTimeField(), $actualXml);
761761
AD46 }
762762

763+
public function testEncodeComment()
764+
{
765+
$expected = <<<'XML'
766+
<?xml version="1.0"?>
767+
<response><!-- foo --></response>
768+
769+
XML;
770+
771+
$data = array('#comment' => ' foo ');
772+
773+
$this->assertEquals($expected, $this->encoder->encode($data, 'xml'));
774+
}
775+
763776
/**
764777
* @return XmlEncoder
765778
*/

0 commit comments

Comments
 (0)
0