You are here

private static function Inline::parseMapping in Loft Data Grids 7.2

Same name and namespace in other branches
  1. 6.2 vendor/symfony/yaml/Symfony/Component/Yaml/Inline.php \Symfony\Component\Yaml\Inline::parseMapping()

Parses a YAML mapping.

Parameters

string $mapping:

int &$i:

array $references:

Return value

array|\stdClass

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 373

Class

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

Namespace

Symfony\Component\Yaml

Code

private static function parseMapping($mapping, &$i = 0, $references = array()) {
  $output = array();
  $len = strlen($mapping);
  ++$i;
  $allowOverwrite = false;

  // {foo: bar, bar:foo, ...}
  while ($i < $len) {
    switch ($mapping[$i]) {
      case ' ':
      case ',':
        ++$i;
        continue 2;
      case '}':
        if (self::$objectForMap) {
          return (object) $output;
        }
        return $output;
    }

    // key
    $key = self::parseScalar($mapping, array(
      ':',
      ' ',
    ), array(
      '"',
      "'",
    ), $i, false);
    if ('<<' === $key) {
      $allowOverwrite = true;
    }

    // value
    $done = false;
    while ($i < $len) {
      switch ($mapping[$i]) {
        case '[':

          // nested sequence
          $value = self::parseSequence($mapping, $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])) {
            $output[$key] = $value;
          }
          $done = true;
          break;
        case '{':

          // nested mapping
          $value = self::parseMapping($mapping, $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])) {
            $output[$key] = $value;
          }
          $done = true;
          break;
        case ':':
        case ' ':
          break;
        default:
          $value = self::parseScalar($mapping, array(
            ',',
            '}',
          ), array(
            '"',
            "'",
          ), $i, true, $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])) {
            $output[$key] = $value;
          }
          $done = true;
          --$i;
      }
      ++$i;
      if ($done) {
        continue 2;
      }
    }
  }
  throw new ParseException(sprintf('Malformed inline YAML string: %s.', $mapping));
}