Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Yaml; Chris@0: Chris@0: use Symfony\Component\Yaml\Exception\ParseException; Chris@0: use Symfony\Component\Yaml\Exception\DumpException; Chris@0: Chris@0: /** Chris@0: * Inline implements a YAML parser/dumper for the YAML inline syntax. Chris@0: * Chris@0: * @author Fabien Potencier Chris@0: * Chris@0: * @internal Chris@0: */ Chris@0: class Inline Chris@0: { Chris@0: const REGEX_QUOTED_STRING = '(?:"([^"\\\\]*+(?:\\\\.[^"\\\\]*+)*+)"|\'([^\']*+(?:\'\'[^\']*+)*+)\')'; Chris@0: Chris@0: public static $parsedLineNumber; Chris@0: Chris@0: private static $exceptionOnInvalidType = false; Chris@0: private static $objectSupport = false; Chris@0: private static $objectForMap = false; Chris@0: private static $constantSupport = false; Chris@0: Chris@0: /** Chris@0: * Converts a YAML string to a PHP value. Chris@0: * Chris@0: * @param string $value A YAML string Chris@0: * @param int $flags A bit field of PARSE_* constants to customize the YAML parser behavior Chris@0: * @param array $references Mapping of variable names to values Chris@0: * Chris@0: * @return mixed A PHP value Chris@0: * Chris@0: * @throws ParseException Chris@0: */ Chris@0: public static function parse($value, $flags = 0, $references = array()) Chris@0: { Chris@0: if (is_bool($flags)) { Chris@0: @trigger_error('Passing a boolean flag to toggle exception handling is deprecated since version 3.1 and will be removed in 4.0. Use the Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE flag instead.', E_USER_DEPRECATED); Chris@0: Chris@0: if ($flags) { Chris@0: $flags = Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE; Chris@0: } else { Chris@0: $flags = 0; Chris@0: } Chris@0: } Chris@0: Chris@0: if (func_num_args() >= 3 && !is_array($references)) { Chris@0: @trigger_error('Passing a boolean flag to toggle object support is deprecated since version 3.1 and will be removed in 4.0. Use the Yaml::PARSE_OBJECT flag instead.', E_USER_DEPRECATED); Chris@0: Chris@0: if ($references) { Chris@0: $flags |= Yaml::PARSE_OBJECT; Chris@0: } Chris@0: Chris@0: if (func_num_args() >= 4) { Chris@0: @trigger_error('Passing a boolean flag to toggle object for map support is deprecated since version 3.1 and will be removed in 4.0. Use the Yaml::PARSE_OBJECT_FOR_MAP flag instead.', E_USER_DEPRECATED); Chris@0: Chris@0: if (func_get_arg(3)) { Chris@0: $flags |= Yaml::PARSE_OBJECT_FOR_MAP; Chris@0: } Chris@0: } Chris@0: Chris@0: if (func_num_args() >= 5) { Chris@0: $references = func_get_arg(4); Chris@0: } else { Chris@0: $references = array(); Chris@0: } Chris@0: } Chris@0: Chris@0: self::$exceptionOnInvalidType = (bool) (Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE & $flags); Chris@0: self::$objectSupport = (bool) (Yaml::PARSE_OBJECT & $flags); Chris@0: self::$objectForMap = (bool) (Yaml::PARSE_OBJECT_FOR_MAP & $flags); Chris@0: self::$constantSupport = (bool) (Yaml::PARSE_CONSTANT & $flags); Chris@0: Chris@0: $value = trim($value); Chris@0: Chris@0: if ('' === $value) { Chris@0: return ''; Chris@0: } Chris@0: Chris@0: if (2 /* MB_OVERLOAD_STRING */ & (int) ini_get('mbstring.func_overload')) { Chris@0: $mbEncoding = mb_internal_encoding(); Chris@0: mb_internal_encoding('ASCII'); Chris@0: } Chris@0: Chris@0: $i = 0; Chris@0: switch ($value[0]) { Chris@0: case '[': Chris@0: $result = self::parseSequence($value, $flags, $i, $references); Chris@0: ++$i; Chris@0: break; Chris@0: case '{': Chris@0: $result = self::parseMapping($value, $flags, $i, $references); Chris@0: ++$i; Chris@0: break; Chris@0: default: Chris@0: $result = self::parseScalar($value, $flags, null, array('"', "'"), $i, true, $references); Chris@0: } Chris@0: Chris@0: // some comments are allowed at the end Chris@0: if (preg_replace('/\s+#.*$/A', '', substr($value, $i))) { Chris@0: throw new ParseException(sprintf('Unexpected characters near "%s".', substr($value, $i))); Chris@0: } Chris@0: Chris@0: if (isset($mbEncoding)) { Chris@0: mb_internal_encoding($mbEncoding); Chris@0: } Chris@0: Chris@0: return $result; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Dumps a given PHP variable to a YAML string. Chris@0: * Chris@0: * @param mixed $value The PHP variable to convert Chris@0: * @param int $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string Chris@0: * Chris@0: * @return string The YAML string representing the PHP value Chris@0: * Chris@0: * @throws DumpException When trying to dump PHP resource Chris@0: */ Chris@0: public static function dump($value, $flags = 0) Chris@0: { Chris@0: if (is_bool($flags)) { Chris@0: @trigger_error('Passing a boolean flag to toggle exception handling is deprecated since version 3.1 and will be removed in 4.0. Use the Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE flag instead.', E_USER_DEPRECATED); Chris@0: Chris@0: if ($flags) { Chris@0: $flags = Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE; Chris@0: } else { Chris@0: $flags = 0; Chris@0: } Chris@0: } Chris@0: Chris@0: if (func_num_args() >= 3) { Chris@0: @trigger_error('Passing a boolean flag to toggle object support is deprecated since version 3.1 and will be removed in 4.0. Use the Yaml::DUMP_OBJECT flag instead.', E_USER_DEPRECATED); Chris@0: Chris@0: if (func_get_arg(2)) { Chris@0: $flags |= Yaml::DUMP_OBJECT; Chris@0: } Chris@0: } Chris@0: Chris@0: switch (true) { Chris@0: case is_resource($value): Chris@0: if (Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE & $flags) { Chris@0: throw new DumpException(sprintf('Unable to dump PHP resources in a YAML file ("%s").', get_resource_type($value))); Chris@0: } Chris@0: Chris@0: return 'null'; Chris@0: case $value instanceof \DateTimeInterface: Chris@0: return $value->format('c'); Chris@0: case is_object($value): Chris@0: if (Yaml::DUMP_OBJECT & $flags) { Chris@0: return '!php/object:'.serialize($value); Chris@0: } Chris@0: Chris@0: if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($value instanceof \stdClass || $value instanceof \ArrayObject)) { Chris@0: return self::dumpArray((array) $value, $flags); Chris@0: } Chris@0: Chris@0: if (Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE & $flags) { Chris@0: throw new DumpException('Object support when dumping a YAML file has been disabled.'); Chris@0: } Chris@0: Chris@0: return 'null'; Chris@0: case is_array($value): Chris@0: return self::dumpArray($value, $flags); Chris@0: case null === $value: Chris@0: return 'null'; Chris@0: case true === $value: Chris@0: return 'true'; Chris@0: case false === $value: Chris@0: return 'false'; Chris@0: case ctype_digit($value): Chris@0: return is_string($value) ? "'$value'" : (int) $value; Chris@0: case is_numeric($value): Chris@0: $locale = setlocale(LC_NUMERIC, 0); Chris@0: if (false !== $locale) { Chris@0: setlocale(LC_NUMERIC, 'C'); Chris@0: } Chris@0: if (is_float($value)) { Chris@0: $repr = (string) $value; Chris@0: if (is_infinite($value)) { Chris@0: $repr = str_ireplace('INF', '.Inf', $repr); Chris@0: } elseif (floor($value) == $value && $repr == $value) { Chris@0: // Preserve float data type since storing a whole number will result in integer value. Chris@0: $repr = '!!float '.$repr; Chris@0: } Chris@0: } else { Chris@0: $repr = is_string($value) ? "'$value'" : (string) $value; Chris@0: } Chris@0: if (false !== $locale) { Chris@0: setlocale(LC_NUMERIC, $locale); Chris@0: } Chris@0: Chris@0: return $repr; Chris@0: case '' == $value: Chris@0: return "''"; Chris@0: case self::isBinaryString($value): Chris@0: return '!!binary '.base64_encode($value); Chris@0: case Escaper::requiresDoubleQuoting($value): Chris@0: return Escaper::escapeWithDoubleQuotes($value); Chris@0: case Escaper::requiresSingleQuoting($value): Chris@0: case Parser::preg_match('{^[0-9]+[_0-9]*$}', $value): Chris@0: case Parser::preg_match(self::getHexRegex(), $value): Chris@0: case Parser::preg_match(self::getTimestampRegex(), $value): Chris@0: return Escaper::escapeWithSingleQuotes($value); Chris@0: default: Chris@0: return $value; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Check if given array is hash or just normal indexed array. Chris@0: * Chris@0: * @internal Chris@0: * Chris@0: * @param array $value The PHP array to check Chris@0: * Chris@0: * @return bool true if value is hash array, false otherwise Chris@0: */ Chris@0: public static function isHash(array $value) Chris@0: { Chris@0: $expectedKey = 0; Chris@0: Chris@0: foreach ($value as $key => $val) { Chris@0: if ($key !== $expectedKey++) { Chris@0: return true; Chris@0: } Chris@0: } Chris@0: Chris@0: return false; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Dumps a PHP array to a YAML string. Chris@0: * Chris@0: * @param array $value The PHP array to dump Chris@0: * @param int $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string Chris@0: * Chris@0: * @return string The YAML string representing the PHP array Chris@0: */ Chris@0: private static function dumpArray($value, $flags) Chris@0: { Chris@0: // array Chris@0: if ($value && !self::isHash($value)) { Chris@0: $output = array(); Chris@0: foreach ($value as $val) { Chris@0: $output[] = self::dump($val, $flags); Chris@0: } Chris@0: Chris@0: return sprintf('[%s]', implode(', ', $output)); Chris@0: } Chris@0: Chris@0: // hash Chris@0: $output = array(); Chris@0: foreach ($value as $key => $val) { Chris@0: $output[] = sprintf('%s: %s', self::dump($key, $flags), self::dump($val, $flags)); Chris@0: } Chris@0: Chris@0: return sprintf('{ %s }', implode(', ', $output)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Parses a YAML scalar. Chris@0: * Chris@0: * @param string $scalar Chris@0: * @param int $flags Chris@0: * @param string[] $delimiters Chris@0: * @param string[] $stringDelimiters Chris@0: * @param int &$i Chris@0: * @param bool $evaluate Chris@0: * @param array $references Chris@0: * Chris@0: * @return string Chris@0: * Chris@0: * @throws ParseException When malformed inline YAML string is parsed Chris@0: * Chris@0: * @internal Chris@0: */ Chris@0: public static function parseScalar($scalar, $flags = 0, $delimiters = null, $stringDelimiters = array('"', "'"), &$i = 0, $evaluate = true, $references = array()) Chris@0: { Chris@0: if (in_array($scalar[$i], $stringDelimiters)) { Chris@0: // quoted scalar Chris@0: $output = self::parseQuotedScalar($scalar, $i); Chris@0: Chris@0: if (null !== $delimiters) { Chris@0: $tmp = ltrim(substr($scalar, $i), ' '); Chris@0: if (!in_array($tmp[0], $delimiters)) { Chris@0: throw new ParseException(sprintf('Unexpected characters (%s).', substr($scalar, $i))); Chris@0: } Chris@0: } Chris@0: } else { Chris@0: // "normal" string Chris@0: if (!$delimiters) { Chris@0: $output = substr($scalar, $i); Chris@0: $i += strlen($output); Chris@0: Chris@0: // remove comments Chris@0: if (Parser::preg_match('/[ \t]+#/', $output, $match, PREG_OFFSET_CAPTURE)) { Chris@0: $output = substr($output, 0, $match[0][1]); Chris@0: } Chris@0: } elseif (Parser::preg_match('/^(.+?)('.implode('|', $delimiters).')/', substr($scalar, $i), $match)) { Chris@0: $output = $match[1]; Chris@0: $i += strlen($output); Chris@0: } else { Chris@0: throw new ParseException(sprintf('Malformed inline YAML string: %s.', $scalar)); Chris@0: } Chris@0: Chris@0: // a non-quoted string cannot start with @ or ` (reserved) nor with a scalar indicator (| or >) Chris@0: if ($output && ('@' === $output[0] || '`' === $output[0] || '|' === $output[0] || '>' === $output[0])) { Chris@0: throw new ParseException(sprintf('The reserved indicator "%s" cannot start a plain scalar; you need to quote the scalar.', $output[0])); Chris@0: } Chris@0: Chris@0: if ($output && '%' === $output[0]) { Chris@0: @trigger_error(sprintf('Not quoting the scalar "%s" starting with the "%%" indicator character is deprecated since Symfony 3.1 and will throw a ParseException in 4.0.', $output), E_USER_DEPRECATED); Chris@0: } Chris@0: Chris@0: if ($evaluate) { Chris@0: $output = self::evaluateScalar($output, $flags, $references); Chris@0: } Chris@0: } Chris@0: Chris@0: return $output; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Parses a YAML quoted scalar. Chris@0: * Chris@0: * @param string $scalar Chris@0: * @param int &$i Chris@0: * Chris@0: * @return string Chris@0: * Chris@0: * @throws ParseException When malformed inline YAML string is parsed Chris@0: */ Chris@0: private static function parseQuotedScalar($scalar, &$i) Chris@0: { Chris@0: if (!Parser::preg_match('/'.self::REGEX_QUOTED_STRING.'/Au', substr($scalar, $i), $match)) { Chris@0: throw new ParseException(sprintf('Malformed inline YAML string: %s.', substr($scalar, $i))); Chris@0: } Chris@0: Chris@0: $output = substr($match[0], 1, strlen($match[0]) - 2); Chris@0: Chris@0: $unescaper = new Unescaper(); Chris@0: if ('"' == $scalar[$i]) { Chris@0: $output = $unescaper->unescapeDoubleQuotedString($output); Chris@0: } else { Chris@0: $output = $unescaper->unescapeSingleQuotedString($output); Chris@0: } Chris@0: Chris@0: $i += strlen($match[0]); Chris@0: Chris@0: return $output; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Parses a YAML sequence. Chris@0: * Chris@0: * @param string $sequence Chris@0: * @param int $flags Chris@0: * @param int &$i Chris@0: * @param array $references Chris@0: * Chris@0: * @return array Chris@0: * Chris@0: * @throws ParseException When malformed inline YAML string is parsed Chris@0: */ Chris@0: private static function parseSequence($sequence, $flags, &$i = 0, $references = array()) Chris@0: { Chris@0: $output = array(); Chris@0: $len = strlen($sequence); Chris@0: ++$i; Chris@0: Chris@0: // [foo, bar, ...] Chris@0: while ($i < $len) { Chris@0: switch ($sequence[$i]) { Chris@0: case '[': Chris@0: // nested sequence Chris@0: $output[] = self::parseSequence($sequence, $flags, $i, $references); Chris@0: break; Chris@0: case '{': Chris@0: // nested mapping Chris@0: $output[] = self::parseMapping($sequence, $flags, $i, $references); Chris@0: break; Chris@0: case ']': Chris@0: return $output; Chris@0: case ',': Chris@0: case ' ': Chris@0: break; Chris@0: default: Chris@0: $isQuoted = in_array($sequence[$i], array('"', "'")); Chris@0: $value = self::parseScalar($sequence, $flags, array(',', ']'), array('"', "'"), $i, true, $references); Chris@0: Chris@0: // the value can be an array if a reference has been resolved to an array var Chris@0: if (is_string($value) && !$isQuoted && false !== strpos($value, ': ')) { Chris@0: // embedded mapping? Chris@0: try { Chris@0: $pos = 0; Chris@0: $value = self::parseMapping('{'.$value.'}', $flags, $pos, $references); Chris@0: } catch (\InvalidArgumentException $e) { Chris@0: // no, it's not Chris@0: } Chris@0: } Chris@0: Chris@0: $output[] = $value; Chris@0: Chris@0: --$i; Chris@0: } Chris@0: Chris@0: ++$i; Chris@0: } Chris@0: Chris@0: throw new ParseException(sprintf('Malformed inline YAML string: %s.', $sequence)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Parses a YAML mapping. Chris@0: * Chris@0: * @param string $mapping Chris@0: * @param int $flags Chris@0: * @param int &$i Chris@0: * @param array $references Chris@0: * Chris@0: * @return array|\stdClass Chris@0: * Chris@0: * @throws ParseException When malformed inline YAML string is parsed Chris@0: */ Chris@0: private static function parseMapping($mapping, $flags, &$i = 0, $references = array()) Chris@0: { Chris@0: $output = array(); Chris@0: $len = strlen($mapping); Chris@0: ++$i; Chris@0: Chris@0: // {foo: bar, bar:foo, ...} Chris@0: while ($i < $len) { Chris@0: switch ($mapping[$i]) { Chris@0: case ' ': Chris@0: case ',': Chris@0: ++$i; Chris@0: continue 2; Chris@0: case '}': Chris@0: if (self::$objectForMap) { Chris@0: return (object) $output; Chris@0: } Chris@0: Chris@0: return $output; Chris@0: } Chris@0: Chris@0: // key Chris@0: $key = self::parseScalar($mapping, $flags, array(':', ' '), array('"', "'"), $i, false); Chris@0: Chris@0: if (':' !== $key && false === $i = strpos($mapping, ':', $i)) { Chris@0: break; Chris@0: } Chris@0: Chris@0: if (':' !== $key && (!isset($mapping[$i + 1]) || !in_array($mapping[$i + 1], array(' ', ',', '[', ']', '{', '}'), true))) { Chris@0: @trigger_error('Using a colon that is not followed by an indication character (i.e. " ", ",", "[", "]", "{", "}" is deprecated since version 3.2 and will throw a ParseException in 4.0.', E_USER_DEPRECATED); Chris@0: } Chris@0: Chris@0: // value Chris@0: $done = false; Chris@0: Chris@0: while ($i < $len) { Chris@0: switch ($mapping[$i]) { Chris@0: case '[': Chris@0: // nested sequence Chris@0: $value = self::parseSequence($mapping, $flags, $i, $references); Chris@0: // Spec: Keys MUST be unique; first one wins. Chris@0: // Parser cannot abort this mapping earlier, since lines Chris@0: // are processed sequentially. Chris@0: if (!isset($output[$key])) { Chris@0: $output[$key] = $value; Chris@0: } else { Chris@0: @trigger_error(sprintf('Duplicate key "%s" detected on line %d whilst parsing YAML. Silent handling of duplicate mapping keys in YAML is deprecated since version 3.2 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0.', $key, self::$parsedLineNumber + 1), E_USER_DEPRECATED); Chris@0: } Chris@0: $done = true; Chris@0: break; Chris@0: case '{': Chris@0: // nested mapping Chris@0: $value = self::parseMapping($mapping, $flags, $i, $references); Chris@0: // Spec: Keys MUST be unique; first one wins. Chris@0: // Parser cannot abort this mapping earlier, since lines Chris@0: // are processed sequentially. Chris@0: if (!isset($output[$key])) { Chris@0: $output[$key] = $value; Chris@0: } else { Chris@0: @trigger_error(sprintf('Duplicate key "%s" detected on line %d whilst parsing YAML. Silent handling of duplicate mapping keys in YAML is deprecated since version 3.2 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0.', $key, self::$parsedLineNumber + 1), E_USER_DEPRECATED); Chris@0: } Chris@0: $done = true; Chris@0: break; Chris@0: case ':': Chris@0: case ' ': Chris@0: break; Chris@0: default: Chris@0: $value = self::parseScalar($mapping, $flags, array(',', '}'), array('"', "'"), $i, true, $references); Chris@0: // Spec: Keys MUST be unique; first one wins. Chris@0: // Parser cannot abort this mapping earlier, since lines Chris@0: // are processed sequentially. Chris@0: if (!isset($output[$key])) { Chris@0: $output[$key] = $value; Chris@0: } else { Chris@0: @trigger_error(sprintf('Duplicate key "%s" detected on line %d whilst parsing YAML. Silent handling of duplicate mapping keys in YAML is deprecated since version 3.2 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0.', $key, self::$parsedLineNumber + 1), E_USER_DEPRECATED); Chris@0: } Chris@0: $done = true; Chris@0: --$i; Chris@0: } Chris@0: Chris@0: ++$i; Chris@0: Chris@0: if ($done) { Chris@0: continue 2; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: throw new ParseException(sprintf('Malformed inline YAML string: %s.', $mapping)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Evaluates scalars and replaces magic values. Chris@0: * Chris@0: * @param string $scalar Chris@0: * @param int $flags Chris@0: * @param array $references Chris@0: * Chris@0: * @return mixed The evaluated YAML string Chris@0: * Chris@0: * @throws ParseException when object parsing support was disabled and the parser detected a PHP object or when a reference could not be resolved Chris@0: */ Chris@0: private static function evaluateScalar($scalar, $flags, $references = array()) Chris@0: { Chris@0: $scalar = trim($scalar); Chris@0: $scalarLower = strtolower($scalar); Chris@0: Chris@0: if (0 === strpos($scalar, '*')) { Chris@0: if (false !== $pos = strpos($scalar, '#')) { Chris@0: $value = substr($scalar, 1, $pos - 2); Chris@0: } else { Chris@0: $value = substr($scalar, 1); Chris@0: } Chris@0: Chris@0: // an unquoted * Chris@0: if (false === $value || '' === $value) { Chris@0: throw new ParseException('A reference must contain at least one character.'); Chris@0: } Chris@0: Chris@0: if (!array_key_exists($value, $references)) { Chris@0: throw new ParseException(sprintf('Reference "%s" does not exist.', $value)); Chris@0: } Chris@0: Chris@0: return $references[$value]; Chris@0: } Chris@0: Chris@0: switch (true) { Chris@0: case 'null' === $scalarLower: Chris@0: case '' === $scalar: Chris@0: case '~' === $scalar: Chris@0: return; Chris@0: case 'true' === $scalarLower: Chris@0: return true; Chris@0: case 'false' === $scalarLower: Chris@0: return false; Chris@0: // Optimise for returning strings. Chris@0: case $scalar[0] === '+' || $scalar[0] === '-' || $scalar[0] === '.' || $scalar[0] === '!' || is_numeric($scalar[0]): Chris@0: switch (true) { Chris@0: case 0 === strpos($scalar, '!str'): Chris@0: return (string) substr($scalar, 5); Chris@0: case 0 === strpos($scalar, '! '): Chris@0: return (int) self::parseScalar(substr($scalar, 2), $flags); Chris@0: case 0 === strpos($scalar, '!php/object:'): Chris@0: if (self::$objectSupport) { Chris@0: return unserialize(substr($scalar, 12)); Chris@0: } Chris@0: Chris@0: if (self::$exceptionOnInvalidType) { Chris@0: throw new ParseException('Object support when parsing a YAML file has been disabled.'); Chris@0: } Chris@0: Chris@0: return; Chris@0: case 0 === strpos($scalar, '!!php/object:'): Chris@0: if (self::$objectSupport) { Chris@0: @trigger_error('The !!php/object tag to indicate dumped PHP objects is deprecated since version 3.1 and will be removed in 4.0. Use the !php/object tag instead.', E_USER_DEPRECATED); Chris@0: Chris@0: return unserialize(substr($scalar, 13)); Chris@0: } Chris@0: Chris@0: if (self::$exceptionOnInvalidType) { Chris@0: throw new ParseException('Object support when parsing a YAML file has been disabled.'); Chris@0: } Chris@0: Chris@0: return; Chris@0: case 0 === strpos($scalar, '!php/const:'): Chris@0: if (self::$constantSupport) { Chris@0: if (defined($const = substr($scalar, 11))) { Chris@0: return constant($const); Chris@0: } Chris@0: Chris@0: throw new ParseException(sprintf('The constant "%s" is not defined.', $const)); Chris@0: } Chris@0: if (self::$exceptionOnInvalidType) { Chris@0: 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)); Chris@0: } Chris@0: Chris@0: return; Chris@0: case 0 === strpos($scalar, '!!float '): Chris@0: return (float) substr($scalar, 8); Chris@0: case Parser::preg_match('{^[+-]?[0-9][0-9_]*$}', $scalar): Chris@0: $scalar = str_replace('_', '', (string) $scalar); Chris@0: // omitting the break / return as integers are handled in the next case Chris@0: case ctype_digit($scalar): Chris@0: $raw = $scalar; Chris@0: $cast = (int) $scalar; Chris@0: Chris@0: return '0' == $scalar[0] ? octdec($scalar) : (((string) $raw == (string) $cast) ? $cast : $raw); Chris@0: case '-' === $scalar[0] && ctype_digit(substr($scalar, 1)): Chris@0: $raw = $scalar; Chris@0: $cast = (int) $scalar; Chris@0: Chris@0: return '0' == $scalar[1] ? octdec($scalar) : (((string) $raw === (string) $cast) ? $cast : $raw); Chris@0: case is_numeric($scalar): Chris@0: case Parser::preg_match(self::getHexRegex(), $scalar): Chris@0: $scalar = str_replace('_', '', $scalar); Chris@0: Chris@0: return '0x' === $scalar[0].$scalar[1] ? hexdec($scalar) : (float) $scalar; Chris@0: case '.inf' === $scalarLower: Chris@0: case '.nan' === $scalarLower: Chris@0: return -log(0); Chris@0: case '-.inf' === $scalarLower: Chris@0: return log(0); Chris@0: case 0 === strpos($scalar, '!!binary '): Chris@0: return self::evaluateBinaryScalar(substr($scalar, 9)); Chris@0: case Parser::preg_match('/^(-|\+)?[0-9][0-9,]*(\.[0-9_]+)?$/', $scalar): Chris@0: case Parser::preg_match('/^(-|\+)?[0-9][0-9_]*(\.[0-9_]+)?$/', $scalar): Chris@0: if (false !== strpos($scalar, ',')) { Chris@0: @trigger_error('Using the comma as a group separator for floats is deprecated since version 3.2 and will be removed in 4.0.', E_USER_DEPRECATED); Chris@0: } Chris@0: Chris@0: return (float) str_replace(array(',', '_'), '', $scalar); Chris@0: case Parser::preg_match(self::getTimestampRegex(), $scalar): Chris@0: if (Yaml::PARSE_DATETIME & $flags) { Chris@0: // When no timezone is provided in the parsed date, YAML spec says we must assume UTC. Chris@0: return new \DateTime($scalar, new \DateTimeZone('UTC')); Chris@0: } Chris@0: Chris@0: $timeZone = date_default_timezone_get(); Chris@0: date_default_timezone_set('UTC'); Chris@0: $time = strtotime($scalar); Chris@0: date_default_timezone_set($timeZone); Chris@0: Chris@0: return $time; Chris@0: } Chris@0: default: Chris@0: return (string) $scalar; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * @param string $scalar Chris@0: * Chris@0: * @return string Chris@0: * Chris@0: * @internal Chris@0: */ Chris@0: public static function evaluateBinaryScalar($scalar) Chris@0: { Chris@0: $parsedBinaryData = self::parseScalar(preg_replace('/\s/', '', $scalar)); Chris@0: Chris@0: if (0 !== (strlen($parsedBinaryData) % 4)) { Chris@0: throw new ParseException(sprintf('The normalized base64 encoded data (data without whitespace characters) length must be a multiple of four (%d bytes given).', strlen($parsedBinaryData))); Chris@0: } Chris@0: Chris@0: if (!Parser::preg_match('#^[A-Z0-9+/]+={0,2}$#i', $parsedBinaryData)) { Chris@0: throw new ParseException(sprintf('The base64 encoded data (%s) contains invalid characters.', $parsedBinaryData)); Chris@0: } Chris@0: Chris@0: return base64_decode($parsedBinaryData, true); Chris@0: } Chris@0: Chris@0: private static function isBinaryString($value) Chris@0: { Chris@0: return !preg_match('//u', $value) || preg_match('/[^\x00\x07-\x0d\x1B\x20-\xff]/', $value); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets a regex that matches a YAML date. Chris@0: * Chris@0: * @return string The regular expression Chris@0: * Chris@0: * @see http://www.yaml.org/spec/1.2/spec.html#id2761573 Chris@0: */ Chris@0: private static function getTimestampRegex() Chris@0: { Chris@0: return <<[0-9][0-9][0-9][0-9]) Chris@0: -(?P[0-9][0-9]?) Chris@0: -(?P[0-9][0-9]?) Chris@0: (?:(?:[Tt]|[ \t]+) Chris@0: (?P[0-9][0-9]?) Chris@0: :(?P[0-9][0-9]) Chris@0: :(?P[0-9][0-9]) Chris@0: (?:\.(?P[0-9]*))? Chris@0: (?:[ \t]*(?PZ|(?P[-+])(?P[0-9][0-9]?) Chris@0: (?::(?P[0-9][0-9]))?))?)? Chris@0: $~x Chris@0: EOF; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets a regex that matches a YAML number in hexadecimal notation. Chris@0: * Chris@0: * @return string Chris@0: */ Chris@0: private static function getHexRegex() Chris@0: { Chris@0: return '~^0x[0-9a-f_]++$~i'; Chris@0: } Chris@0: }