From c896d71594fb9b39694b1c0e9b4b8e382da27762 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sun, 26 Aug 2012 18:38:30 +0200 Subject: [PATCH] refined previous commit --- .../Serializer/Encoder/XmlEncoder.php | 24 +++++++++---------- .../Serializer/Encoder/XmlEncoderTest.php | 6 ++--- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php b/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php index 9d863aaa4d..6dd8a3f1d8 100644 --- a/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php +++ b/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php @@ -54,15 +54,24 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec */ public function decode($data, $format) { - $this->assertNoCustomDocType($data); $internalErrors = libxml_use_internal_errors(true); $disableEntities = libxml_disable_entity_loader(true); libxml_clear_errors(); - $xml = simplexml_load_string($data); + $dom = new \DOMDocument(); + $dom->loadXML($data); + libxml_use_internal_errors($internalErrors); libxml_disable_entity_loader($disableEntities); + foreach ($dom->childNodes as $child) { + if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) { + throw new UnexpectedValueException('Document types are not allowed.'); + } + } + + $xml = simplexml_import_dom($dom); + if ($error = libxml_get_last_error()) { throw new UnexpectedValueException($error->message); } @@ -291,17 +300,6 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec throw new UnexpectedValueException('An unexpected value could not be serialized: '.var_export($data, true)); } - private function assertNoCustomDocType($data) - { - $dom = new \DOMDocument; - $dom->loadXML($data); - foreach ($dom->childNodes as $child) { - if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) { - throw new \InvalidArgumentException('Document types are not allowed.'); - } - } - } - /** * Selects the type of node to create and appends it to the parent. * diff --git a/tests/Symfony/Tests/Component/Serializer/Encoder/XmlEncoderTest.php b/tests/Symfony/Tests/Component/Serializer/Encoder/XmlEncoderTest.php index f2c450de6a..3c3449a6e6 100644 --- a/tests/Symfony/Tests/Component/Serializer/Encoder/XmlEncoderTest.php +++ b/tests/Symfony/Tests/Component/Serializer/Encoder/XmlEncoderTest.php @@ -54,7 +54,7 @@ class XmlEncoderTest extends \PHPUnit_Framework_TestCase } /** - * @expectedException \InvalidArgumentException + * @expectedException UnexpectedValueException * @expectedExceptionMessage Document types are not allowed. */ public function testDocTypeIsNotAllowed() @@ -255,8 +255,8 @@ class XmlEncoderTest extends \PHPUnit_Framework_TestCase } catch (\Exception $e) { chdir($oldCwd); - if (!$e instanceof \InvalidArgumentException) { - $this->fail('Expected InvalidArgumentException'); + if (!$e instanceof UnexpectedValueException) { + $this->fail('Expected UnexpectedValueException'); } } }