@@ -59,6 +59,7 @@ class XmlEncoder implements EncoderInterface, DecoderInterface, NormalizationAwa
59
59
public const TYPE_CAST_ATTRIBUTES = 'xml_type_cast_attributes ' ;
60
60
public const VERSION = 'xml_version ' ;
61
61
public const CDATA_WRAPPING = 'cdata_wrapping ' ;
62
+ public const CDATA_WRAPPING_NAME_PATTERN = 'cdata_wrapping_name_pattern ' ;
62
63
public const CDATA_WRAPPING_PATTERN = 'cdata_wrapping_pattern ' ;
63
64
public const IGNORE_EMPTY_ATTRIBUTES = 'ignore_empty_attributes ' ;
64
65
@@ -72,6 +73,7 @@ class XmlEncoder implements EncoderInterface, DecoderInterface, NormalizationAwa
72
73
self ::ROOT_NODE_NAME => 'response ' ,
73
74
self ::TYPE_CAST_ATTRIBUTES => true ,
74
75
self ::CDATA_WRAPPING => true ,
76
+ self ::CDATA_WRAPPING_NAME_PATTERN => false ,
75
77
self ::CDATA_WRAPPING_PATTERN => '/[<>&]/ ' ,
76
78
self ::IGNORE_EMPTY_ATTRIBUTES => false ,
77
79
];
@@ -440,10 +442,15 @@ private function appendNode(\DOMNode $parentNode, mixed $data, string $format, a
440
442
441
443
/**
442
444
* Checks if a value contains any characters which would require CDATA wrapping.
445
+ *
446
+ * @param array<string, mixed> $context
443
447
*/
444
- private function needsCdataWrapping (string $ val , array $ context ): bool
448
+ private function needsCdataWrapping (string $ name , string $ val , array $ context ): bool
445
449
{
446
- return ($ context [self ::CDATA_WRAPPING ] ?? $ this ->defaultContext [self ::CDATA_WRAPPING ]) && preg_match ($ context [self ::CDATA_WRAPPING_PATTERN ] ?? $ this ->defaultContext [self ::CDATA_WRAPPING_PATTERN ], $ val );
450
+ return ($ context [self ::CDATA_WRAPPING ] ?? $ this ->defaultContext [self ::CDATA_WRAPPING ])
451
+ && (preg_match ($ context [self ::CDATA_WRAPPING_PATTERN ] ?? $ this ->defaultContext [self ::CDATA_WRAPPING_PATTERN ], $ val )
452
+ || (($ context [self ::CDATA_WRAPPING_NAME_PATTERN ] ?? $ this ->defaultContext [self ::CDATA_WRAPPING_NAME_PATTERN ]) && preg_match ($ context [self ::CDATA_WRAPPING_NAME_PATTERN ] ?? $ this ->defaultContext [self ::CDATA_WRAPPING_NAME_PATTERN ], $ name ))
453
+ );
447
454
}
448
455
449
456
/**
@@ -471,7 +478,7 @@ private function selectNodeType(\DOMNode $node, mixed $val, string $format, arra
471
478
return $ this ->selectNodeType ($ node , $ this ->serializer ->normalize ($ val , $ format , $ context ), $ format , $ context );
472
479
} elseif (is_numeric ($ val )) {
473
480
return $ this ->appendText ($ node , (string ) $ val );
474
- } elseif (\is_string ($ val ) && $ this ->needsCdataWrapping ($ val , $ context )) {
481
+ } elseif (\is_string ($ val ) && $ this ->needsCdataWrapping ($ node -> nodeName , $ val , $ context )) {
475
482
return $ this ->appendCData ($ node , $ val );
476
483
} elseif (\is_string ($ val )) {
477
484
return $ this ->appendText ($ node , $ val );
0 commit comments