4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Yaml;
14 use Symfony\Component\Yaml\Exception\ParseException;
15 use Symfony\Component\Yaml\Exception\DumpException;
18 * Inline implements a YAML parser/dumper for the YAML inline syntax.
20 * @author Fabien Potencier <fabien@symfony.com>
26 const REGEX_QUOTED_STRING = '(?:"([^"\\\\]*+(?:\\\\.[^"\\\\]*+)*+)"|\'([^\']*+(?:\'\'[^\']*+)*+)\')';
28 public static $parsedLineNumber;
30 private static $exceptionOnInvalidType = false;
31 private static $objectSupport = false;
32 private static $objectForMap = false;
33 private static $constantSupport = false;
36 * Converts a YAML string to a PHP value.
38 * @param string $value A YAML string
39 * @param int $flags A bit field of PARSE_* constants to customize the YAML parser behavior
40 * @param array $references Mapping of variable names to values
42 * @return mixed A PHP value
44 * @throws ParseException
46 public static function parse($value, $flags = 0, $references = array())
48 if (is_bool($flags)) {
49 @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);
52 $flags = Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE;
58 if (func_num_args() >= 3 && !is_array($references)) {
59 @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);
62 $flags |= Yaml::PARSE_OBJECT;
65 if (func_num_args() >= 4) {
66 @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);
68 if (func_get_arg(3)) {
69 $flags |= Yaml::PARSE_OBJECT_FOR_MAP;
73 if (func_num_args() >= 5) {
74 $references = func_get_arg(4);
76 $references = array();
80 self::$exceptionOnInvalidType = (bool) (Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE & $flags);
81 self::$objectSupport = (bool) (Yaml::PARSE_OBJECT & $flags);
82 self::$objectForMap = (bool) (Yaml::PARSE_OBJECT_FOR_MAP & $flags);
83 self::$constantSupport = (bool) (Yaml::PARSE_CONSTANT & $flags);
85 $value = trim($value);
91 if (2 /* MB_OVERLOAD_STRING */ & (int) ini_get('mbstring.func_overload')) {
92 $mbEncoding = mb_internal_encoding();
93 mb_internal_encoding('ASCII');
99 $result = self::parseSequence($value, $flags, $i, $references);
103 $result = self::parseMapping($value, $flags, $i, $references);
107 $result = self::parseScalar($value, $flags, null, array('"', "'"), $i, true, $references);
110 // some comments are allowed at the end
111 if (preg_replace('/\s+#.*$/A', '', substr($value, $i))) {
112 throw new ParseException(sprintf('Unexpected characters near "%s".', substr($value, $i)));
115 if (isset($mbEncoding)) {
116 mb_internal_encoding($mbEncoding);
123 * Dumps a given PHP variable to a YAML string.
125 * @param mixed $value The PHP variable to convert
126 * @param int $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string
128 * @return string The YAML string representing the PHP value
130 * @throws DumpException When trying to dump PHP resource
132 public static function dump($value, $flags = 0)
134 if (is_bool($flags)) {
135 @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);
138 $flags = Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE;
144 if (func_num_args() >= 3) {
145 @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);
147 if (func_get_arg(2)) {
148 $flags |= Yaml::DUMP_OBJECT;
153 case is_resource($value):
154 if (Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE & $flags) {
155 throw new DumpException(sprintf('Unable to dump PHP resources in a YAML file ("%s").', get_resource_type($value)));
159 case $value instanceof \DateTimeInterface:
160 return $value->format('c');
161 case is_object($value):
162 if (Yaml::DUMP_OBJECT & $flags) {
163 return '!php/object:'.serialize($value);
166 if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($value instanceof \stdClass || $value instanceof \ArrayObject)) {
167 return self::dumpArray($value, $flags);
170 if (Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE & $flags) {
171 throw new DumpException('Object support when dumping a YAML file has been disabled.');
175 case is_array($value):
176 return self::dumpArray($value, $flags);
177 case null === $value:
179 case true === $value:
181 case false === $value:
183 case ctype_digit($value):
184 return is_string($value) ? "'$value'" : (int) $value;
185 case is_numeric($value):
186 $locale = setlocale(LC_NUMERIC, 0);
187 if (false !== $locale) {
188 setlocale(LC_NUMERIC, 'C');
190 if (is_float($value)) {
191 $repr = (string) $value;
192 if (is_infinite($value)) {
193 $repr = str_ireplace('INF', '.Inf', $repr);
194 } elseif (floor($value) == $value && $repr == $value) {
195 // Preserve float data type since storing a whole number will result in integer value.
196 $repr = '!!float '.$repr;
199 $repr = is_string($value) ? "'$value'" : (string) $value;
201 if (false !== $locale) {
202 setlocale(LC_NUMERIC, $locale);
208 case self::isBinaryString($value):
209 return '!!binary '.base64_encode($value);
210 case Escaper::requiresDoubleQuoting($value):
211 return Escaper::escapeWithDoubleQuotes($value);
212 case Escaper::requiresSingleQuoting($value):
213 case Parser::preg_match('{^[0-9]+[_0-9]*$}', $value):
214 case Parser::preg_match(self::getHexRegex(), $value):
215 case Parser::preg_match(self::getTimestampRegex(), $value):
216 return Escaper::escapeWithSingleQuotes($value);
223 * Check if given array is hash or just normal indexed array.
227 * @param array|\ArrayObject|\stdClass $value The PHP array or array-like object to check
229 * @return bool true if value is hash array, false otherwise
231 public static function isHash($value)
233 if ($value instanceof \stdClass || $value instanceof \ArrayObject) {
239 foreach ($value as $key => $val) {
240 if ($key !== $expectedKey++) {
249 * Dumps a PHP array to a YAML string.
251 * @param array $value The PHP array to dump
252 * @param int $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string
254 * @return string The YAML string representing the PHP array
256 private static function dumpArray($value, $flags)
259 if ($value && !self::isHash($value)) {
261 foreach ($value as $val) {
262 $output[] = self::dump($val, $flags);
265 return sprintf('[%s]', implode(', ', $output));
270 foreach ($value as $key => $val) {
271 $output[] = sprintf('%s: %s', self::dump($key, $flags), self::dump($val, $flags));
274 return sprintf('{ %s }', implode(', ', $output));
278 * Parses a YAML scalar.
280 * @param string $scalar
282 * @param string[] $delimiters
283 * @param string[] $stringDelimiters
285 * @param bool $evaluate
286 * @param array $references
290 * @throws ParseException When malformed inline YAML string is parsed
294 public static function parseScalar($scalar, $flags = 0, $delimiters = null, $stringDelimiters = array('"', "'"), &$i = 0, $evaluate = true, $references = array())
296 if (in_array($scalar[$i], $stringDelimiters)) {
298 $output = self::parseQuotedScalar($scalar, $i);
300 if (null !== $delimiters) {
301 $tmp = ltrim(substr($scalar, $i), ' ');
302 if (!in_array($tmp[0], $delimiters)) {
303 throw new ParseException(sprintf('Unexpected characters (%s).', substr($scalar, $i)));
309 $output = substr($scalar, $i);
310 $i += strlen($output);
313 if (Parser::preg_match('/[ \t]+#/', $output, $match, PREG_OFFSET_CAPTURE)) {
314 $output = substr($output, 0, $match[0][1]);
316 } elseif (Parser::preg_match('/^(.+?)('.implode('|', $delimiters).')/', substr($scalar, $i), $match)) {
318 $i += strlen($output);
320 throw new ParseException(sprintf('Malformed inline YAML string: %s.', $scalar));
323 // a non-quoted string cannot start with @ or ` (reserved) nor with a scalar indicator (| or >)
324 if ($output && ('@' === $output[0] || '`' === $output[0] || '|' === $output[0] || '>' === $output[0])) {
325 throw new ParseException(sprintf('The reserved indicator "%s" cannot start a plain scalar; you need to quote the scalar.', $output[0]));
328 if ($output && '%' === $output[0]) {
329 @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);
333 $output = self::evaluateScalar($output, $flags, $references);
341 * Parses a YAML quoted scalar.
343 * @param string $scalar
348 * @throws ParseException When malformed inline YAML string is parsed
350 private static function parseQuotedScalar($scalar, &$i)
352 if (!Parser::preg_match('/'.self::REGEX_QUOTED_STRING.'/Au', substr($scalar, $i), $match)) {
353 throw new ParseException(sprintf('Malformed inline YAML string: %s.', substr($scalar, $i)));
356 $output = substr($match[0], 1, strlen($match[0]) - 2);
358 $unescaper = new Unescaper();
359 if ('"' == $scalar[$i]) {
360 $output = $unescaper->unescapeDoubleQuotedString($output);
362 $output = $unescaper->unescapeSingleQuotedString($output);
365 $i += strlen($match[0]);
371 * Parses a YAML sequence.
373 * @param string $sequence
376 * @param array $references
380 * @throws ParseException When malformed inline YAML string is parsed
382 private static function parseSequence($sequence, $flags, &$i = 0, $references = array())
385 $len = strlen($sequence);
390 switch ($sequence[$i]) {
393 $output[] = self::parseSequence($sequence, $flags, $i, $references);
397 $output[] = self::parseMapping($sequence, $flags, $i, $references);
405 $isQuoted = in_array($sequence[$i], array('"', "'"));
406 $value = self::parseScalar($sequence, $flags, array(',', ']'), array('"', "'"), $i, true, $references);
408 // the value can be an array if a reference has been resolved to an array var
409 if (is_string($value) && !$isQuoted && false !== strpos($value, ': ')) {
413 $value = self::parseMapping('{'.$value.'}', $flags, $pos, $references);
414 } catch (\InvalidArgumentException $e) {
427 throw new ParseException(sprintf('Malformed inline YAML string: %s.', $sequence));
431 * Parses a YAML mapping.
433 * @param string $mapping
436 * @param array $references
438 * @return array|\stdClass
440 * @throws ParseException When malformed inline YAML string is parsed
442 private static function parseMapping($mapping, $flags, &$i = 0, $references = array())
445 $len = strlen($mapping);
448 // {foo: bar, bar:foo, ...}
450 switch ($mapping[$i]) {
456 if (self::$objectForMap) {
457 return (object) $output;
464 $isKeyQuoted = in_array($mapping[$i], array('"', "'"), true);
465 $key = self::parseScalar($mapping, $flags, array(':', ' '), array('"', "'"), $i, false);
467 if (':' !== $key && false === $i = strpos($mapping, ':', $i)) {
471 if (':' !== $key && !$isKeyQuoted && (!isset($mapping[$i + 1]) || !in_array($mapping[$i + 1], array(' ', ',', '[', ']', '{', '}'), true))) {
472 @trigger_error('Using a colon after an unquoted mapping key 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);
479 switch ($mapping[$i]) {
482 $value = self::parseSequence($mapping, $flags, $i, $references);
483 // Spec: Keys MUST be unique; first one wins.
484 // Parser cannot abort this mapping earlier, since lines
485 // are processed sequentially.
486 if (!isset($output[$key])) {
487 $output[$key] = $value;
489 @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);
495 $value = self::parseMapping($mapping, $flags, $i, $references);
496 // Spec: Keys MUST be unique; first one wins.
497 // Parser cannot abort this mapping earlier, since lines
498 // are processed sequentially.
499 if (!isset($output[$key])) {
500 $output[$key] = $value;
502 @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);
510 $value = self::parseScalar($mapping, $flags, array(',', '}'), array('"', "'"), $i, true, $references);
511 // Spec: Keys MUST be unique; first one wins.
512 // Parser cannot abort this mapping earlier, since lines
513 // are processed sequentially.
514 if (!isset($output[$key])) {
515 $output[$key] = $value;
517 @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);
531 throw new ParseException(sprintf('Malformed inline YAML string: %s.', $mapping));
535 * Evaluates scalars and replaces magic values.
537 * @param string $scalar
539 * @param array $references
541 * @return mixed The evaluated YAML string
543 * @throws ParseException when object parsing support was disabled and the parser detected a PHP object or when a reference could not be resolved
545 private static function evaluateScalar($scalar, $flags, $references = array())
547 $scalar = trim($scalar);
548 $scalarLower = strtolower($scalar);
550 if (0 === strpos($scalar, '*')) {
551 if (false !== $pos = strpos($scalar, '#')) {
552 $value = substr($scalar, 1, $pos - 2);
554 $value = substr($scalar, 1);
558 if (false === $value || '' === $value) {
559 throw new ParseException('A reference must contain at least one character.');
562 if (!array_key_exists($value, $references)) {
563 throw new ParseException(sprintf('Reference "%s" does not exist.', $value));
566 return $references[$value];
570 case 'null' === $scalarLower:
572 case '~' === $scalar:
574 case 'true' === $scalarLower:
576 case 'false' === $scalarLower:
578 // Optimise for returning strings.
579 case $scalar[0] === '+' || $scalar[0] === '-' || $scalar[0] === '.' || $scalar[0] === '!' || is_numeric($scalar[0]):
581 case 0 === strpos($scalar, '!str'):
582 return (string) substr($scalar, 5);
583 case 0 === strpos($scalar, '! '):
584 return (int) self::parseScalar(substr($scalar, 2), $flags);
585 case 0 === strpos($scalar, '!php/object:'):
586 if (self::$objectSupport) {
587 return unserialize(substr($scalar, 12));
590 if (self::$exceptionOnInvalidType) {
591 throw new ParseException('Object support when parsing a YAML file has been disabled.');
595 case 0 === strpos($scalar, '!!php/object:'):
596 if (self::$objectSupport) {
597 @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);
599 return unserialize(substr($scalar, 13));
602 if (self::$exceptionOnInvalidType) {
603 throw new ParseException('Object support when parsing a YAML file has been disabled.');
607 case 0 === strpos($scalar, '!php/const:'):
608 if (self::$constantSupport) {
609 if (defined($const = substr($scalar, 11))) {
610 return constant($const);
613 throw new ParseException(sprintf('The constant "%s" is not defined.', $const));
615 if (self::$exceptionOnInvalidType) {
616 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));
620 case 0 === strpos($scalar, '!!float '):
621 return (float) substr($scalar, 8);
622 case Parser::preg_match('{^[+-]?[0-9][0-9_]*$}', $scalar):
623 $scalar = str_replace('_', '', (string) $scalar);
624 // omitting the break / return as integers are handled in the next case
625 case ctype_digit($scalar):
627 $cast = (int) $scalar;
629 return '0' == $scalar[0] ? octdec($scalar) : (((string) $raw == (string) $cast) ? $cast : $raw);
630 case '-' === $scalar[0] && ctype_digit(substr($scalar, 1)):
632 $cast = (int) $scalar;
634 return '0' == $scalar[1] ? octdec($scalar) : (((string) $raw === (string) $cast) ? $cast : $raw);
635 case is_numeric($scalar):
636 case Parser::preg_match(self::getHexRegex(), $scalar):
637 $scalar = str_replace('_', '', $scalar);
639 return '0x' === $scalar[0].$scalar[1] ? hexdec($scalar) : (float) $scalar;
640 case '.inf' === $scalarLower:
641 case '.nan' === $scalarLower:
643 case '-.inf' === $scalarLower:
645 case 0 === strpos($scalar, '!!binary '):
646 return self::evaluateBinaryScalar(substr($scalar, 9));
647 case Parser::preg_match('/^(-|\+)?[0-9][0-9,]*(\.[0-9_]+)?$/', $scalar):
648 case Parser::preg_match('/^(-|\+)?[0-9][0-9_]*(\.[0-9_]+)?$/', $scalar):
649 if (false !== strpos($scalar, ',')) {
650 @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);
653 return (float) str_replace(array(',', '_'), '', $scalar);
654 case Parser::preg_match(self::getTimestampRegex(), $scalar):
655 if (Yaml::PARSE_DATETIME & $flags) {
656 // When no timezone is provided in the parsed date, YAML spec says we must assume UTC.
657 return new \DateTime($scalar, new \DateTimeZone('UTC'));
660 $timeZone = date_default_timezone_get();
661 date_default_timezone_set('UTC');
662 $time = strtotime($scalar);
663 date_default_timezone_set($timeZone);
668 return (string) $scalar;
673 * @param string $scalar
679 public static function evaluateBinaryScalar($scalar)
681 $parsedBinaryData = self::parseScalar(preg_replace('/\s/', '', $scalar));
683 if (0 !== (strlen($parsedBinaryData) % 4)) {
684 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)));
687 if (!Parser::preg_match('#^[A-Z0-9+/]+={0,2}$#i', $parsedBinaryData)) {
688 throw new ParseException(sprintf('The base64 encoded data (%s) contains invalid characters.', $parsedBinaryData));
691 return base64_decode($parsedBinaryData, true);
694 private static function isBinaryString($value)
696 return !preg_match('//u', $value) || preg_match('/[^\x00\x07-\x0d\x1B\x20-\xff]/', $value);
700 * Gets a regex that matches a YAML date.
702 * @return string The regular expression
704 * @see http://www.yaml.org/spec/1.2/spec.html#id2761573
706 private static function getTimestampRegex()
710 (?P<year>[0-9][0-9][0-9][0-9])
711 -(?P<month>[0-9][0-9]?)
712 -(?P<day>[0-9][0-9]?)
714 (?P<hour>[0-9][0-9]?)
715 :(?P<minute>[0-9][0-9])
716 :(?P<second>[0-9][0-9])
717 (?:\.(?P<fraction>[0-9]*))?
718 (?:[ \t]*(?P<tz>Z|(?P<tz_sign>[-+])(?P<tz_hour>[0-9][0-9]?)
719 (?::(?P<tz_minute>[0-9][0-9]))?))?)?
725 * Gets a regex that matches a YAML number in hexadecimal notation.
729 private static function getHexRegex()
731 return '~^0x[0-9a-f_]++$~i';