run:R W Run
DIR
2026-04-08 19:37:33
R W Run
DIR
2026-04-08 19:37:34
R W Run
DIR
2026-04-08 19:37:35
R W Run
5.92 KB
2026-04-08 19:31:36
R W Run
5.08 KB
2026-04-08 19:31:38
R W Run
3.99 KB
2026-04-08 19:31:37
R W Run
30.5 KB
2026-04-08 19:31:38
R W Run
1.04 KB
2026-04-08 19:31:36
R W Run
47.67 KB
2026-04-08 19:31:36
R W Run
457 By
2026-04-08 19:31:37
R W Run
3.78 KB
2026-04-08 19:31:37
R W Run
2.92 KB
2026-04-08 19:31:39
R W Run
error_log
📄Inline.php
1<?php
2
3/*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Symfony\Component\Yaml;
13
14use Symfony\Component\Yaml\Exception\DumpException;
15use Symfony\Component\Yaml\Exception\ParseException;
16use Symfony\Component\Yaml\Tag\TaggedValue;
17
18/**
19 * Inline implements a YAML parser/dumper for the YAML inline syntax.
20 *
21 * @author Fabien Potencier <fabien@symfony.com>
22 *
23 * @internal
24 */
25class Inline
26{
27 const REGEX_QUOTED_STRING = '(?:"([^"\\\\]*+(?:\\\\.[^"\\\\]*+)*+)"|\'([^\']*+(?:\'\'[^\']*+)*+)\')';
28
29 public static $parsedLineNumber = -1;
30 public static $parsedFilename;
31
32 private static $exceptionOnInvalidType = false;
33 private static $objectSupport = false;
34 private static $objectForMap = false;
35 private static $constantSupport = false;
36
37 public static function initialize(int $flags, int $parsedLineNumber = null, string $parsedFilename = null)
38 {
39 self::$exceptionOnInvalidType = (bool) (Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE & $flags);
40 self::$objectSupport = (bool) (Yaml::PARSE_OBJECT & $flags);
41 self::$objectForMap = (bool) (Yaml::PARSE_OBJECT_FOR_MAP & $flags);
42 self::$constantSupport = (bool) (Yaml::PARSE_CONSTANT & $flags);
43 self::$parsedFilename = $parsedFilename;
44
45 if (null !== $parsedLineNumber) {
46 self::$parsedLineNumber = $parsedLineNumber;
47 }
48 }
49
50 /**
51 * Converts a YAML string to a PHP value.
52 *
53 * @param string $value A YAML string
54 * @param int $flags A bit field of PARSE_* constants to customize the YAML parser behavior
55 * @param array $references Mapping of variable names to values
56 *
57 * @return mixed A PHP value
58 *
59 * @throws ParseException
60 */
61 public static function parse(string $value = null, int $flags = 0, array $references = [])
62 {
63 self::initialize($flags);
64
65 $value = trim($value);
66
67 if ('' === $value) {
68 return '';
69 }
70
71 if (2 /* MB_OVERLOAD_STRING */ & (int) ini_get('mbstring.func_overload')) {
72 $mbEncoding = mb_internal_encoding();
73 mb_internal_encoding('ASCII');
74 }
75
76 try {
77 $i = 0;
78 $tag = self::parseTag($value, $i, $flags);
79 switch ($value[$i]) {
80 case '[':
81 $result = self::parseSequence($value, $flags, $i, $references);
82 ++$i;
83 break;
84 case '{':
85 $result = self::parseMapping($value, $flags, $i, $references);
86 ++$i;
87 break;
88 default:
89 $result = self::parseScalar($value, $flags, null, $i, null === $tag, $references);
90 }
91
92 // some comments are allowed at the end
93 if (preg_replace('/\s+#.*$/A', '', substr($value, $i))) {
94 throw new ParseException(sprintf('Unexpected characters near "%s".', substr($value, $i)), self::$parsedLineNumber + 1, $value, self::$parsedFilename);
95 }
96
97 if (null !== $tag && '' !== $tag) {
98 return new TaggedValue($tag, $result);
99 }
100
101 return $result;
102 } finally {
103 if (isset($mbEncoding)) {
104 mb_internal_encoding($mbEncoding);
105 }
106 }
107 }
108
109 /**
110 * Dumps a given PHP variable to a YAML string.
111 *
112 * @param mixed $value The PHP variable to convert
113 * @param int $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string
114 *
115 * @return string The YAML string representing the PHP value
116 *
117 * @throws DumpException When trying to dump PHP resource
118 */
119 public static function dump($value, int $flags = 0): string
120 {
121 switch (true) {
122 case \is_resource($value):
123 if (Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE & $flags) {
124 throw new DumpException(sprintf('Unable to dump PHP resources in a YAML file ("%s").', get_resource_type($value)));
125 }
126
127 return self::dumpNull($flags);
128 case $value instanceof \DateTimeInterface:
129 return $value->format('c');
130 case \is_object($value):
131 if ($value instanceof TaggedValue) {
132 return '!'.$value->getTag().' '.self::dump($value->getValue(), $flags);
133 }
134
135 if (Yaml::DUMP_OBJECT & $flags) {
136 return '!php/object '.self::dump(serialize($value));
137 }
138
139 if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($value instanceof \stdClass || $value instanceof \ArrayObject)) {
140 $output = [];
141
142 foreach ($value as $key => $val) {
143 $output[] = sprintf('%s: %s', self::dump($key, $flags), self::dump($val, $flags));
144 }
145
146 return sprintf('{ %s }', implode(', ', $output));
147 }
148
149 if (Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE & $flags) {
150 throw new DumpException('Object support when dumping a YAML file has been disabled.');
151 }
152
153 return self::dumpNull($flags);
154 case \is_array($value):
155 return self::dumpArray($value, $flags);
156 case null === $value:
157 return self::dumpNull($flags);
158 case true === $value:
159 return 'true';
160 case false === $value:
161 return 'false';
162 case ctype_digit($value):
163 return \is_string($value) ? "'$value'" : (int) $value;
164 case is_numeric($value):
165 $locale = setlocale(LC_NUMERIC, 0);
166 if (false !== $locale) {
167 setlocale(LC_NUMERIC, 'C');
168 }
169 if (\is_float($value)) {
170 $repr = (string) $value;
171 if (is_infinite($value)) {
172 $repr = str_ireplace('INF', '.Inf', $repr);
173 } elseif (floor($value) == $value && $repr == $value) {
174 // Preserve float data type since storing a whole number will result in integer value.
175 $repr = '!!float '.$repr;
176 }
177 } else {
178 $repr = \is_string($value) ? "'$value'" : (string) $value;
179 }
180 if (false !== $locale) {
181 setlocale(LC_NUMERIC, $locale);
182 }
183
184 return $repr;
185 case '' == $value:
186 return "''";
187 case self::isBinaryString($value):
188 return '!!binary '.base64_encode($value);
189 case Escaper::requiresDoubleQuoting($value):
190 return Escaper::escapeWithDoubleQuotes($value);
191 case Escaper::requiresSingleQuoting($value):
192 case Parser::preg_match('{^[0-9]+[_0-9]*$}', $value):
193 case Parser::preg_match(self::getHexRegex(), $value):
194 case Parser::preg_match(self::getTimestampRegex(), $value):
195 return Escaper::escapeWithSingleQuotes($value);
196 default:
197 return $value;
198 }
199 }
200
201 /**
202 * Check if given array is hash or just normal indexed array.
203 *
204 * @param array|\ArrayObject|\stdClass $value The PHP array or array-like object to check
205 *
206 * @return bool true if value is hash array, false otherwise
207 */
208 public static function isHash($value): bool
209 {
210 if ($value instanceof \stdClass || $value instanceof \ArrayObject) {
211 return true;
212 }
213
214 $expectedKey = 0;
215
216 foreach ($value as $key => $val) {
217 if ($key !== $expectedKey++) {
218 return true;
219 }
220 }
221
222 return false;
223 }
224
225 /**
226 * Dumps a PHP array to a YAML string.
227 *
228 * @param array $value The PHP array to dump
229 * @param int $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string
230 *
231 * @return string The YAML string representing the PHP array
232 */
233 private static function dumpArray(array $value, int $flags): string
234 {
235 // array
236 if (($value || Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE & $flags) && !self::isHash($value)) {
237 $output = [];
238 foreach ($value as $val) {
239 $output[] = self::dump($val, $flags);
240 }
241
242 return sprintf('[%s]', implode(', ', $output));
243 }
244
245 // hash
246 $output = [];
247 foreach ($value as $key => $val) {
248 $output[] = sprintf('%s: %s', self::dump($key, $flags), self::dump($val, $flags));
249 }
250
251 return sprintf('{ %s }', implode(', ', $output));
252 }
253
254 private static function dumpNull(int $flags): string
255 {
256 if (Yaml::DUMP_NULL_AS_TILDE & $flags) {
257 return '~';
258 }
259
260 return 'null';
261 }
262
263 /**
264 * Parses a YAML scalar.
265 *
266 * @return mixed
267 *
268 * @throws ParseException When malformed inline YAML string is parsed
269 */
270 public static function parseScalar(string $scalar, int $flags = 0, array $delimiters = null, int &$i = 0, bool $evaluate = true, array $references = [])
271 {
272 if (\in_array($scalar[$i], ['"', "'"])) {
273 // quoted scalar
274 $output = self::parseQuotedScalar($scalar, $i);
275
276 if (null !== $delimiters) {
277 $tmp = ltrim(substr($scalar, $i), " \n");
278 if ('' === $tmp) {
279 throw new ParseException(sprintf('Unexpected end of line, expected one of "%s".', implode('', $delimiters)), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
280 }
281 if (!\in_array($tmp[0], $delimiters)) {
282 throw new ParseException(sprintf('Unexpected characters (%s).', substr($scalar, $i)), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
283 }
284 }
285 } else {
286 // "normal" string
287 if (!$delimiters) {
288 $output = substr($scalar, $i);
289 $i += \strlen($output);
290
291 // remove comments
292 if (Parser::preg_match('/[ \t]+#/', $output, $match, PREG_OFFSET_CAPTURE)) {
293 $output = substr($output, 0, $match[0][1]);
294 }
295 } elseif (Parser::preg_match('/^(.*?)('.implode('|', $delimiters).')/', substr($scalar, $i), $match)) {
296 $output = $match[1];
297 $i += \strlen($output);
298 $output = trim($output);
299 } else {
300 throw new ParseException(sprintf('Malformed inline YAML string: "%s".', $scalar), self::$parsedLineNumber + 1, null, self::$parsedFilename);
301 }
302
303 // a non-quoted string cannot start with @ or ` (reserved) nor with a scalar indicator (| or >)
304 if ($output && ('@' === $output[0] || '`' === $output[0] || '|' === $output[0] || '>' === $output[0] || '%' === $output[0])) {
305 throw new ParseException(sprintf('The reserved indicator "%s" cannot start a plain scalar; you need to quote the scalar.', $output[0]), self::$parsedLineNumber + 1, $output, self::$parsedFilename);
306 }
307
308 if ($evaluate) {
309 $output = self::evaluateScalar($output, $flags, $references);
310 }
311 }
312
313 return $output;
314 }
315
316 /**
317 * Parses a YAML quoted scalar.
318 *
319 * @throws ParseException When malformed inline YAML string is parsed
320 */
321 private static function parseQuotedScalar(string $scalar, int &$i): string
322 {
323 if (!Parser::preg_match('/'.self::REGEX_QUOTED_STRING.'/Au', substr($scalar, $i), $match)) {
324 throw new ParseException(sprintf('Malformed inline YAML string: "%s".', substr($scalar, $i)), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
325 }
326
327 $output = substr($match[0], 1, \strlen($match[0]) - 2);
328
329 $unescaper = new Unescaper();
330 if ('"' == $scalar[$i]) {
331 $output = $unescaper->unescapeDoubleQuotedString($output);
332 } else {
333 $output = $unescaper->unescapeSingleQuotedString($output);
334 }
335
336 $i += \strlen($match[0]);
337
338 return $output;
339 }
340
341 /**
342 * Parses a YAML sequence.
343 *
344 * @throws ParseException When malformed inline YAML string is parsed
345 */
346 private static function parseSequence(string $sequence, int $flags, int &$i = 0, array $references = []): array
347 {
348 $output = [];
349 $len = \strlen($sequence);
350 ++$i;
351
352 // [foo, bar, ...]
353 while ($i < $len) {
354 if (']' === $sequence[$i]) {
355 return $output;
356 }
357 if (',' === $sequence[$i] || ' ' === $sequence[$i]) {
358 ++$i;
359
360 continue;
361 }
362
363 $tag = self::parseTag($sequence, $i, $flags);
364 switch ($sequence[$i]) {
365 case '[':
366 // nested sequence
367 $value = self::parseSequence($sequence, $flags, $i, $references);
368 break;
369 case '{':
370 // nested mapping
371 $value = self::parseMapping($sequence, $flags, $i, $references);
372 break;
373 default:
374 $isQuoted = \in_array($sequence[$i], ['"', "'"]);
375 $value = self::parseScalar($sequence, $flags, [',', ']'], $i, null === $tag, $references);
376
377 // the value can be an array if a reference has been resolved to an array var
378 if (\is_string($value) && !$isQuoted && false !== strpos($value, ': ')) {
379 // embedded mapping?
380 try {
381 $pos = 0;
382 $value = self::parseMapping('{'.$value.'}', $flags, $pos, $references);
383 } catch (\InvalidArgumentException $e) {
384 // no, it's not
385 }
386 }
387
388 --$i;
389 }
390
391 if (null !== $tag && '' !== $tag) {
392 $value = new TaggedValue($tag, $value);
393 }
394
395 $output[] = $value;
396
397 ++$i;
398 }
399
400 throw new ParseException(sprintf('Malformed inline YAML string: "%s".', $sequence), self::$parsedLineNumber + 1, null, self::$parsedFilename);
401 }
402
403 /**
404 * Parses a YAML mapping.
405 *
406 * @return array|\stdClass
407 *
408 * @throws ParseException When malformed inline YAML string is parsed
409 */
410 private static function parseMapping(string $mapping, int $flags, int &$i = 0, array $references = [])
411 {
412 $output = [];
413 $len = \strlen($mapping);
414 ++$i;
415 $allowOverwrite = false;
416
417 // {foo: bar, bar:foo, ...}
418 while ($i < $len) {
419 switch ($mapping[$i]) {
420 case ' ':
421 case ',':
422 case "\n":
423 ++$i;
424 continue 2;
425 case '}':
426 if (self::$objectForMap) {
427 return (object) $output;
428 }
429
430 return $output;
431 }
432
433 // key
434 $offsetBeforeKeyParsing = $i;
435 $isKeyQuoted = \in_array($mapping[$i], ['"', "'"], true);
436 $key = self::parseScalar($mapping, $flags, [':', ' '], $i, false, []);
437
438 if ($offsetBeforeKeyParsing === $i) {
439 throw new ParseException('Missing mapping key.', self::$parsedLineNumber + 1, $mapping);
440 }
441
442 if ('!php/const' === $key) {
443 $key .= ' '.self::parseScalar($mapping, $flags, [':'], $i, false, []);
444 $key = self::evaluateScalar($key, $flags);
445 }
446
447 if (false === $i = strpos($mapping, ':', $i)) {
448 break;
449 }
450
451 if (!$isKeyQuoted) {
452 $evaluatedKey = self::evaluateScalar($key, $flags, $references);
453
454 if ('' !== $key && $evaluatedKey !== $key && !\is_string($evaluatedKey) && !\is_int($evaluatedKey)) {
455 throw new ParseException('Implicit casting of incompatible mapping keys to strings is not supported. Quote your evaluable mapping keys instead.', self::$parsedLineNumber + 1, $mapping);
456 }
457 }
458
459 if (!$isKeyQuoted && (!isset($mapping[$i + 1]) || !\in_array($mapping[$i + 1], [' ', ',', '[', ']', '{', '}', "\n"], true))) {
460 throw new ParseException('Colons must be followed by a space or an indication character (i.e. " ", ",", "[", "]", "{", "}").', self::$parsedLineNumber + 1, $mapping);
461 }
462
463 if ('<<' === $key) {
464 $allowOverwrite = true;
465 }
466
467 while ($i < $len) {
468 if (':' === $mapping[$i] || ' ' === $mapping[$i] || "\n" === $mapping[$i]) {
469 ++$i;
470
471 continue;
472 }
473
474 $tag = self::parseTag($mapping, $i, $flags);
475 switch ($mapping[$i]) {
476 case '[':
477 // nested sequence
478 $value = self::parseSequence($mapping, $flags, $i, $references);
479 // Spec: Keys MUST be unique; first one wins.
480 // Parser cannot abort this mapping earlier, since lines
481 // are processed sequentially.
482 // But overwriting is allowed when a merge node is used in current block.
483 if ('<<' === $key) {
484 foreach ($value as $parsedValue) {
485 $output += $parsedValue;
486 }
487 } elseif ($allowOverwrite || !isset($output[$key])) {
488 if (null !== $tag) {
489 $output[$key] = new TaggedValue($tag, $value);
490 } else {
491 $output[$key] = $value;
492 }
493 } elseif (isset($output[$key])) {
494 throw new ParseException(sprintf('Duplicate key "%s" detected.', $key), self::$parsedLineNumber + 1, $mapping);
495 }
496 break;
497 case '{':
498 // nested mapping
499 $value = self::parseMapping($mapping, $flags, $i, $references);
500 // Spec: Keys MUST be unique; first one wins.
501 // Parser cannot abort this mapping earlier, since lines
502 // are processed sequentially.
503 // But overwriting is allowed when a merge node is used in current block.
504 if ('<<' === $key) {
505 $output += $value;
506 } elseif ($allowOverwrite || !isset($output[$key])) {
507 if (null !== $tag) {
508 $output[$key] = new TaggedValue($tag, $value);
509 } else {
510 $output[$key] = $value;
511 }
512 } elseif (isset($output[$key])) {
513 throw new ParseException(sprintf('Duplicate key "%s" detected.', $key), self::$parsedLineNumber + 1, $mapping);
514 }
515 break;
516 default:
517 $value = self::parseScalar($mapping, $flags, [',', '}', "\n"], $i, null === $tag, $references);
518 // Spec: Keys MUST be unique; first one wins.
519 // Parser cannot abort this mapping earlier, since lines
520 // are processed sequentially.
521 // But overwriting is allowed when a merge node is used in current block.
522 if ('<<' === $key) {
523 $output += $value;
524 } elseif ($allowOverwrite || !isset($output[$key])) {
525 if (null !== $tag) {
526 $output[$key] = new TaggedValue($tag, $value);
527 } else {
528 $output[$key] = $value;
529 }
530 } elseif (isset($output[$key])) {
531 throw new ParseException(sprintf('Duplicate key "%s" detected.', $key), self::$parsedLineNumber + 1, $mapping);
532 }
533 --$i;
534 }
535 ++$i;
536
537 continue 2;
538 }
539 }
540
541 throw new ParseException(sprintf('Malformed inline YAML string: "%s".', $mapping), self::$parsedLineNumber + 1, null, self::$parsedFilename);
542 }
543
544 /**
545 * Evaluates scalars and replaces magic values.
546 *
547 * @return mixed The evaluated YAML string
548 *
549 * @throws ParseException when object parsing support was disabled and the parser detected a PHP object or when a reference could not be resolved
550 */
551 private static function evaluateScalar(string $scalar, int $flags, array $references = [])
552 {
553 $scalar = trim($scalar);
554 $scalarLower = strtolower($scalar);
555
556 if (0 === strpos($scalar, '*')) {
557 if (false !== $pos = strpos($scalar, '#')) {
558 $value = substr($scalar, 1, $pos - 2);
559 } else {
560 $value = substr($scalar, 1);
561 }
562
563 // an unquoted *
564 if (false === $value || '' === $value) {
565 throw new ParseException('A reference must contain at least one character.', self::$parsedLineNumber + 1, $value, self::$parsedFilename);
566 }
567
568 if (!\array_key_exists($value, $references)) {
569 throw new ParseException(sprintf('Reference "%s" does not exist.', $value), self::$parsedLineNumber + 1, $value, self::$parsedFilename);
570 }
571
572 return $references[$value];
573 }
574
575 switch (true) {
576 case 'null' === $scalarLower:
577 case '' === $scalar:
578 case '~' === $scalar:
579 return null;
580 case 'true' === $scalarLower:
581 return true;
582 case 'false' === $scalarLower:
583 return false;
584 case '!' === $scalar[0]:
585 switch (true) {
586 case 0 === strpos($scalar, '!!str '):
587 return (string) substr($scalar, 6);
588 case 0 === strpos($scalar, '! '):
589 return substr($scalar, 2);
590 case 0 === strpos($scalar, '!php/object'):
591 if (self::$objectSupport) {
592 if (!isset($scalar[12])) {
593 return false;
594 }
595
596 return unserialize(self::parseScalar(substr($scalar, 12)));
597 }
598
599 if (self::$exceptionOnInvalidType) {
600 throw new ParseException('Object support when parsing a YAML file has been disabled.', self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
601 }
602
603 return null;
604 case 0 === strpos($scalar, '!php/const'):
605 if (self::$constantSupport) {
606 if (!isset($scalar[11])) {
607 return '';
608 }
609
610 $i = 0;
611 if (\defined($const = self::parseScalar(substr($scalar, 11), 0, null, $i, false))) {
612 return \constant($const);
613 }
614
615 throw new ParseException(sprintf('The constant "%s" is not defined.', $const), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
616 }
617 if (self::$exceptionOnInvalidType) {
618 throw new ParseException(sprintf('The string "%s" could not be parsed as a constant. Did you forget to pass the "Yaml::PARSE_CONSTANT" flag to the parser?', $scalar), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
619 }
620
621 return null;
622 case 0 === strpos($scalar, '!!float '):
623 return (float) substr($scalar, 8);
624 case 0 === strpos($scalar, '!!binary '):
625 return self::evaluateBinaryScalar(substr($scalar, 9));
626 default:
627 throw new ParseException(sprintf('The string "%s" could not be parsed as it uses an unsupported built-in tag.', $scalar), self::$parsedLineNumber, $scalar, self::$parsedFilename);
628 }
629
630 // Optimize for returning strings.
631 // no break
632 case '+' === $scalar[0] || '-' === $scalar[0] || '.' === $scalar[0] || is_numeric($scalar[0]):
633 if (Parser::preg_match('{^[+-]?[0-9][0-9_]*$}', $scalar)) {
634 $scalar = str_replace('_', '', (string) $scalar);
635 }
636
637 switch (true) {
638 case ctype_digit($scalar):
639 $raw = $scalar;
640 $cast = (int) $scalar;
641
642 return '0' == $scalar[0] ? octdec($scalar) : (((string) $raw == (string) $cast) ? $cast : $raw);
643 case '-' === $scalar[0] && ctype_digit(substr($scalar, 1)):
644 $raw = $scalar;
645 $cast = (int) $scalar;
646
647 return '0' == $scalar[1] ? -octdec(substr($scalar, 1)) : (($raw === (string) $cast) ? $cast : $raw);
648 case is_numeric($scalar):
649 case Parser::preg_match(self::getHexRegex(), $scalar):
650 $scalar = str_replace('_', '', $scalar);
651
652 return '0x' === $scalar[0].$scalar[1] ? hexdec($scalar) : (float) $scalar;
653 case '.inf' === $scalarLower:
654 case '.nan' === $scalarLower:
655 return -log(0);
656 case '-.inf' === $scalarLower:
657 return log(0);
658 case Parser::preg_match('/^(-|\+)?[0-9][0-9_]*(\.[0-9_]+)?$/', $scalar):
659 return (float) str_replace('_', '', $scalar);
660 case Parser::preg_match(self::getTimestampRegex(), $scalar):
661 if (Yaml::PARSE_DATETIME & $flags) {
662 // When no timezone is provided in the parsed date, YAML spec says we must assume UTC.
663 return new \DateTime($scalar, new \DateTimeZone('UTC'));
664 }
665
666 $timeZone = date_default_timezone_get();
667 date_default_timezone_set('UTC');
668 $time = strtotime($scalar);
669 date_default_timezone_set($timeZone);
670
671 return $time;
672 }
673 }
674
675 return (string) $scalar;
676 }
677
678 private static function parseTag(string $value, int &$i, int $flags): ?string
679 {
680 if ('!' !== $value[$i]) {
681 return null;
682 }
683
684 $tagLength = strcspn($value, " \t\n[]{},", $i + 1);
685 $tag = substr($value, $i + 1, $tagLength);
686
687 $nextOffset = $i + $tagLength + 1;
688 $nextOffset += strspn($value, ' ', $nextOffset);
689
690 if ('' === $tag && (!isset($value[$nextOffset]) || \in_array($value[$nextOffset], [']', '}', ','], true))) {
691 throw new ParseException(sprintf('Using the unquoted scalar value "!" is not supported. You must quote it.', $value), self::$parsedLineNumber + 1, $value, self::$parsedFilename);
692 }
693
694 // Is followed by a scalar and is a built-in tag
695 if ('' !== $tag && (!isset($value[$nextOffset]) || !\in_array($value[$nextOffset], ['[', '{'], true)) && ('!' === $tag[0] || 'str' === $tag || 'php/const' === $tag || 'php/object' === $tag)) {
696 // Manage in {@link self::evaluateScalar()}
697 return null;
698 }
699
700 $i = $nextOffset;
701
702 // Built-in tags
703 if ('' !== $tag && '!' === $tag[0]) {
704 throw new ParseException(sprintf('The built-in tag "!%s" is not implemented.', $tag), self::$parsedLineNumber + 1, $value, self::$parsedFilename);
705 }
706
707 if ('' !== $tag && !isset($value[$i])) {
708 throw new ParseException(sprintf('Missing value for tag "%s".', $tag), self::$parsedLineNumber + 1, $value, self::$parsedFilename);
709 }
710
711 if ('' === $tag || Yaml::PARSE_CUSTOM_TAGS & $flags) {
712 return $tag;
713 }
714
715 throw new ParseException(sprintf('Tags support is not enabled. Enable the "Yaml::PARSE_CUSTOM_TAGS" flag to use "!%s".', $tag), self::$parsedLineNumber + 1, $value, self::$parsedFilename);
716 }
717
718 public static function evaluateBinaryScalar(string $scalar): string
719 {
720 $parsedBinaryData = self::parseScalar(preg_replace('/\s/', '', $scalar));
721
722 if (0 !== (\strlen($parsedBinaryData) % 4)) {
723 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)), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
724 }
725
726 if (!Parser::preg_match('#^[A-Z0-9+/]+={0,2}$#i', $parsedBinaryData)) {
727 throw new ParseException(sprintf('The base64 encoded data (%s) contains invalid characters.', $parsedBinaryData), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
728 }
729
730 return base64_decode($parsedBinaryData, true);
731 }
732
733 private static function isBinaryString(string $value): bool
734 {
735 return !preg_match('//u', $value) || preg_match('/[^\x00\x07-\x0d\x1B\x20-\xff]/', $value);
736 }
737
738 /**
739 * Gets a regex that matches a YAML date.
740 *
741 * @return string The regular expression
742 *
743 * @see http://www.yaml.org/spec/1.2/spec.html#id2761573
744 */
745 private static function getTimestampRegex(): string
746 {
747 return <<<EOF
748 ~^
749 (?P<year>[0-9][0-9][0-9][0-9])
750 -(?P<month>[0-9][0-9]?)
751 -(?P<day>[0-9][0-9]?)
752 (?:(?:[Tt]|[ \t]+)
753 (?P<hour>[0-9][0-9]?)
754 :(?P<minute>[0-9][0-9])
755 :(?P<second>[0-9][0-9])
756 (?:\.(?P<fraction>[0-9]*))?
757 (?:[ \t]*(?P<tz>Z|(?P<tz_sign>[-+])(?P<tz_hour>[0-9][0-9]?)
758 (?::(?P<tz_minute>[0-9][0-9]))?))?)?
759 $~x
760EOF;
761 }
762
763 /**
764 * Gets a regex that matches a YAML number in hexadecimal notation.
765 */
766 private static function getHexRegex(): string
767 {
768 return '~^0x[0-9a-f_]++$~i';
769 }
770}
771