22
22
* @author John Wards <jwards@whiteoctober.co.uk>
23
23
* @author Fabian Vogler <fabian@equivalence.ch>
24
24
* @author Kévin Dunglas <dunglas@gmail.com>
25
+ * @author Dany Maillard <danymaillard93b@gmail.com>
25
26
*/
26
27
class XmlEncoder implements EncoderInterface, DecoderInterface, NormalizationAwareInterface, SerializerAwareInterface
27
28
{
@@ -226,6 +227,13 @@ final protected function appendDocumentFragment(\DOMNode $node, $fragment): bool
226
227
return false ;
227
228
}
228
229
230
+ private function appendComment (\DOMNode $ node , string $ data ): bool
231
+ {
232
+ $ node ->appendChild ($ this ->dom ->createComment ($ data ));
233
+
234
+ return true ;
235
+ }
236
+
229
237
/**
230
238
* Checks the name is a valid xml element name.
231
239
*/
@@ -364,8 +372,8 @@ private function buildXml(\DOMNode $parentNode, $data, string $xmlRootNodeName =
364
372
$ data = $ this ->serializer ->normalize ($ data , $ this ->format , $ this ->context );
365
373
}
366
374
$ 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 );
369
377
} elseif (\is_array ($ data ) && false === is_numeric ($ key )) {
370
378
// Is this array fully numeric keys?
371
379
if (ctype_digit (implode ('' , array_keys ($ data )))) {
@@ -445,7 +453,7 @@ private function needsCdataWrapping(string $val): bool
445
453
*
446
454
* @throws NotEncodableValueException
447
455
*/
448
- private function selectNodeType (\DOMNode $ node , $ val ): bool
456
+ private function selectNodeType (\DOMNode $ node , $ val, $ key = null ): bool
449
457
{
450
458
if (\is_array ($ val )) {
451
459
return $ this ->buildXml ($ node , $ val );
@@ -460,6 +468,8 @@ private function selectNodeType(\DOMNode $node, $val): bool
460
468
return $ this ->appendText ($ node , (string ) $ val );
461
469
} elseif (\is_string ($ val ) && $ this ->needsCdataWrapping ($ val )) {
462
470
return $ this ->appendCData ($ node , $ val );
471
+ } elseif ('#comment ' === $ key ) {
472
+ return $ this ->appendComment ($ node , $ val );
463
473
} elseif (\is_string ($ val )) {
464
474
return $ this ->appendText ($ node , $ val );
465
475
} elseif (\is_bool ($ val )) {
0 commit comments