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

Breadcrumb

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

function XmlEncoder::parseXmlValue

Parse the input DOMNode value (content and children) into an array or a string.

1 call to XmlEncoder::parseXmlValue()
XmlEncoder::parseXml in vendor/symfony/serializer/Encoder/XmlEncoder.php
Parse the input DOMNode into an array or a string.

File

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

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 parseXmlValue(\DOMNode $node, array $context = []) : array|string {
    if (!$node->hasChildNodes()) {
        return $node->nodeValue;
    }
    if (1 === $node->childNodes->length && \in_array($node->firstChild->nodeType, [
        \XML_TEXT_NODE,
        \XML_CDATA_SECTION_NODE,
    ])) {
        return $node->firstChild->nodeValue;
    }
    $value = [];
    $decoderIgnoredNodeTypes = $context[self::DECODER_IGNORED_NODE_TYPES] ?? $this->defaultContext[self::DECODER_IGNORED_NODE_TYPES];
    foreach ($node->childNodes as $subnode) {
        if (\in_array($subnode->nodeType, $decoderIgnoredNodeTypes, true)) {
            continue;
        }
        $val = $this->parseXml($subnode, $context);
        if ('item' === $subnode->nodeName && isset($val['@key'])) {
            $value[$val['@key']] = $val['#'] ?? $val;
        }
        else {
            $value[$subnode->nodeName][] = $val;
        }
    }
    $asCollection = $context[self::AS_COLLECTION] ?? $this->defaultContext[self::AS_COLLECTION];
    foreach ($value as $key => $val) {
        if (!$asCollection && \is_array($val) && 1 === \count($val)) {
            $value[$key] = current($val);
        }
    }
    return $value;
}
RSS feed
Powered by Drupal