public function XmlEncoder::decode

Decodes a string into PHP data

Parameters

scalar $data Data to decode:

string $format Format name:

Return value

mixed

Overrides DecoderInterface::decode

File

drupal/core/vendor/symfony/serializer/Symfony/Component/Serializer/Encoder/XmlEncoder.php, line 55

Class

XmlEncoder
Encodes XML data

Namespace

Symfony\Component\Serializer\Encoder

Code

public function decode($data, $format) {
  $internalErrors = libxml_use_internal_errors(true);
  $disableEntities = libxml_disable_entity_loader(true);
  libxml_clear_errors();
  $dom = new \DOMDocument();
  $dom
    ->loadXML($data, LIBXML_NONET);
  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);
  }
  if (!$xml
    ->count()) {
    if (!$xml
      ->attributes()) {
      return (string) $xml;
    }
    $data = array();
    foreach ($xml
      ->attributes() as $attrkey => $attr) {
      $data['@' . $attrkey] = (string) $attr;
    }
    $data['#'] = (string) $xml;
    return $data;
  }
  return $this
    ->parseXml($xml);
}