[Serializer] [XML] Ignore Process Instruction

This commit is contained in:
Jordan Samouh 2017-03-17 17:37:38 +01:00
parent ab1d9383b9
commit 0c741f5704
2 changed files with 46 additions and 2 deletions

View File

@ -92,14 +92,16 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
throw new UnexpectedValueException($error->message);
}
$rootNode = null;
foreach ($dom->childNodes as $child) {
if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) {
throw new UnexpectedValueException('Document types are not allowed.');
}
if (!$rootNode && $child->nodeType !== XML_PI_NODE) {
$rootNode = $child;
}
}
$rootNode = $dom->firstChild;
// todo: throw an exception if the root node name is not correctly configured (bc)
if ($rootNode->hasChildNodes()) {
@ -329,6 +331,10 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
$value = array();
foreach ($node->childNodes as $subnode) {
if ($subnode->nodeType === XML_PI_NODE) {
continue;
}
$val = $this->parseXml($subnode);
if ('item' === $subnode->nodeName && isset($val['@key'])) {

View File

@ -371,6 +371,44 @@ XML;
$this->assertEquals($expected, $this->encoder->decode($source, 'xml'));
}
public function testDecodeXMLWithProcessInstruction()
{
$source = <<<'XML'
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="/xsl/xmlverbatimwrapper.xsl"?>
<?display table-view?>
<?sort alpha-ascending?>
<response>
<foo>foo</foo>
<?textinfo whitespace is allowed ?>
<bar>a</bar>
<bar>b</bar>
<baz>
<key>val</key>
<key2>val</key2>
<item key="A B">bar</item>
<item>
<title>title1</title>
</item>
<?item ignore-title ?>
<item>
<title>title2</title>
</item>
<Barry>
<FooBar id="1">
<Baz>Ed</Baz>
</FooBar>
</Barry>
</baz>
<qux>1</qux>
</response>
<?instruction <value> ?>
XML;
$obj = $this->getObject();
$this->assertEquals(get_object_vars($obj), $this->encoder->decode($source, 'xml'));
}
public function testDecodeIgnoreWhiteSpace()
{
$source = <<<'XML'