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 */
2627class 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
FE09
$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 )) {
0 commit comments