Inline.php 37 KB

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