-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Serializer] Add an option to the XmlEncoder
allowing to decode tags as collection
#58604
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.3
Are you sure you want to change the base?
Changes from all commits
f93c89a
46e9197
dff07d4
58fc1a9
517cd87
0921aae
6f7d48f
61ee9c8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,11 @@ class XmlEncoder implements EncoderInterface, DecoderInterface, NormalizationAwa | |
|
||
public const AS_COLLECTION = 'as_collection'; | ||
|
||
/** | ||
* An array of XML tags who should always be treated as a collection, even when it has only one child. | ||
*/ | ||
public const FORCE_COLLECTION = 'force_collection'; | ||
|
||
/** | ||
* An array of ignored XML node types while decoding, each one of the DOM Predefined XML_* constants. | ||
*/ | ||
|
@@ -72,6 +77,7 @@ class XmlEncoder implements EncoderInterface, DecoderInterface, NormalizationAwa | |
self::TYPE_CAST_ATTRIBUTES => true, | ||
self::CDATA_WRAPPING => true, | ||
self::CDATA_WRAPPING_PATTERN => '/[<>&]/', | ||
self::FORCE_COLLECTION => [], | ||
]; | ||
|
||
public function __construct(array $defaultContext = []) | ||
|
@@ -224,10 +230,20 @@ final protected function isElementNameValid(string $name): bool | |
*/ | ||
private function parseXml(\DOMNode $node, array $context = []): array|string | ||
{ | ||
$nodeName = $node->nodeName; | ||
|
||
$data = $this->parseXmlAttributes($node, $context); | ||
|
||
$value = $this->parseXmlValue($node, $context); | ||
|
||
if (\is_array($value) | ||
&& ($childNodeName = $node->firstChild?->nodeName) | ||
&& 1 === \count($value) | ||
&& \in_array($nodeName, $context[self::FORCE_COLLECTION] ?? $this->defaultContext[self::FORCE_COLLECTION], true) | ||
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. I think it could be great to accept the value 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. Maybe, however I'm still unsure how this PR would be welcome. I'm okay to continue working on it, but I'd like to get a green light before investing too much time on it 😅 But if it get a positive feedback (which doesn't seem to be the case for the moment) I can consider adding a test case for this, maybe using xpath also 🤔 |
||
) { | ||
return [$childNodeName => [$value[$childNodeName]]]; | ||
} | ||
Comment on lines
+239
to
+245
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. This condition can go after the 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. Actually it can't according to the test suite 👀 if (!\count($data)) {
return $value;
} |
||
|
||
if (!\count($data)) { | ||
return $value; | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.