Inline.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  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\ParseException;
  12. use Symfony\Component\Yaml\Exception\DumpException;
  13. /**
  14. * Inline implements a YAML parser/dumper for the YAML inline syntax.
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. *
  18. * @internal
  19. */
  20. class Inline
  21. {
  22. const REGEX_QUOTED_STRING = '(?:"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'([^\']*(?:\'\'[^\']*)*)\')';
  23. private static $exceptionOnInvalidType = false;
  24. private static $objectSupport = false;
  25. private static $objectForMap = false;
  26. /**
  27. * Converts a YAML string to a PHP array.
  28. *
  29. * @param string $value A YAML string
  30. * @param int $flags A bit field of PARSE_* constants to customize the YAML parser behavior
  31. * @param array $references Mapping of variable names to values
  32. *
  33. * @return array A PHP array representing the YAML string
  34. *
  35. * @throws ParseException
  36. */
  37. public static function parse($value, $flags = 0, $references = array())
  38. {
  39. if (is_bool($flags)) {
  40. @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);
  41. if ($flags) {
  42. $flags = Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE;
  43. } else {
  44. $flags = 0;
  45. }
  46. }
  47. if (func_num_args() >= 3 && !is_array($references)) {
  48. @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);
  49. if ($references) {
  50. $flags |= Yaml::PARSE_OBJECT;
  51. }
  52. if (func_num_args() >= 4) {
  53. @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);
  54. if (func_get_arg(3)) {
  55. $flags |= Yaml::PARSE_OBJECT_FOR_MAP;
  56. }
  57. }
  58. if (func_num_args() >= 5) {
  59. $references = func_get_arg(4);
  60. } else {
  61. $references = array();
  62. }
  63. }
  64. self::$exceptionOnInvalidType = (bool) (Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE & $flags);
  65. self::$objectSupport = (bool) (Yaml::PARSE_OBJECT & $flags);
  66. self::$objectForMap = (bool) (Yaml::PARSE_OBJECT_FOR_MAP & $flags);
  67. $value = trim($value);
  68. if ('' === $value) {
  69. return '';
  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. $i = 0;
  76. switch ($value[0]) {
  77. case '[':
  78. $result = self::parseSequence($value, $flags, $i, $references);
  79. ++$i;
  80. break;
  81. case '{':
  82. $result = self::parseMapping($value, $flags, $i, $references);
  83. ++$i;
  84. break;
  85. default:
  86. $result = self::parseScalar($value, $flags, null, array('"', "'"), $i, true, $references);
  87. }
  88. // some comments are allowed at the end
  89. if (preg_replace('/\s+#.*$/A', '', substr($value, $i))) {
  90. throw new ParseException(sprintf('Unexpected characters near "%s".', substr($value, $i)));
  91. }
  92. if (isset($mbEncoding)) {
  93. mb_internal_encoding($mbEncoding);
  94. }
  95. return $result;
  96. }
  97. /**
  98. * Dumps a given PHP variable to a YAML string.
  99. *
  100. * @param mixed $value The PHP variable to convert
  101. * @param int $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string
  102. *
  103. * @return string The YAML string representing the PHP array
  104. *
  105. * @throws DumpException When trying to dump PHP resource
  106. */
  107. public static function dump($value, $flags = 0)
  108. {
  109. if (is_bool($flags)) {
  110. @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);
  111. if ($flags) {
  112. $flags = Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE;
  113. } else {
  114. $flags = 0;
  115. }
  116. }
  117. if (func_num_args() >= 3) {
  118. @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);
  119. if (func_get_arg(2)) {
  120. $flags |= Yaml::DUMP_OBJECT;
  121. }
  122. }
  123. switch (true) {
  124. case is_resource($value):
  125. if (Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE & $flags) {
  126. throw new DumpException(sprintf('Unable to dump PHP resources in a YAML file ("%s").', get_resource_type($value)));
  127. }
  128. return 'null';
  129. case $value instanceof \DateTimeInterface:
  130. return $value->format('c');
  131. case is_object($value):
  132. if (Yaml::DUMP_OBJECT & $flags) {
  133. return '!php/object:'.serialize($value);
  134. }
  135. if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($value instanceof \stdClass || $value instanceof \ArrayObject)) {
  136. return self::dumpArray((array) $value, $flags);
  137. }
  138. if (Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE & $flags) {
  139. throw new DumpException('Object support when dumping a YAML file has been disabled.');
  140. }
  141. return 'null';
  142. case is_array($value):
  143. return self::dumpArray($value, $flags);
  144. case null === $value:
  145. return 'null';
  146. case true === $value:
  147. return 'true';
  148. case false === $value:
  149. return 'false';
  150. case ctype_digit($value):
  151. return is_string($value) ? "'$value'" : (int) $value;
  152. case is_numeric($value):
  153. $locale = setlocale(LC_NUMERIC, 0);
  154. if (false !== $locale) {
  155. setlocale(LC_NUMERIC, 'C');
  156. }
  157. if (is_float($value)) {
  158. $repr = (string) $value;
  159. if (is_infinite($value)) {
  160. $repr = str_ireplace('INF', '.Inf', $repr);
  161. } elseif (floor($value) == $value && $repr == $value) {
  162. // Preserve float data type since storing a whole number will result in integer value.
  163. $repr = '!!float '.$repr;
  164. }
  165. } else {
  166. $repr = is_string($value) ? "'$value'" : (string) $value;
  167. }
  168. if (false !== $locale) {
  169. setlocale(LC_NUMERIC, $locale);
  170. }
  171. return $repr;
  172. case '' == $value:
  173. return "''";
  174. case self::isBinaryString($value):
  175. return '!!binary '.base64_encode($value);
  176. case Escaper::requiresDoubleQuoting($value):
  177. return Escaper::escapeWithDoubleQuotes($value);
  178. case Escaper::requiresSingleQuoting($value):
  179. case preg_match(self::getHexRegex(), $value):
  180. case preg_match(self::getTimestampRegex(), $value):
  181. return Escaper::escapeWithSingleQuotes($value);
  182. default:
  183. return $value;
  184. }
  185. }
  186. /**
  187. * Check if given array is hash or just normal indexed array.
  188. *
  189. * @internal
  190. *
  191. * @param array $value The PHP array to check
  192. *
  193. * @return bool true if value is hash array, false otherwise
  194. */
  195. public static function isHash(array $value)
  196. {
  197. $expectedKey = 0;
  198. foreach ($value as $key => $val) {
  199. if ($key !== $expectedKey++) {
  200. return true;
  201. }
  202. }
  203. return false;
  204. }
  205. /**
  206. * Dumps a PHP array to a YAML string.
  207. *
  208. * @param array $value The PHP array to dump
  209. * @param int $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string
  210. *
  211. * @return string The YAML string representing the PHP array
  212. */
  213. private static function dumpArray($value, $flags)
  214. {
  215. // array
  216. if ($value && !self::isHash($value)) {
  217. $output = array();
  218. foreach ($value as $val) {
  219. $output[] = self::dump($val, $flags);
  220. }
  221. return sprintf('[%s]', implode(', ', $output));
  222. }
  223. // hash
  224. $output = array();
  225. foreach ($value as $key => $val) {
  226. $output[] = sprintf('%s: %s', self::dump($key, $flags), self::dump($val, $flags));
  227. }
  228. return sprintf('{ %s }', implode(', ', $output));
  229. }
  230. /**
  231. * Parses a scalar to a YAML string.
  232. *
  233. * @param string $scalar
  234. * @param int $flags
  235. * @param string $delimiters
  236. * @param array $stringDelimiters
  237. * @param int &$i
  238. * @param bool $evaluate
  239. * @param array $references
  240. *
  241. * @return string A YAML string
  242. *
  243. * @throws ParseException When malformed inline YAML string is parsed
  244. *
  245. * @internal
  246. */
  247. public static function parseScalar($scalar, $flags = 0, $delimiters = null, $stringDelimiters = array('"', "'"), &$i = 0, $evaluate = true, $references = array())
  248. {
  249. if (in_array($scalar[$i], $stringDelimiters)) {
  250. // quoted scalar
  251. $output = self::parseQuotedScalar($scalar, $i);
  252. if (null !== $delimiters) {
  253. $tmp = ltrim(substr($scalar, $i), ' ');
  254. if (!in_array($tmp[0], $delimiters)) {
  255. throw new ParseException(sprintf('Unexpected characters (%s).', substr($scalar, $i)));
  256. }
  257. }
  258. } else {
  259. // "normal" string
  260. if (!$delimiters) {
  261. $output = substr($scalar, $i);
  262. $i += strlen($output);
  263. // remove comments
  264. if (preg_match('/[ \t]+#/', $output, $match, PREG_OFFSET_CAPTURE)) {
  265. $output = substr($output, 0, $match[0][1]);
  266. }
  267. } elseif (preg_match('/^(.+?)('.implode('|', $delimiters).')/', substr($scalar, $i), $match)) {
  268. $output = $match[1];
  269. $i += strlen($output);
  270. } else {
  271. throw new ParseException(sprintf('Malformed inline YAML string (%s).', $scalar));
  272. }
  273. // a non-quoted string cannot start with @ or ` (reserved) nor with a scalar indicator (| or >)
  274. if ($output && ('@' === $output[0] || '`' === $output[0] || '|' === $output[0] || '>' === $output[0])) {
  275. throw new ParseException(sprintf('The reserved indicator "%s" cannot start a plain scalar; you need to quote the scalar.', $output[0]));
  276. }
  277. if ($output && '%' === $output[0]) {
  278. @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);
  279. }
  280. if ($evaluate) {
  281. $output = self::evaluateScalar($output, $flags, $references);
  282. }
  283. }
  284. return $output;
  285. }
  286. /**
  287. * Parses a quoted scalar to YAML.
  288. *
  289. * @param string $scalar
  290. * @param int &$i
  291. *
  292. * @return string A YAML string
  293. *
  294. * @throws ParseException When malformed inline YAML string is parsed
  295. */
  296. private static function parseQuotedScalar($scalar, &$i)
  297. {
  298. if (!preg_match('/'.self::REGEX_QUOTED_STRING.'/Au', substr($scalar, $i), $match)) {
  299. throw new ParseException(sprintf('Malformed inline YAML string (%s).', substr($scalar, $i)));
  300. }
  301. $output = substr($match[0], 1, strlen($match[0]) - 2);
  302. $unescaper = new Unescaper();
  303. if ('"' == $scalar[$i]) {
  304. $output = $unescaper->unescapeDoubleQuotedString($output);
  305. } else {
  306. $output = $unescaper->unescapeSingleQuotedString($output);
  307. }
  308. $i += strlen($match[0]);
  309. return $output;
  310. }
  311. /**
  312. * Parses a sequence to a YAML string.
  313. *
  314. * @param string $sequence
  315. * @param int $flags
  316. * @param int &$i
  317. * @param array $references
  318. *
  319. * @return string A YAML string
  320. *
  321. * @throws ParseException When malformed inline YAML string is parsed
  322. */
  323. private static function parseSequence($sequence, $flags, &$i = 0, $references = array())
  324. {
  325. $output = array();
  326. $len = strlen($sequence);
  327. ++$i;
  328. // [foo, bar, ...]
  329. while ($i < $len) {
  330. switch ($sequence[$i]) {
  331. case '[':
  332. // nested sequence
  333. $output[] = self::parseSequence($sequence, $flags, $i, $references);
  334. break;
  335. case '{':
  336. // nested mapping
  337. $output[] = self::parseMapping($sequence, $flags, $i, $references);
  338. break;
  339. case ']':
  340. return $output;
  341. case ',':
  342. case ' ':
  343. break;
  344. default:
  345. $isQuoted = in_array($sequence[$i], array('"', "'"));
  346. $value = self::parseScalar($sequence, $flags, array(',', ']'), array('"', "'"), $i, true, $references);
  347. // the value can be an array if a reference has been resolved to an array var
  348. if (is_string($value) && !$isQuoted && false !== strpos($value, ': ')) {
  349. // embedded mapping?
  350. try {
  351. $pos = 0;
  352. $value = self::parseMapping('{'.$value.'}', $flags, $pos, $references);
  353. } catch (\InvalidArgumentException $e) {
  354. // no, it's not
  355. }
  356. }
  357. $output[] = $value;
  358. --$i;
  359. }
  360. ++$i;
  361. }
  362. throw new ParseException(sprintf('Malformed inline YAML string %s', $sequence));
  363. }
  364. /**
  365. * Parses a mapping to a YAML string.
  366. *
  367. * @param string $mapping
  368. * @param int $flags
  369. * @param int &$i
  370. * @param array $references
  371. *
  372. * @return string A YAML string
  373. *
  374. * @throws ParseException When malformed inline YAML string is parsed
  375. */
  376. private static function parseMapping($mapping, $flags, &$i = 0, $references = array())
  377. {
  378. $output = array();
  379. $len = strlen($mapping);
  380. ++$i;
  381. // {foo: bar, bar:foo, ...}
  382. while ($i < $len) {
  383. switch ($mapping[$i]) {
  384. case ' ':
  385. case ',':
  386. ++$i;
  387. continue 2;
  388. case '}':
  389. if (self::$objectForMap) {
  390. return (object) $output;
  391. }
  392. return $output;
  393. }
  394. // key
  395. $key = self::parseScalar($mapping, $flags, array(':', ' '), array('"', "'"), $i, false);
  396. // value
  397. $done = false;
  398. while ($i < $len) {
  399. switch ($mapping[$i]) {
  400. case '[':
  401. // nested sequence
  402. $value = self::parseSequence($mapping, $flags, $i, $references);
  403. // Spec: Keys MUST be unique; first one wins.
  404. // Parser cannot abort this mapping earlier, since lines
  405. // are processed sequentially.
  406. if (!isset($output[$key])) {
  407. $output[$key] = $value;
  408. }
  409. $done = true;
  410. break;
  411. case '{':
  412. // nested mapping
  413. $value = self::parseMapping($mapping, $flags, $i, $references);
  414. // Spec: Keys MUST be unique; first one wins.
  415. // Parser cannot abort this mapping earlier, since lines
  416. // are processed sequentially.
  417. if (!isset($output[$key])) {
  418. $output[$key] = $value;
  419. }
  420. $done = true;
  421. break;
  422. case ':':
  423. case ' ':
  424. break;
  425. default:
  426. $value = self::parseScalar($mapping, $flags, array(',', '}'), array('"', "'"), $i, true, $references);
  427. // Spec: Keys MUST be unique; first one wins.
  428. // Parser cannot abort this mapping earlier, since lines
  429. // are processed sequentially.
  430. if (!isset($output[$key])) {
  431. $output[$key] = $value;
  432. }
  433. $done = true;
  434. --$i;
  435. }
  436. ++$i;
  437. if ($done) {
  438. continue 2;
  439. }
  440. }
  441. }
  442. throw new ParseException(sprintf('Malformed inline YAML string %s', $mapping));
  443. }
  444. /**
  445. * Evaluates scalars and replaces magic values.
  446. *
  447. * @param string $scalar
  448. * @param int $flags
  449. * @param array $references
  450. *
  451. * @return string A YAML string
  452. *
  453. * @throws ParseException when object parsing support was disabled and the parser detected a PHP object or when a reference could not be resolved
  454. */
  455. private static function evaluateScalar($scalar, $flags, $references = array())
  456. {
  457. $scalar = trim($scalar);
  458. $scalarLower = strtolower($scalar);
  459. if (0 === strpos($scalar, '*')) {
  460. if (false !== $pos = strpos($scalar, '#')) {
  461. $value = substr($scalar, 1, $pos - 2);
  462. } else {
  463. $value = substr($scalar, 1);
  464. }
  465. // an unquoted *
  466. if (false === $value || '' === $value) {
  467. throw new ParseException('A reference must contain at least one character.');
  468. }
  469. if (!array_key_exists($value, $references)) {
  470. throw new ParseException(sprintf('Reference "%s" does not exist.', $value));
  471. }
  472. return $references[$value];
  473. }
  474. switch (true) {
  475. case 'null' === $scalarLower:
  476. case '' === $scalar:
  477. case '~' === $scalar:
  478. return;
  479. case 'true' === $scalarLower:
  480. return true;
  481. case 'false' === $scalarLower:
  482. return false;
  483. // Optimise for returning strings.
  484. case $scalar[0] === '+' || $scalar[0] === '-' || $scalar[0] === '.' || $scalar[0] === '!' || is_numeric($scalar[0]):
  485. switch (true) {
  486. case 0 === strpos($scalar, '!str'):
  487. return (string) substr($scalar, 5);
  488. case 0 === strpos($scalar, '! '):
  489. return (int) self::parseScalar(substr($scalar, 2), $flags);
  490. case 0 === strpos($scalar, '!php/object:'):
  491. if (self::$objectSupport) {
  492. return unserialize(substr($scalar, 12));
  493. }
  494. if (self::$exceptionOnInvalidType) {
  495. throw new ParseException('Object support when parsing a YAML file has been disabled.');
  496. }
  497. return;
  498. case 0 === strpos($scalar, '!!php/object:'):
  499. if (self::$objectSupport) {
  500. @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);
  501. return unserialize(substr($scalar, 13));
  502. }
  503. if (self::$exceptionOnInvalidType) {
  504. throw new ParseException('Object support when parsing a YAML file has been disabled.');
  505. }
  506. return;
  507. case 0 === strpos($scalar, '!!float '):
  508. return (float) substr($scalar, 8);
  509. case ctype_digit($scalar):
  510. $raw = $scalar;
  511. $cast = (int) $scalar;
  512. return '0' == $scalar[0] ? octdec($scalar) : (((string) $raw == (string) $cast) ? $cast : $raw);
  513. case '-' === $scalar[0] && ctype_digit(substr($scalar, 1)):
  514. $raw = $scalar;
  515. $cast = (int) $scalar;
  516. return '0' == $scalar[1] ? octdec($scalar) : (((string) $raw === (string) $cast) ? $cast : $raw);
  517. case is_numeric($scalar):
  518. case preg_match(self::getHexRegex(), $scalar):
  519. return '0x' === $scalar[0].$scalar[1] ? hexdec($scalar) : (float) $scalar;
  520. case '.inf' === $scalarLower:
  521. case '.nan' === $scalarLower:
  522. return -log(0);
  523. case '-.inf' === $scalarLower:
  524. return log(0);
  525. case 0 === strpos($scalar, '!!binary '):
  526. return self::evaluateBinaryScalar(substr($scalar, 9));
  527. case preg_match('/^(-|\+)?[0-9,]+(\.[0-9]+)?$/', $scalar):
  528. return (float) str_replace(',', '', $scalar);
  529. case preg_match(self::getTimestampRegex(), $scalar):
  530. if (Yaml::PARSE_DATETIME & $flags) {
  531. return new \DateTime($scalar, new \DateTimeZone('UTC'));
  532. }
  533. $timeZone = date_default_timezone_get();
  534. date_default_timezone_set('UTC');
  535. $time = strtotime($scalar);
  536. date_default_timezone_set($timeZone);
  537. return $time;
  538. }
  539. default:
  540. return (string) $scalar;
  541. }
  542. }
  543. /**
  544. * @param string $scalar
  545. *
  546. * @return string
  547. *
  548. * @internal
  549. */
  550. public static function evaluateBinaryScalar($scalar)
  551. {
  552. $parsedBinaryData = self::parseScalar(preg_replace('/\s/', '', $scalar));
  553. if (0 !== (strlen($parsedBinaryData) % 4)) {
  554. 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)));
  555. }
  556. if (!preg_match('#^[A-Z0-9+/]+={0,2}$#i', $parsedBinaryData)) {
  557. throw new ParseException(sprintf('The base64 encoded data (%s) contains invalid characters.', $parsedBinaryData));
  558. }
  559. return base64_decode($parsedBinaryData, true);
  560. }
  561. private static function isBinaryString($value)
  562. {
  563. return !preg_match('//u', $value) || preg_match('/[^\x09-\x0d\x20-\xff]/', $value);
  564. }
  565. /**
  566. * Gets a regex that matches a YAML date.
  567. *
  568. * @return string The regular expression
  569. *
  570. * @see http://www.yaml.org/spec/1.2/spec.html#id2761573
  571. */
  572. private static function getTimestampRegex()
  573. {
  574. return <<<EOF
  575. ~^
  576. (?P<year>[0-9][0-9][0-9][0-9])
  577. -(?P<month>[0-9][0-9]?)
  578. -(?P<day>[0-9][0-9]?)
  579. (?:(?:[Tt]|[ \t]+)
  580. (?P<hour>[0-9][0-9]?)
  581. :(?P<minute>[0-9][0-9])
  582. :(?P<second>[0-9][0-9])
  583. (?:\.(?P<fraction>[0-9]*))?
  584. (?:[ \t]*(?P<tz>Z|(?P<tz_sign>[-+])(?P<tz_hour>[0-9][0-9]?)
  585. (?::(?P<tz_minute>[0-9][0-9]))?))?)?
  586. $~x
  587. EOF;
  588. }
  589. /**
  590. * Gets a regex that matches a YAML number in hexadecimal notation.
  591. *
  592. * @return string
  593. */
  594. private static function getHexRegex()
  595. {
  596. return '~^0x[0-9a-f]++$~i';
  597. }
  598. }