Skip to content

Commit

Permalink
refined previous commit
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Aug 28, 2012
1 parent dc18faa commit cfea6c0
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions Encoder/XmlEncoder.php
Expand Up @@ -54,15 +54,24 @@ public function encode($data, $format)
*/
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);
}
Expand Down Expand Up @@ -291,17 +300,6 @@ private function buildXml($parentNode, $data)
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.
*
Expand Down

0 comments on commit cfea6c0

Please sign in to comment.