You are here

private function XmlEncoder::buildXml in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/symfony/serializer/Encoder/XmlEncoder.php \Symfony\Component\Serializer\Encoder\XmlEncoder::buildXml()

Parse the data and convert it to DOMElements.

Parameters

\DOMNode $parentNode:

array|object $data:

string|null $xmlRootNodeName:

Return value

bool

Throws

UnexpectedValueException

2 calls to XmlEncoder::buildXml()
XmlEncoder::encode in vendor/symfony/serializer/Encoder/XmlEncoder.php
Encodes data into the given format.
XmlEncoder::selectNodeType in vendor/symfony/serializer/Encoder/XmlEncoder.php
Tests the value being passed and decide what sort of element to create.

File

vendor/symfony/serializer/Encoder/XmlEncoder.php, line 365

Class

XmlEncoder
Encodes XML data.

Namespace

Symfony\Component\Serializer\Encoder

Code

private function buildXml(\DOMNode $parentNode, $data, $xmlRootNodeName = null) {
  $append = true;
  if (is_array($data) || $data instanceof \Traversable) {
    foreach ($data as $key => $data) {

      //Ah this is the magic @ attribute types.
      if (0 === strpos($key, '@') && is_scalar($data) && $this
        ->isElementNameValid($attributeName = substr($key, 1))) {
        $parentNode
          ->setAttribute($attributeName, $data);
      }
      elseif ($key === '#') {
        $append = $this
          ->selectNodeType($parentNode, $data);
      }
      elseif (is_array($data) && false === is_numeric($key)) {

        // Is this array fully numeric keys?
        if (ctype_digit(implode('', array_keys($data)))) {

          /*
           * Create nodes to append to $parentNode based on the $key of this array
           * Produces <xml><item>0</item><item>1</item></xml>
           * From array("item" => array(0,1));.
           */
          foreach ($data as $subData) {
            $append = $this
              ->appendNode($parentNode, $subData, $key);
          }
        }
        else {
          $append = $this
            ->appendNode($parentNode, $data, $key);
        }
      }
      elseif (is_numeric($key) || !$this
        ->isElementNameValid($key)) {
        $append = $this
          ->appendNode($parentNode, $data, 'item', $key);
      }
      else {
        $append = $this
          ->appendNode($parentNode, $data, $key);
      }
    }
    return $append;
  }
  if (is_object($data)) {
    $data = $this->serializer
      ->normalize($data, $this->format, $this->context);
    if (null !== $data && !is_scalar($data)) {
      return $this
        ->buildXml($parentNode, $data, $xmlRootNodeName);
    }

    // top level data object was normalized into a scalar
    if (!$parentNode->parentNode->parentNode) {
      $root = $parentNode->parentNode;
      $root
        ->removeChild($parentNode);
      return $this
        ->appendNode($root, $data, $xmlRootNodeName);
    }
    return $this
      ->appendNode($parentNode, $data, 'data');
  }
  throw new UnexpectedValueException(sprintf('An unexpected value could not be serialized: %s', var_export($data, true)));
}