Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. XmlEncoder.php

function XmlEncoder::buildXml

Parse the data and convert it to DOMElements.

Throws

NotEncodableValueException

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 342

Class

XmlEncoder
@author Jordi Boggiano <j.boggiano@seld.be> @author John Wards <jwards@whiteoctober.co.uk> @author Fabian Vogler <fabian@equivalence.ch> @author Kévin Dunglas <dunglas@gmail.com> @author Dany Maillard…

Namespace

Symfony\Component\Serializer\Encoder

Code

private function buildXml(\DOMNode $parentNode, mixed $data, string $format, array $context, ?string $xmlRootNodeName = null) : bool {
    $append = true;
    $removeEmptyTags = $context[self::REMOVE_EMPTY_TAGS] ?? $this->defaultContext[self::REMOVE_EMPTY_TAGS] ?? false;
    $encoderIgnoredNodeTypes = $context[self::ENCODER_IGNORED_NODE_TYPES] ?? $this->defaultContext[self::ENCODER_IGNORED_NODE_TYPES];
    if (\is_array($data) || $data instanceof \Traversable && (null === $this->serializer || !$this->serializer
        ->supportsNormalization($data, $format))) {
        foreach ($data as $key => $data) {
            // Ah this is the magic @ attribute types.
            if (str_starts_with($key, '@') && $this->isElementNameValid($attributeName = substr($key, 1))) {
                if (!\is_scalar($data)) {
                    $data = $this->serializer
                        ->normalize($data, $format, $context);
                }
                if (\is_bool($data)) {
                    $data = (int) $data;
                }
                $parentNode->setAttribute($attributeName, $data);
            }
            elseif ('#' === $key) {
                $append = $this->selectNodeType($parentNode, $data, $format, $context);
            }
            elseif ('#comment' === $key) {
                if (!\in_array(\XML_COMMENT_NODE, $encoderIgnoredNodeTypes, true)) {
                    $append = $this->appendComment($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 ["item" => [0,1]];.
                     */
                    foreach ($data as $subData) {
                        $append = $this->appendNode($parentNode, $subData, $format, $context, $key);
                    }
                }
                else {
                    $append = $this->appendNode($parentNode, $data, $format, $context, $key);
                }
            }
            elseif (is_numeric($key) || !$this->isElementNameValid($key)) {
                $append = $this->appendNode($parentNode, $data, $format, $context, 'item', $key);
            }
            elseif (null !== $data || !$removeEmptyTags) {
                $append = $this->appendNode($parentNode, $data, $format, $context, $key);
            }
        }
        return $append;
    }
    if (\is_object($data)) {
        if (null === $this->serializer) {
            throw new BadMethodCallException(\sprintf('The serializer needs to be set to allow "%s()" to be used with object data.', __METHOD__));
        }
        $data = $this->serializer
            ->normalize($data, $format, $context);
        if (null !== $data && !\is_scalar($data)) {
            return $this->buildXml($parentNode, $data, $format, $context, $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, $format, $context, $xmlRootNodeName);
        }
        return $this->appendNode($parentNode, $data, $format, $context, 'data');
    }
    throw new NotEncodableValueException('An unexpected value could not be serialized: ' . (!\is_resource($data) ? var_export($data, true) : \sprintf('%s resource', get_resource_type($data))));
}
RSS feed
Powered by Drupal