-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Serializer] Add CDATA_WRAPPING_NAME_PATTERN support to XmlEncoder #60355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 7.4
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
8000
|
@@ -59,6 +59,7 @@ class XmlEncoder implements EncoderInterface, DecoderInterface, NormalizationAwa | |
public const TYPE_CAST_ATTRIBUTES = 'xml_type_cast_attributes'; | ||
public const VERSION = 'xml_version'; | ||
public const CDATA_WRAPPING = 'cdata_wrapping'; | ||
public const CDATA_WRAPPING_NAME_PATTERN = 'cdata_wrapping_name_pattern'; | ||
public const CDATA_WRAPPING_PATTERN = 'cdata_wrapping_pattern'; | ||
public const IGNORE_EMPTY_ATTRIBUTES = 'ignore_empty_attributes'; | ||
|
||
|
@@ -72,6 +73,7 @@ class XmlEncoder implements EncoderInterface, DecoderInterface, NormalizationAwa | |
self::ROOT_NODE_NAME => 'response', | ||
self::TYPE_CAST_ATTRIBUTES => true, | ||
self::CDATA_WRAPPING => true, | ||
self::CDATA_WRAPPING_NAME_PATTERN => false, | ||
self::CDATA_WRAPPING_PATTERN => '/[<>&]/', | ||
self::IGNORE_EMPTY_ATTRIBUTES => false, | ||
]; | ||
|
@@ -440,10 +442,15 @@ private function appendNode(\DOMNode $parentNode, mixed $data, string $format, a | |
|
||
/** | ||
* Checks if a value contains any characters which would require CDATA wrapping. | ||
* | ||
* @param array<mixed, mixed> $context | ||
*/ | ||
private function needsCdataWrapping(string $val, array $context): bool | ||
private function needsCdataWrapping(string $name, string $val, array $context): bool | ||
{ | ||
return ($context[self::CDATA_WRAPPING] ?? $this->defaultContext[self::CDATA_WRAPPING]) && preg_match($context[self::CDATA_WRAPPING_PATTERN] ?? $this->defaultContext[self::CDATA_WRAPPING_PATTERN], $val); | ||
return ($context[self::CDATA_WRAPPING] ?? $this->defaultContext[self::CDATA_WRAPPING]) && | ||
(preg_match($context[self::CDATA_WRAPPING_PATTERN] ?? $this->defaultContext[self::CDATA_WRAPPING_PATTERN], $val) || | ||
(($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)) | ||
); | ||
} | ||
|
||
/** | ||
|
@@ -471,7 +478,7 @@ private function selectNodeType(\DOMNode $node, mixed $val, string $format, arra | |
return $this->selectNodeType($node, $this->serializer->normalize($val, $format, $context), $format, $context); | ||
} elseif (is_numeric($val)) { | ||
return $this->appendText($node, (string) $val); | ||
} elseif (\is_string($val) && $this->needsCdataWrapping($val, $context)) { | ||
} elseif (\is_string($val) && $this->needsCdataWrapping($node->nodeName, $val, $context)) { | ||
return $this->appendCData($node, $val); | ||
} elseif (\is_string($val)) { | ||
return $this->appendText($node, $val); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -568,6 +568,23 @@ public function testDecodeXMLWithProcessInstruction() | |
$this->assertEquals(get_object_vars($obj), $this->encoder->decode($source, 'xml')); | ||
} | ||
|
||
public function testCDataNamePattern() | ||
{ | ||
$expected = <<<'XML' | ||
<?xml version="1.0"?> | ||
<response><person><firstname><![CDATA[Benjamin]]></firstname><lastname><![CDATA[Alexandre]]></lastname><other><![CDATA[data]]></other></person><person><firstname><![CDATA[Damien]]></firstname><lastname><![CDATA[Clay]]></lastname><other><![CDATA[data]]></other></person></response> | ||
|
||
XML; | ||
$source = ['person' => [ | ||
['firstname' => 'Benjamin', 'lastname' => 'Alexandre', 'other' => 'data'], | ||
['firstname' => 'Damien', 'lastname' => 'Clay', 'other' => 'data'], | ||
]]; | ||
|
||
$this->assertEquals($expected, $this->encoder->encode($source, 'xml',[ | ||
XmlEncoder::CDATA_WRAPPING_NAME_PATTERN => '(firstname|lastname|)' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With the test, I did not expected to see There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes thats right. I will fix it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. using parenthesis as regex delimiter is quite uncommon. People reading the tests could think that we expect a partial pattern where we add the delimiter ourselves. I suggest changing the delimiters being used (and I also suggest using an anchored regex in that test) |
||
])); | ||
} | ||
|
||
public function testDecodeIgnoreWhiteSpace() | ||
{ | ||
$source = <<<'XML' | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.