You are here

private static function Inline::evaluateScalar in Lockr 7.3

Evaluates scalars and replaces magic values.

Parameters

string $scalar:

int $flags:

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

2 calls to Inline::evaluateScalar()
Inline::parseMapping in vendor/symfony/yaml/Inline.php
Parses a YAML mapping.
Inline::parseScalar in vendor/symfony/yaml/Inline.php
Parses a YAML scalar.

File

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

Class

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

Namespace

Symfony\Component\Yaml

Code

private static function evaluateScalar($scalar, $flags, $references = []) {
  $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.', self::$parsedLineNumber + 1, $value, self::$parsedFilename);
    }
    if (!\array_key_exists($value, $references)) {
      throw new ParseException(sprintf('Reference "%s" does not exist.', $value), self::$parsedLineNumber + 1, $value, self::$parsedFilename);
    }
    return $references[$value];
  }
  switch (true) {
    case 'null' === $scalarLower:
    case '' === $scalar:
    case '~' === $scalar:
      return;
    case 'true' === $scalarLower:
      return true;
    case 'false' === $scalarLower:
      return false;
    case '!' === $scalar[0]:
      switch (true) {
        case 0 === strpos($scalar, '!str'):
          @trigger_error(self::getDeprecationMessage('Support for the !str tag is deprecated since Symfony 3.4. Use the !!str tag instead.'), E_USER_DEPRECATED);
          return (string) substr($scalar, 5);
        case 0 === strpos($scalar, '!!str '):
          return (string) substr($scalar, 6);
        case 0 === strpos($scalar, '! '):
          @trigger_error(self::getDeprecationMessage('Using the non-specific tag "!" is deprecated since Symfony 3.4 as its behavior will change in 4.0. It will force non-evaluating your values in 4.0. Use plain integers or !!float instead.'), E_USER_DEPRECATED);
          return (int) self::parseScalar(substr($scalar, 2), $flags);
        case 0 === strpos($scalar, '!php/object:'):
          if (self::$objectSupport) {
            @trigger_error(self::getDeprecationMessage('The !php/object: tag to indicate dumped PHP objects is deprecated since Symfony 3.4 and will be removed in 4.0. Use the !php/object (without the colon) tag instead.'), E_USER_DEPRECATED);
            return unserialize(substr($scalar, 12));
          }
          if (self::$exceptionOnInvalidType) {
            throw new ParseException('Object support when parsing a YAML file has been disabled.', self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
          }
          return;
        case 0 === strpos($scalar, '!!php/object:'):
          if (self::$objectSupport) {
            @trigger_error(self::getDeprecationMessage('The !!php/object: tag to indicate dumped PHP objects is deprecated since Symfony 3.1 and will be removed in 4.0. Use the !php/object (without the colon) tag instead.'), E_USER_DEPRECATED);
            return unserialize(substr($scalar, 13));
          }
          if (self::$exceptionOnInvalidType) {
            throw new ParseException('Object support when parsing a YAML file has been disabled.', self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
          }
          return;
        case 0 === strpos($scalar, '!php/object'):
          if (self::$objectSupport) {
            return unserialize(self::parseScalar(substr($scalar, 12)));
          }
          if (self::$exceptionOnInvalidType) {
            throw new ParseException('Object support when parsing a YAML file has been disabled.', self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
          }
          return;
        case 0 === strpos($scalar, '!php/const:'):
          if (self::$constantSupport) {
            @trigger_error(self::getDeprecationMessage('The !php/const: tag to indicate dumped PHP constants is deprecated since Symfony 3.4 and will be removed in 4.0. Use the !php/const (without the colon) tag instead.'), E_USER_DEPRECATED);
            if (\defined($const = substr($scalar, 11))) {
              return \constant($const);
            }
            throw new ParseException(sprintf('The constant "%s" is not defined.', $const), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
          }
          if (self::$exceptionOnInvalidType) {
            throw new ParseException(sprintf('The string "%s" could not be parsed as a constant. Have you forgotten to pass the "Yaml::PARSE_CONSTANT" flag to the parser?', $scalar), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
          }
          return;
        case 0 === strpos($scalar, '!php/const'):
          if (self::$constantSupport) {
            $i = 0;
            if (\defined($const = self::parseScalar(substr($scalar, 11), 0, null, $i, false))) {
              return \constant($const);
            }
            throw new ParseException(sprintf('The constant "%s" is not defined.', $const), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
          }
          if (self::$exceptionOnInvalidType) {
            throw new ParseException(sprintf('The string "%s" could not be parsed as a constant. Have you forgotten to pass the "Yaml::PARSE_CONSTANT" flag to the parser?', $scalar), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
          }
          return;
        case 0 === strpos($scalar, '!!float '):
          return (double) substr($scalar, 8);
        case 0 === strpos($scalar, '!!binary '):
          return self::evaluateBinaryScalar(substr($scalar, 9));
        default:
          @trigger_error(self::getDeprecationMessage(sprintf('Using the unquoted scalar value "%s" is deprecated since Symfony 3.3 and will be considered as a tagged value in 4.0. You must quote it.', $scalar)), E_USER_DEPRECATED);
      }

    // Optimize for returning strings.
    // no break
    case '+' === $scalar[0] || '-' === $scalar[0] || '.' === $scalar[0] || is_numeric($scalar[0]):
      switch (true) {
        case Parser::preg_match('{^[+-]?[0-9][0-9_]*$}', $scalar):
          $scalar = str_replace('_', '', (string) $scalar);

        // omitting the break / return as integers are handled in the next case
        // no break
        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):
          $scalar = str_replace('_', '', $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,]*(\\.[0-9_]+)?$/', $scalar):
        case Parser::preg_match('/^(-|\\+)?[0-9][0-9_]*(\\.[0-9_]+)?$/', $scalar):
          if (false !== strpos($scalar, ',')) {
            @trigger_error(self::getDeprecationMessage('Using the comma as a group separator for floats is deprecated since Symfony 3.2 and will be removed in 4.0.'), E_USER_DEPRECATED);
          }
          return (double) str_replace([
            ',',
            '_',
          ], '', $scalar);
        case Parser::preg_match(self::getTimestampRegex(), $scalar):
          if (Yaml::PARSE_DATETIME & $flags) {

            // When no timezone is provided in the parsed date, YAML spec says we must assume UTC.
            return new \DateTime($scalar, new \DateTimeZone('UTC'));
          }
          $timeZone = date_default_timezone_get();
          date_default_timezone_set('UTC');
          $time = strtotime($scalar);
          date_default_timezone_set($timeZone);
          return $time;
      }
  }
  return (string) $scalar;
}