@@ -59,6 +59,7 @@ class XmlEncoder implements EncoderInterface, DecoderInterface, NormalizationAwa
5959 public const TYPE_CAST_ATTRIBUTES = 'xml_type_cast_attributes ' ;
6060 public const VERSION = 'xml_version ' ;
6161 public const CDATA_WRAPPING = 'cdata_wrapping ' ;
62+ public const CDATA_WRAPPING_NAME_PATTERN = 'cdata_wrapping_name_pattern ' ;
6263 public const CDATA_WRAPPING_PATTERN = 'cdata_wrapping_pattern ' ;
6364 public const IGNORE_EMPTY_ATTRIBUTES = 'ignore_empty_attributes ' ;
6465
@@ -440,10 +441,15 @@ private function appendNode(\DOMNode $parentNode, mixed $data, string $format, a
440441
441442 /**
442443 * Checks if a value contains any characters which would require CDATA wrapping.
444+ *
445+ * @param array<mixed, mixed> $context
443446 */
444- private function needsCdataWrapping (string $ val , array $ context ): bool
447+ private function needsCdataWrapping (string $ name , string $ val , array $ context ): bool
445448 {
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 );
449+ return ($ context [self ::CDATA_WRAPPING ] ?? $ this ->defaultContext [self ::CDATA_WRAPPING ]) &&
450+ (preg_match ($ context [self ::CDATA_WRAPPING_PATTERN ] ?? $ this ->defaultContext [self ::CDATA_WRAPPING_PATTERN ], $ val ) ||
451+ (($ 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 ))
452+ );
447453 }
448454
449455 /**
@@ -471,7 +477,7 @@ private function selectNodeType(\DOMNode $node, mixed $val, string $format, arra
471477 return $ this ->selectNodeType ($ node , $ this ->serializer ->normalize ($ val , $ format , $ context ), $ format , $ context );
472478 } elseif (is_numeric ($ val )) {
473479 return $ this ->appendText ($ node , (string ) $ val );
474- } elseif (\is_string ($ val ) && $ this ->needsCdataWrapping ($ val , $ context )) {
480+ } elseif (\is_string ($ val ) && $ this ->needsCdataWrapping ($ node -> nodeName , $ val , $ context )) {
475481 return $ this ->appendCData ($ node , $ val );
476482 } elseif (\is_string ($ val )) {
477483 return $ this ->appendText ($ node , $ val );
0 commit comments