43 public static function parse($value, $flags = 0, $references =
array())
45 if (is_bool($flags)) {
46 @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);
55 if (func_num_args() >= 3 && !is_array($references)) {
56 @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 if (func_num_args() >= 4) {
63 @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);
65 if (func_get_arg(3)) {
70 if (func_num_args() >= 5) {
71 $references = func_get_arg(4);
73 $references =
array();
81 $value = trim($value);
87 if (2 & (
int) ini_get(
'mbstring.func_overload')) {
88 $mbEncoding = mb_internal_encoding();
89 mb_internal_encoding(
'ASCII');
95 $result = self::parseSequence($value, $flags,
$i, $references);
99 $result = self::parseMapping($value, $flags,
$i, $references);
103 $result = self::parseScalar($value, $flags, null,
array(
'"',
"'"),
$i,
true, $references);
107 if (preg_replace(
'/\s+#.*$/A',
'', substr($value,
$i))) {
108 throw new ParseException(sprintf(
'Unexpected characters near "%s".', substr($value,
$i)));
111 if (isset($mbEncoding)) {
112 mb_internal_encoding($mbEncoding);
128 public static function dump($value, $flags = 0)
130 if (is_bool($flags)) {
131 @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);
140 if (func_num_args() >= 3) {
141 @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);
143 if (func_get_arg(2)) {
149 case is_resource($value):
151 throw new DumpException(sprintf(
'Unable to dump PHP resources in a YAML file ("%s").', get_resource_type($value)));
155 case $value instanceof \DateTimeInterface:
156 return $value->format(
'c');
157 case is_object($value):
159 return '!php/object:'.serialize($value);
163 return self::dumpArray((
array) $value, $flags);
167 throw new DumpException(
'Object support when dumping a YAML file has been disabled.');
171 case is_array($value):
172 return self::dumpArray($value, $flags);
173 case null === $value:
175 case true === $value:
177 case false === $value:
179 case ctype_digit($value):
180 return is_string($value) ?
"'$value'" : (int) $value;
181 case is_numeric($value):
182 $locale = setlocale(LC_NUMERIC, 0);
183 if (
false !== $locale) {
184 setlocale(LC_NUMERIC,
'C');
186 if (is_float($value)) {
188 if (is_infinite($value)) {
189 $repr = str_ireplace(
'INF',
'.Inf', $repr);
190 } elseif (floor($value) == $value && $repr == $value) {
192 $repr =
'!!float '.$repr;
195 $repr = is_string($value) ?
"'$value'" : (
string) $value;
197 if (
false !== $locale) {
198 setlocale(LC_NUMERIC, $locale);
204 case self::isBinaryString($value):
205 return '!!binary '.base64_encode($value);
209 case preg_match(self::getHexRegex(), $value):
210 case preg_match(self::getTimestampRegex(), $value):
230 foreach ($value as
$key => $val) {
231 if (
$key !== $expectedKey++) {
250 if ($value && !self::isHash($value)) {
252 foreach ($value as $val) {
253 $output[] = self::dump($val, $flags);
256 return sprintf(
'[%s]', implode(
', ',
$output));
261 foreach ($value as
$key => $val) {
262 $output[] = sprintf(
'%s: %s', self::dump(
$key, $flags), self::dump($val, $flags));
265 return sprintf(
'{ %s }', implode(
', ',
$output));
285 public static function parseScalar($scalar, $flags = 0, $delimiters = null, $stringDelimiters =
array(
'"',
"'"), &
$i = 0, $evaluate =
true, $references =
array())
287 if (in_array($scalar[
$i], $stringDelimiters)) {
289 $output = self::parseQuotedScalar($scalar, $i);
291 if (null !== $delimiters) {
292 $tmp = ltrim(substr($scalar, $i),
' ');
293 if (!in_array($tmp[0], $delimiters)) {
294 throw new ParseException(sprintf(
'Unexpected characters (%s).', substr($scalar, $i)));
304 if (preg_match(
'/[ \t]+#/',
$output, $match, PREG_OFFSET_CAPTURE)) {
307 } elseif (preg_match(
'/^(.+?)('.implode(
'|', $delimiters).
')/', substr($scalar, $i), $match)) {
311 throw new ParseException(sprintf(
'Malformed inline YAML string (%s).', $scalar));
316 throw new ParseException(sprintf(
'The reserved indicator "%s" cannot start a plain scalar; you need to quote the scalar.',
$output[0]));
320 @trigger_error(
'Not quoting a scalar starting with the "%" indicator character is deprecated since Symfony 3.1 and will throw a ParseException in 4.0.', E_USER_DEPRECATED);
343 if (!preg_match(
'/'.self::REGEX_QUOTED_STRING.
'/Au', substr($scalar,
$i), $match)) {
344 throw new ParseException(sprintf(
'Malformed inline YAML string (%s).', substr($scalar,
$i)));
347 $output = substr($match[0], 1, strlen($match[0]) - 2);
350 if (
'"' == $scalar[
$i]) {
356 $i += strlen($match[0]);
376 $len = strlen($sequence);
381 switch ($sequence[
$i]) {
384 $output[] = self::parseSequence($sequence, $flags, $i, $references);
388 $output[] = self::parseMapping($sequence, $flags, $i, $references);
396 $isQuoted = in_array($sequence[$i],
array(
'"',
"'"));
397 $value = self::parseScalar($sequence, $flags,
array(
',',
']'),
array(
'"',
"'"), $i,
true, $references);
400 if (is_string($value) && !$isQuoted &&
false !== strpos($value,
': ')) {
404 $value = self::parseMapping(
'{'.$value.
'}', $flags, $pos, $references);
418 throw new ParseException(sprintf(
'Malformed inline YAML string %s', $sequence));
436 $len = strlen($mapping);
441 switch ($mapping[
$i]) {
447 if (self::$objectForMap) {
455 $key = self::parseScalar($mapping, $flags,
array(
':',
' '),
array(
'"',
"'"), $i,
false);
461 switch ($mapping[$i]) {
464 $value = self::parseSequence($mapping, $flags, $i, $references);
475 $value = self::parseMapping($mapping, $flags, $i, $references);
488 $value = self::parseScalar($mapping, $flags,
array(
',',
'}'),
array(
'"',
"'"), $i,
true, $references);
507 throw new ParseException(sprintf(
'Malformed inline YAML string %s', $mapping));
523 $scalar = trim($scalar);
524 $scalarLower = strtolower($scalar);
526 if (0 === strpos($scalar,
'*')) {
527 if (
false !== $pos = strpos($scalar,
'#')) {
528 $value = substr($scalar, 1, $pos - 2);
530 $value = substr($scalar, 1);
534 if (
false === $value ||
'' === $value) {
535 throw new ParseException(
'A reference must contain at least one character.');
538 if (!array_key_exists($value, $references)) {
539 throw new ParseException(sprintf(
'Reference "%s" does not exist.', $value));
542 return $references[$value];
546 case 'null' === $scalarLower:
548 case '~' === $scalar:
550 case 'true' === $scalarLower:
552 case 'false' === $scalarLower:
555 case $scalar[0] ===
'+' || $scalar[0] ===
'-' || $scalar[0] ===
'.' || $scalar[0] ===
'!' || is_numeric($scalar[0]):
557 case 0 === strpos($scalar,
'!str'):
558 return (
string) substr($scalar, 5);
559 case 0 === strpos($scalar,
'! '):
560 return (
int) self::parseScalar(substr($scalar, 2), $flags);
561 case 0 === strpos($scalar,
'!php/object:'):
562 if (self::$objectSupport) {
563 return unserialize(substr($scalar, 12));
566 if (self::$exceptionOnInvalidType) {
567 throw new ParseException(
'Object support when parsing a YAML file has been disabled.');
571 case 0 === strpos($scalar,
'!!php/object:'):
572 if (self::$objectSupport) {
573 @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);
575 return unserialize(substr($scalar, 13));
578 if (self::$exceptionOnInvalidType) {
579 throw new ParseException(
'Object support when parsing a YAML file has been disabled.');
583 case 0 === strpos($scalar,
'!!float '):
584 return (
float) substr($scalar, 8);
585 case ctype_digit($scalar):
587 $cast = (int) $scalar;
589 return '0' == $scalar[0] ? octdec($scalar) : (((
string) $raw == (
string) $cast) ? $cast : $raw);
590 case '-' === $scalar[0] && ctype_digit(substr($scalar, 1)):
592 $cast = (int) $scalar;
594 return '0' == $scalar[1] ? octdec($scalar) : (((
string) $raw === (
string) $cast) ? $cast : $raw);
595 case is_numeric($scalar):
596 case preg_match(self::getHexRegex(), $scalar):
597 return '0x' === $scalar[0].$scalar[1] ? hexdec($scalar) : (float) $scalar;
598 case '.inf' === $scalarLower:
599 case '.nan' === $scalarLower:
601 case '-.inf' === $scalarLower:
603 case 0 === strpos($scalar,
'!!binary '):
604 return self::evaluateBinaryScalar(substr($scalar, 9));
605 case preg_match(
'/^(-|\+)?[0-9,]+(\.[0-9]+)?$/', $scalar):
606 return (
float) str_replace(
',',
'', $scalar);
607 case preg_match(self::getTimestampRegex(), $scalar):
609 return new \DateTime($scalar,
new \DateTimeZone(
'UTC'));
612 $timeZone = date_default_timezone_get();
613 date_default_timezone_set(
'UTC');
614 $time = strtotime($scalar);
615 date_default_timezone_set($timeZone);
620 return (
string) $scalar;
633 $parsedBinaryData = self::parseScalar(preg_replace(
'/\s/',
'', $scalar));
635 if (0 !== (strlen($parsedBinaryData) % 4)) {
636 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)));
639 if (!preg_match(
'#^[A-Z0-9+/]+={0,2}$#i', $parsedBinaryData)) {
640 throw new ParseException(sprintf(
'The base64 encoded data (%s) contains invalid characters.', $parsedBinaryData));
643 return base64_decode($parsedBinaryData,
true);
648 return !preg_match(
'//u', $value) || preg_match(
'/[^\x09-\x0d\x20-\xff]/', $value);
662 (?P<year>[0-9][0-9][0-9][0-9])
663 -(?P<month>[0-9][0-9]?)
664 -(?P<day>[0-9][0-9]?)
666 (?P<hour>[0-9][0-9]?)
667 :(?P<minute>[0-9][0-9])
668 :(?P<second>[0-9][0-9])
669 (?:\.(?P<fraction>[0-9]*))?
670 (?:[ \t]*(?P<tz>Z|(?P<tz_sign>[-+])(?P<tz_hour>[0-9][0-9]?)
671 (?::(?P<tz_minute>[0-9][0-9]))?))?)?
683 return '~^0x[0-9a-f]++$~i';
static evaluateBinaryScalar($scalar)
static requiresDoubleQuoting($value)
Determines if a PHP value would require double quoting in YAML.
const PARSE_EXCEPTION_ON_INVALID_TYPE
static evaluateScalar($scalar, $flags, $references=array())
Evaluates scalars and replaces magic values.
static parseQuotedScalar($scalar, &$i)
Parses a quoted scalar to YAML.
static parseSequence($sequence, $flags, &$i=0, $references=array())
Parses a sequence to a YAML string.
static isHash(array $value)
Check if given array is hash or just normal indexed array.
Inline implements a YAML parser/dumper for the YAML inline syntax.
if(!is_dir( $entity_dir)) exit("Fatal Error ([A-Za-z0-9]+)\+" &#(? foreach( $entity_files as $file) $output
static dump($value, $flags=0)
Dumps a given PHP variable to a YAML string.
Unescaper encapsulates unescaping rules for single and double-quoted YAML strings.
const REGEX_QUOTED_STRING
Exception class thrown when an error occurs during dumping.
static getHexRegex()
Gets a regex that matches a YAML number in hexadecimal notation.
static parseScalar($scalar, $flags=0, $delimiters=null, $stringDelimiters=array('"', "'"), &$i = 0, $evaluate = true, $references = array())
Parses a scalar to a YAML string.
Create styles array
The data for the language used.
static escapeWithDoubleQuotes($value)
Escapes and surrounds a PHP value with double quotes.
static $exceptionOnInvalidType
static getTimestampRegex()
Gets a regex that matches a YAML date.
static escapeWithSingleQuotes($value)
Escapes and surrounds a PHP value with single quotes.
static parseMapping($mapping, $flags, &$i=0, $references=array())
Parses a mapping to a YAML string.
static parse($value, $flags=0, $references=array())
Converts a YAML string to a PHP array.
Exception class thrown when an error occurs during parsing.
static isBinaryString($value)
static dumpArray($value, $flags)
Dumps a PHP array to a YAML string.
const PARSE_OBJECT_FOR_MAP
const DUMP_EXCEPTION_ON_INVALID_TYPE
static requiresSingleQuoting($value)
Determines if a PHP value would require single quoting in YAML.
const EOF
How fgetc() reports an End Of File.