Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 86c2978

Browse files
committed
Make document type nodes ignorable
1 parent ee211c4 commit 86c2978

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

src/Symfony/Component/Serializer/Encoder/XmlEncoder.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,13 @@ public function decode($data, $format, array $context = [])
154154
$rootNode = null;
155155
$decoderIgnoredNodeTypes = $context[self::DECODER_IGNORED_NODE_TYPES] ?? $this->defaultContext[self::DECODER_IGNORED_NODE_TYPES];
156156
foreach ($dom->childNodes as $child) {
157+
if (\in_array($child->nodeType, $decoderIgnoredNodeTypes, true)) {
158+
continue;
159+
}
157160
if (\XML_DOCUMENT_TYPE_NODE === $child->nodeType) {
158161
throw new NotEncodableValueException('Document types are not allowed.');
159162
}
160-
if (!$rootNode && !\in_array($child->nodeType, $decoderIgnoredNodeTypes, true)) {
163+
if (!$rootNode) {
161164
$rootNode = $child;
162165
}
163166
}
@@ -477,7 +480,7 @@ private function buildXml(\DOMNode $parentNode, $data, string $xmlRootNodeName =
477480
return $this->appendNode($parentNode, $data, 'data');
478481
}
479482

480-
throw new NotEncodableValueException('An unexpected value could not be serialized: '.(!\is_resource($data) ? var_export($data, true) : sprintf('%s resource', get_resource_type($data))));
483+
throw new NotEncodableValueException('An unexpected value could not be serialized: '.(!\is_resource($data) ? var_export($data, true) : sprintf('"%s" resource', get_resource_type($data))));
481484
}
482485

483486
/**

src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,33 @@ public function testDecodeIgnoreComments()
587587
$this->assertEquals($expected, $this->encoder->decode($source, 'xml'));
588588
}
589589

590+
public function testDecodeIgnoreDocumentType()
591+
{
592+
$source = <<<'XML'
593+
<?xml version="1.0"?>
594+
<!DOCTYPE people>
595+
<people>
596+
<person>
597+
<firstname>Benjamin</firstname>
598+
<lastname>Alexandre</lastname>
599+
</person>
600+
<person>
601+
<firstname>Damien</firstname>
602+
<lastname>Clay</lastname>
603+
</person>
604+
</people>
605+
XML;
606+
$expected = ['person' => [
607+
['firstname' => 'Benjamin', 'lastname' => 'Alexandre'],
608+
['firstname' => 'Damien', 'lastname' => 'Clay'],
609+
]];
610+
$this->assertEquals($expected, $this->encoder->decode(
611+
$source,
612+
'xml',
613+
[XmlEncoder::DECODER_IGNORED_NODE_TYPES => [\XML_DOCUMENT_TYPE_NODE]]
614+
));
615+
}
616+
590617
public function testDecodePreserveComments()
591618
{
592619
$this->doTestDecodePreserveComments();

0 commit comments

Comments
 (0)