10000 [Serializer] Ignore comments when decoding XML by q0rban · Pull Request #26445 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Serializer] Ignore comments when decoding XML #26445

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

Closed
Prev Previous commit
Next Next commit
[Serializer] Ignore comments when decoding XML
Previously, if the first line of XML was a comment, that would be used as
the root node of the decoded XML. This work strips comments from decoded
XML by default, but also allows for customizing which XML node types are
ignored during decoding.
  • Loading branch information
James Sansbury committed Mar 7, 2018
commit 744eea50014d48e02a5390a5f7438056c9e38e4b
9 changes: 6 additions & 3 deletions src/Symfony/Component/Serializer/Encoder/XmlEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,19 @@ class XmlEncoder implements EncoderInterface, DecoderInterface, NormalizationAwa
private $context;
private $rootNodeName = 'response';
private $loadOptions;
private $ignoredNodeTypes;

/**
* Construct new XmlEncoder and allow to change the root node element name.
*
* @param int|null $loadOptions A bit field of LIBXML_* constants
* @param array $ignoredNodeTypes An array of ignored XML node types, each one of the DOM Predefined XML_* Constants.
*/
public function __construct(string $rootNodeName = 'response', int $loadOptions = null)
public function __construct(string $rootNodeName = 'response', int $loadOptions = null, array $ignoredNodeTypes = null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

, array $ignoredNodeTypes = array(XML_PI_NODE, XML_COMMENT_NODE)

then you can get rid of ternary

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wondered about that. I was following the pattern I saw for $loadOptions, but I'm happy to switch to that.

{
$this->rootNodeName = $rootNodeName;
$this->loadOptions = null !== $loadOptions ? $loadOptions : LIBXML_NONET | LIBXML_NOBLANKS;
$this->ignoredNodeTypes = !empty($ignoredNodeTypes) ? $ignoredNodeTypes : array(XML_PI_NODE, XML_COMMENT_NODE);
}

/**
Expand Down Expand Up @@ -105,7 +108,7 @@ public function decode($data, $format, array $context = array())
if (XML_DOCUMENT_TYPE_NODE === $child->nodeType) {
throw new NotEncodableValueException('Document types are not allowed.');
}
if (!$rootNode && XML_PI_NODE !== $child->nodeType) {
if (!$rootNode && !\in_array($child->nodeType, $this->ignoredNodeTypes, TRUE)) {
$rootNode = $child;
}
}
Expand Down Expand Up @@ -316,7 +319,7 @@ private function parseXmlValue(\DOMNode $node, array $context = array())
$value = array();

foreach ($node->childNodes as $subnode) {
if (XML_PI_NODE === $subnode->nodeType) {
if (\in_array($subnode->nodeType, $this->ignoredNodeTypes, TRUE)) {
continue;
}

Expand Down
0