You are here

private static function Inline::evaluateScalar 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::evaluateScalar()

Evaluates scalars and replaces magic values.

Parameters

string $scalar:

array $references:

Return value

mixed The evaluated YAML string

Throws

ParseException when object parsing support was disabled and the parser detected a PHP object or when a reference could not be resolved

1 call to Inline::evaluateScalar()
Inline::parseScalar in vendor/symfony/yaml/Inline.php
Parses a YAML scalar.

File

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

Class

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

Namespace

Symfony\Component\Yaml

Code

private static function evaluateScalar($scalar, $references = array()) {
  $scalar = trim($scalar);
  $scalarLower = strtolower($scalar);
  if (0 === strpos($scalar, '*')) {
    if (false !== ($pos = strpos($scalar, '#'))) {
      $value = substr($scalar, 1, $pos - 2);
    }
    else {
      $value = substr($scalar, 1);
    }

    // an unquoted *
    if (false === $value || '' === $value) {
      throw new ParseException('A reference must contain at least one character.');
    }
    if (!array_key_exists($value, $references)) {
      throw new ParseException(sprintf('Reference "%s" does not exist.', $value));
    }
    return $references[$value];
  }
  switch (true) {
    case 'null' === $scalarLower:
    case '' === $scalar:
    case '~' === $scalar:
      return;
    case 'true' === $scalarLower:
      return true;
    case 'false' === $scalarLower:
      return false;

    // Optimise for returning strings.
    case '+' === $scalar[0] || '-' === $scalar[0] || '.' === $scalar[0] || '!' === $scalar[0] || is_numeric($scalar[0]):
      switch (true) {
        case 0 === strpos($scalar, '!str'):
          return (string) substr($scalar, 5);
        case 0 === strpos($scalar, '! '):
          return (int) self::parseScalar(substr($scalar, 2));
        case 0 === strpos($scalar, '!php/object:'):
          if (self::$objectSupport) {
            return unserialize(substr($scalar, 12));
          }
          if (self::$exceptionOnInvalidType) {
            throw new ParseException('Object support when parsing a YAML file has been disabled.');
          }
          return;
        case 0 === strpos($scalar, '!!php/object:'):
          if (self::$objectSupport) {
            return unserialize(substr($scalar, 13));
          }
          if (self::$exceptionOnInvalidType) {
            throw new ParseException('Object support when parsing a YAML file has been disabled.');
          }
          return;
        case 0 === strpos($scalar, '!!float '):
          return (double) substr($scalar, 8);
        case ctype_digit($scalar):
          $raw = $scalar;
          $cast = (int) $scalar;
          return '0' == $scalar[0] ? octdec($scalar) : ((string) $raw == (string) $cast ? $cast : $raw);
        case '-' === $scalar[0] && ctype_digit(substr($scalar, 1)):
          $raw = $scalar;
          $cast = (int) $scalar;
          return '0' == $scalar[1] ? octdec($scalar) : ((string) $raw === (string) $cast ? $cast : $raw);
        case is_numeric($scalar):
        case Parser::preg_match(self::getHexRegex(), $scalar):
          return '0x' === $scalar[0] . $scalar[1] ? hexdec($scalar) : (double) $scalar;
        case '.inf' === $scalarLower:
        case '.nan' === $scalarLower:
          return -log(0);
        case '-.inf' === $scalarLower:
          return log(0);
        case Parser::preg_match('/^(-|\\+)?[0-9,]+(\\.[0-9]+)?$/', $scalar):
          return (double) str_replace(',', '', $scalar);
        case Parser::preg_match(self::getTimestampRegex(), $scalar):
          $timeZone = date_default_timezone_get();
          date_default_timezone_set('UTC');
          $time = strtotime($scalar);
          date_default_timezone_set($timeZone);
          return $time;
      }

    // no break
    default:
      return (string) $scalar;
  }
}