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

Breadcrumb

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

function Inline::parseMapping

Parses a YAML mapping.

Throws

ParseException When malformed inline YAML string is parsed

2 calls to Inline::parseMapping()
Inline::parse in vendor/symfony/yaml/Inline.php
Converts a YAML string to a PHP value.
Inline::parseSequence in vendor/symfony/yaml/Inline.php
Parses a YAML sequence.

File

vendor/symfony/yaml/Inline.php, line 423

Class

Inline
Inline implements a YAML parser/dumper for the YAML inline syntax.

Namespace

Symfony\Component\Yaml

Code

private static function parseMapping(string $mapping, int $flags, int &$i = 0, array &$references = []) : array|\stdClass {
    $output = [];
    $len = \strlen($mapping);
    ++$i;
    $allowOverwrite = false;
    // {foo: bar, bar:foo, ...}
    while ($i < $len) {
        switch ($mapping[$i]) {
            case ' ':
            case ',':
            case "\n":
                ++$i;
                continue 2;
            case '}':
                if (self::$objectForMap) {
                    return (object) $output;
                }
                return $output;
        }
        // key
        $offsetBeforeKeyParsing = $i;
        $isKeyQuoted = \in_array($mapping[$i], [
            '"',
            "'",
        ], true);
        $key = self::parseScalar($mapping, $flags, [
            ':',
            ' ',
        ], $i, false);
        if ($offsetBeforeKeyParsing === $i) {
            throw new ParseException('Missing mapping key.', self::$parsedLineNumber + 1, $mapping);
        }
        if ('!php/const' === $key || '!php/enum' === $key) {
            $key .= ' ' . self::parseScalar($mapping, $flags, [
                ':',
            ], $i, false);
            $key = self::evaluateScalar($key, $flags);
        }
        if (false === ($i = strpos($mapping, ':', $i))) {
            break;
        }
        if (!$isKeyQuoted) {
            $evaluatedKey = self::evaluateScalar($key, $flags, $references);
            if ('' !== $key && $evaluatedKey !== $key && !\is_string($evaluatedKey) && !\is_int($evaluatedKey)) {
                throw new ParseException('Implicit casting of incompatible mapping keys to strings is not supported. Quote your evaluable mapping keys instead.', self::$parsedLineNumber + 1, $mapping);
            }
        }
        if (!$isKeyQuoted && (!isset($mapping[$i + 1]) || !\in_array($mapping[$i + 1], [
            ' ',
            ',',
            '[',
            ']',
            '{',
            '}',
            "\n",
        ], true))) {
            throw new ParseException('Colons must be followed by a space or an indication character (i.e. " ", ",", "[", "]", "{", "}").', self::$parsedLineNumber + 1, $mapping);
        }
        if ('<<' === $key) {
            $allowOverwrite = true;
        }
        while ($i < $len) {
            if (':' === $mapping[$i] || ' ' === $mapping[$i] || "\n" === $mapping[$i]) {
                ++$i;
                continue;
            }
            $tag = self::parseTag($mapping, $i, $flags);
            switch ($mapping[$i]) {
                case '[':
                    // nested sequence
                    $value = self::parseSequence($mapping, $flags, $i, $references);
                    // Spec: Keys MUST be unique; first one wins.
                    // Parser cannot abort this mapping earlier, since lines
                    // are processed sequentially.
                    // But overwriting is allowed when a merge node is used in current block.
                    if ('<<' === $key) {
                        foreach ($value as $parsedValue) {
                            $output += $parsedValue;
                        }
                    }
                    elseif ($allowOverwrite || !isset($output[$key])) {
                        if (null !== $tag) {
                            $output[$key] = new TaggedValue($tag, $value);
                        }
                        else {
                            $output[$key] = $value;
                        }
                    }
                    elseif (isset($output[$key])) {
                        throw new ParseException(\sprintf('Duplicate key "%s" detected.', $key), self::$parsedLineNumber + 1, $mapping);
                    }
                    break;
                case '{':
                    // nested mapping
                    $value = self::parseMapping($mapping, $flags, $i, $references);
                    // Spec: Keys MUST be unique; first one wins.
                    // Parser cannot abort this mapping earlier, since lines
                    // are processed sequentially.
                    // But overwriting is allowed when a merge node is used in current block.
                    if ('<<' === $key) {
                        $output += $value;
                    }
                    elseif ($allowOverwrite || !isset($output[$key])) {
                        if (null !== $tag) {
                            $output[$key] = new TaggedValue($tag, $value);
                        }
                        else {
                            $output[$key] = $value;
                        }
                    }
                    elseif (isset($output[$key])) {
                        throw new ParseException(\sprintf('Duplicate key "%s" detected.', $key), self::$parsedLineNumber + 1, $mapping);
                    }
                    break;
                default:
                    $value = self::parseScalar($mapping, $flags, [
                        ',',
                        '}',
                        "\n",
                    ], $i, null === $tag, $references, $isValueQuoted);
                    // Spec: Keys MUST be unique; first one wins.
                    // Parser cannot abort this mapping earlier, since lines
                    // are processed sequentially.
                    // But overwriting is allowed when a merge node is used in current block.
                    if ('<<' === $key) {
                        $output += $value;
                    }
                    elseif ($allowOverwrite || !isset($output[$key])) {
                        if (!$isValueQuoted && \is_string($value) && '' !== $value && '&' === $value[0] && !self::isBinaryString($value) && Parser::preg_match(Parser::REFERENCE_PATTERN, $value, $matches)) {
                            $references[$matches['ref']] = $matches['value'];
                            $value = $matches['value'];
                        }
                        if (null !== $tag) {
                            $output[$key] = new TaggedValue($tag, $value);
                        }
                        else {
                            $output[$key] = $value;
                        }
                    }
                    elseif (isset($output[$key])) {
                        throw new ParseException(\sprintf('Duplicate key "%s" detected.', $key), self::$parsedLineNumber + 1, $mapping);
                    }
                    --$i;
            }
            ++$i;
            continue 2;
        }
    }
    throw new ParseException(\sprintf('Malformed inline YAML string: "%s".', $mapping), self::$parsedLineNumber + 1, null, self::$parsedFilename);
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal