Parser.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  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. /**
  13. * Parser parses YAML strings to convert them to PHP arrays.
  14. *
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. */
  17. class Parser
  18. {
  19. const BLOCK_SCALAR_HEADER_PATTERN = '(?P<separator>\||>)(?P<modifiers>\+|\-|\d+|\+\d+|\-\d+|\d+\+|\d+\-)?(?P<comments> +#.*)?';
  20. // BC - wrongly named
  21. const FOLDED_SCALAR_PATTERN = self::BLOCK_SCALAR_HEADER_PATTERN;
  22. private $offset = 0;
  23. private $lines = array();
  24. private $currentLineNb = -1;
  25. private $currentLine = '';
  26. private $refs = array();
  27. /**
  28. * Constructor.
  29. *
  30. * @param int $offset The offset of YAML document (used for line numbers in error messages)
  31. */
  32. public function __construct($offset = 0)
  33. {
  34. $this->offset = $offset;
  35. }
  36. /**
  37. * Parses a YAML string to a PHP value.
  38. *
  39. * @param string $value A YAML string
  40. * @param bool $exceptionOnInvalidType true if an exception must be thrown on invalid types (a PHP resource or object), false otherwise
  41. * @param bool $objectSupport true if object support is enabled, false otherwise
  42. * @param bool $objectForMap true if maps should return a stdClass instead of array()
  43. *
  44. * @return mixed A PHP value
  45. *
  46. * @throws ParseException If the YAML is not valid
  47. */
  48. public function parse($value, $exceptionOnInvalidType = false, $objectSupport = false, $objectForMap = false)
  49. {
  50. if (!preg_match('//u', $value)) {
  51. throw new ParseException('The YAML value does not appear to be valid UTF-8.');
  52. }
  53. $this->currentLineNb = -1;
  54. $this->currentLine = '';
  55. $value = $this->cleanup($value);
  56. $this->lines = explode("\n", $value);
  57. if (2 /* MB_OVERLOAD_STRING */ & (int) ini_get('mbstring.func_overload')) {
  58. $mbEncoding = mb_internal_encoding();
  59. mb_internal_encoding('UTF-8');
  60. }
  61. $data = array();
  62. $context = null;
  63. $allowOverwrite = false;
  64. while ($this->moveToNextLine()) {
  65. if ($this->isCurrentLineEmpty()) {
  66. continue;
  67. }
  68. // tab?
  69. if ("\t" === $this->currentLine[0]) {
  70. throw new ParseException('A YAML file cannot contain tabs as indentation.', $this->getRealCurrentLineNb() + 1, $this->currentLine);
  71. }
  72. $isRef = $mergeNode = false;
  73. if (preg_match('#^\-((?P<leadspaces>\s+)(?P<value>.+?))?\s*$#u', $this->currentLine, $values)) {
  74. if ($context && 'mapping' == $context) {
  75. throw new ParseException('You cannot define a sequence item when in a mapping');
  76. }
  77. $context = 'sequence';
  78. if (isset($values['value']) && preg_match('#^&(?P<ref>[^ ]+) *(?P<value>.*)#u', $values['value'], $matches)) {
  79. $isRef = $matches['ref'];
  80. $values['value'] = $matches['value'];
  81. }
  82. // array
  83. if (!isset($values['value']) || '' == trim($values['value'], ' ') || 0 === strpos(ltrim($values['value'], ' '), '#')) {
  84. $c = $this->getRealCurrentLineNb() + 1;
  85. $parser = new self($c);
  86. $parser->refs = &$this->refs;
  87. $data[] = $parser->parse($this->getNextEmbedBlock(null, true), $exceptionOnInvalidType, $objectSupport, $objectForMap);
  88. } else {
  89. if (isset($values['leadspaces'])
  90. && preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\{\[].*?) *\:(\s+(?P<value>.+?))?\s*$#u', $values['value'], $matches)
  91. ) {
  92. // this is a compact notation element, add to next block and parse
  93. $c = $this->getRealCurrentLineNb();
  94. $parser = new self($c);
  95. $parser->refs = &$this->refs;
  96. $block = $values['value'];
  97. if ($this->isNextLineIndented()) {
  98. $block .= "\n".$this->getNextEmbedBlock($this->getCurrentLineIndentation() + strlen($values['leadspaces']) + 1);
  99. }
  100. $data[] = $parser->parse($block, $exceptionOnInvalidType, $objectSupport, $objectForMap);
  101. } else {
  102. $data[] = $this->parseValue($values['value'], $exceptionOnInvalidType, $objectSupport, $objectForMap, $context);
  103. }
  104. }
  105. if ($isRef) {
  106. $this->refs[$isRef] = end($data);
  107. }
  108. } elseif (preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\[\{].*?) *\:(\s+(?P<value>.+?))?\s*$#u', $this->currentLine, $values) && (false === strpos($values['key'], ' #') || in_array($values['key'][0], array('"', "'")))) {
  109. if ($context && 'sequence' == $context) {
  110. throw new ParseException('You cannot define a mapping item when in a sequence');
  111. }
  112. $context = 'mapping';
  113. // force correct settings
  114. Inline::parse(null, $exceptionOnInvalidType, $objectSupport, $objectForMap, $this->refs);
  115. try {
  116. $key = Inline::parseScalar($values['key']);
  117. } catch (ParseException $e) {
  118. $e->setParsedLine($this->getRealCurrentLineNb() + 1);
  119. $e->setSnippet($this->currentLine);
  120. throw $e;
  121. }
  122. // Convert float keys to strings, to avoid being converted to integers by PHP
  123. if (is_float($key)) {
  124. $key = (string) $key;
  125. }
  126. if ('<<' === $key) {
  127. $mergeNode = true;
  128. $allowOverwrite = true;
  129. if (isset($values['value']) && 0 === strpos($values['value'], '*')) {
  130. $refName = substr($values['value'], 1);
  131. if (!array_key_exists($refName, $this->refs)) {
  132. throw new ParseException(sprintf('Reference "%s" does not exist.', $refName), $this->getRealCurrentLineNb() + 1, $this->currentLine);
  133. }
  134. $refValue = $this->refs[$refName];
  135. if (!is_array($refValue)) {
  136. throw new ParseException('YAML merge keys used with a scalar value instead of an array.', $this->getRealCurrentLineNb() + 1, $this->currentLine);
  137. }
  138. foreach ($refValue as $key => $value) {
  139. if (!isset($data[$key])) {
  140. $data[$key] = $value;
  141. }
  142. }
  143. } else {
  144. if (isset($values['value']) && $values['value'] !== '') {
  145. $value = $values['value'];
  146. } else {
  147. $value = $this->getNextEmbedBlock();
  148. }
  149. $c = $this->getRealCurrentLineNb() + 1;
  150. $parser = new self($c);
  151. $parser->refs = &$this->refs;
  152. $parsed = $parser->parse($value, $exceptionOnInvalidType, $objectSupport, $objectForMap);
  153. if (!is_array($parsed)) {
  154. throw new ParseException('YAML merge keys used with a scalar value instead of an array.', $this->getRealCurrentLineNb() + 1, $this->currentLine);
  155. }
  156. if (isset($parsed[0])) {
  157. // If the value associated with the merge key is a sequence, then this sequence is expected to contain mapping nodes
  158. // and each of these nodes is merged in turn according to its order in the sequence. Keys in mapping nodes earlier
  159. // in the sequence override keys specified in later mapping nodes.
  160. foreach ($parsed as $parsedItem) {
  161. if (!is_array($parsedItem)) {
  162. throw new ParseException('Merge items must be arrays.', $this->getRealCurrentLineNb() + 1, $parsedItem);
  163. }
  164. foreach ($parsedItem as $key => $value) {
  165. if (!isset($data[$key])) {
  166. $data[$key] = $value;
  167. }
  168. }
  169. }
  170. } else {
  171. // If the value associated with the key is a single mapping node, each of its key/value pairs is inserted into the
  172. // current mapping, unless the key already exists in it.
  173. foreach ($parsed as $key => $value) {
  174. if (!isset($data[$key])) {
  175. $data[$key] = $value;
  176. }
  177. }
  178. }
  179. }
  180. } elseif (isset($values['value']) && preg_match('#^&(?P<ref>[^ ]+) *(?P<value>.*)#u', $values['value'], $matches)) {
  181. $isRef = $matches['ref'];
  182. $values['value'] = $matches['value'];
  183. }
  184. if ($mergeNode) {
  185. // Merge keys
  186. } elseif (!isset($values['value']) || '' == trim($values['value'], ' ') || 0 === strpos(ltrim($values['value'], ' '), '#')) {
  187. // hash
  188. // if next line is less indented or equal, then it means that the current value is null
  189. if (!$this->isNextLineIndented() && !$this->isNextLineUnIndentedCollection()) {
  190. // Spec: Keys MUST be unique; first one wins.
  191. // But overwriting is allowed when a merge node is used in current block.
  192. if ($allowOverwrite || !isset($data[$key])) {
  193. $data[$key] = null;
  194. }
  195. } else {
  196. $c = $this->getRealCurrentLineNb() + 1;
  197. $parser = new self($c);
  198. $parser->refs = &$this->refs;
  199. $value = $parser->parse($this->getNextEmbedBlock(), $exceptionOnInvalidType, $objectSupport, $objectForMap);
  200. // Spec: Keys MUST be unique; first one wins.
  201. // But overwriting is allowed when a merge node is used in current block.
  202. if ($allowOverwrite || !isset($data[$key])) {
  203. $data[$key] = $value;
  204. }
  205. }
  206. } else {
  207. $value = $this->parseValue($values['value'], $exceptionOnInvalidType, $objectSupport, $objectForMap, $context);
  208. // Spec: Keys MUST be unique; first one wins.
  209. // But overwriting is allowed when a merge node is used in current block.
  210. if ($allowOverwrite || !isset($data[$key])) {
  211. $data[$key] = $value;
  212. }
  213. }
  214. if ($isRef) {
  215. $this->refs[$isRef] = $data[$key];
  216. }
  217. } else {
  218. // multiple documents are not supported
  219. if ('---' === $this->currentLine) {
  220. throw new ParseException('Multiple documents are not supported.');
  221. }
  222. // 1-liner optionally followed by newline(s)
  223. if (is_string($value) && $this->lines[0] === trim($value)) {
  224. try {
  225. $value = Inline::parse($this->lines[0], $exceptionOnInvalidType, $objectSupport, $objectForMap, $this->refs);
  226. } catch (ParseException $e) {
  227. $e->setParsedLine($this->getRealCurrentLineNb() + 1);
  228. $e->setSnippet($this->currentLine);
  229. throw $e;
  230. }
  231. if (is_array($value)) {
  232. $first = reset($value);
  233. if (is_string($first) && 0 === strpos($first, '*')) {
  234. $data = array();
  235. foreach ($value as $alias) {
  236. $data[] = $this->refs[substr($alias, 1)];
  237. }
  238. $value = $data;
  239. }
  240. }
  241. if (isset($mbEncoding)) {
  242. mb_internal_encoding($mbEncoding);
  243. }
  244. return $value;
  245. }
  246. switch (preg_last_error()) {
  247. case PREG_INTERNAL_ERROR:
  248. $error = 'Internal PCRE error.';
  249. break;
  250. case PREG_BACKTRACK_LIMIT_ERROR:
  251. $error = 'pcre.backtrack_limit reached.';
  252. break;
  253. case PREG_RECURSION_LIMIT_ERROR:
  254. $error = 'pcre.recursion_limit reached.';
  255. break;
  256. case PREG_BAD_UTF8_ERROR:
  257. $error = 'Malformed UTF-8 data.';
  258. break;
  259. case PREG_BAD_UTF8_OFFSET_ERROR:
  260. $error = 'Offset doesn\'t correspond to the begin of a valid UTF-8 code point.';
  261. break;
  262. default:
  263. $error = 'Unable to parse.';
  264. }
  265. throw new ParseException($error, $this->getRealCurrentLineNb() + 1, $this->currentLine);
  266. }
  267. }
  268. if (isset($mbEncoding)) {
  269. mb_internal_encoding($mbEncoding);
  270. }
  271. if ($objectForMap && !is_object($data) && 'mapping' === $context) {
  272. $object = new \stdClass();
  273. foreach ($data as $key => $value) {
  274. $object->$key = $value;
  275. }
  276. $data = $object;
  277. }
  278. return empty($data) ? null : $data;
  279. }
  280. /**
  281. * Returns the current line number (takes the offset into account).
  282. *
  283. * @return int The current line number
  284. */
  285. private function getRealCurrentLineNb()
  286. {
  287. return $this->currentLineNb + $this->offset;
  288. }
  289. /**
  290. * Returns the current line indentation.
  291. *
  292. * @return int The current line indentation
  293. */
  294. private function getCurrentLineIndentation()
  295. {
  296. return strlen($this->currentLine) - strlen(ltrim($this->currentLine, ' '));
  297. }
  298. /**
  299. * Returns the next embed block of YAML.
  300. *
  301. * @param int $indentation The indent level at which the block is to be read, or null for default
  302. * @param bool $inSequence True if the enclosing data structure is a sequence
  303. *
  304. * @return string A YAML string
  305. *
  306. * @throws ParseException When indentation problem are detected
  307. */
  308. private function getNextEmbedBlock($indentation = null, $inSequence = false)
  309. {
  310. $oldLineIndentation = $this->getCurrentLineIndentation();
  311. $blockScalarIndentations = array();
  312. if ($this->isBlockScalarHeader()) {
  313. $blockScalarIndentations[] = $this->getCurrentLineIndentation();
  314. }
  315. if (!$this->moveToNextLine()) {
  316. return;
  317. }
  318. if (null === $indentation) {
  319. $newIndent = $this->getCurrentLineIndentation();
  320. $unindentedEmbedBlock = $this->isStringUnIndentedCollectionItem();
  321. if (!$this->isCurrentLineEmpty() && 0 === $newIndent && !$unindentedEmbedBlock) {
  322. throw new ParseException('Indentation problem.', $this->getRealCurrentLineNb() + 1, $this->currentLine);
  323. }
  324. } else {
  325. $newIndent = $indentation;
  326. }
  327. $data = array();
  328. if ($this->getCurrentLineIndentation() >= $newIndent) {
  329. $data[] = substr($this->currentLine, $newIndent);
  330. } else {
  331. $this->moveToPreviousLine();
  332. return;
  333. }
  334. if ($inSequence && $oldLineIndentation === $newIndent && isset($data[0][0]) && '-' === $data[0][0]) {
  335. // the previous line contained a dash but no item content, this line is a sequence item with the same indentation
  336. // and therefore no nested list or mapping
  337. $this->moveToPreviousLine();
  338. return;
  339. }
  340. $isItUnindentedCollection = $this->isStringUnIndentedCollectionItem();
  341. if (empty($blockScalarIndentations) && $this->isBlockScalarHeader()) {
  342. $blockScalarIndentations[] = $this->getCurrentLineIndentation();
  343. }
  344. $previousLineIndentation = $this->getCurrentLineIndentation();
  345. while ($this->moveToNextLine()) {
  346. $indent = $this->getCurrentLineIndentation();
  347. // terminate all block scalars that are more indented than the current line
  348. if (!empty($blockScalarIndentations) && $indent < $previousLineIndentation && trim($this->currentLine) !== '') {
  349. foreach ($blockScalarIndentations as $key => $blockScalarIndentation) {
  350. if ($blockScalarIndentation >= $this->getCurrentLineIndentation()) {
  351. unset($blockScalarIndentations[$key]);
  352. }
  353. }
  354. }
  355. if (empty($blockScalarIndentations) && !$this->isCurrentLineComment() && $this->isBlockScalarHeader()) {
  356. $blockScalarIndentations[] = $this->getCurrentLineIndentation();
  357. }
  358. $previousLineIndentation = $indent;
  359. if ($isItUnindentedCollection && !$this->isStringUnIndentedCollectionItem() && $newIndent === $indent) {
  360. $this->moveToPreviousLine();
  361. break;
  362. }
  363. if ($this->isCurrentLineBlank()) {
  364. $data[] = substr($this->currentLine, $newIndent);
  365. continue;
  366. }
  367. // we ignore "comment" lines only when we are not inside a scalar block
  368. if (empty($blockScalarIndentations) && $this->isCurrentLineComment()) {
  369. continue;
  370. }
  371. if ($indent >= $newIndent) {
  372. $data[] = substr($this->currentLine, $newIndent);
  373. } elseif (0 == $indent) {
  374. $this->moveToPreviousLine();
  375. break;
  376. } else {
  377. throw new ParseException('Indentation problem.', $this->getRealCurrentLineNb() + 1, $this->currentLine);
  378. }
  379. }
  380. return implode("\n", $data);
  381. }
  382. /**
  383. * Moves the parser to the next line.
  384. *
  385. * @return bool
  386. */
  387. private function moveToNextLine()
  388. {
  389. if ($this->currentLineNb >= count($this->lines) - 1) {
  390. return false;
  391. }
  392. $this->currentLine = $this->lines[++$this->currentLineNb];
  393. return true;
  394. }
  395. /**
  396. * Moves the parser to the previous line.
  397. */
  398. private function moveToPreviousLine()
  399. {
  400. $this->currentLine = $this->lines[--$this->currentLineNb];
  401. }
  402. /**
  403. * Parses a YAML value.
  404. *
  405. * @param string $value A YAML value
  406. * @param bool $exceptionOnInvalidType True if an exception must be thrown on invalid types false otherwise
  407. * @param bool $objectSupport True if object support is enabled, false otherwise
  408. * @param bool $objectForMap true if maps should return a stdClass instead of array()
  409. * @param string $context The parser context (either sequence or mapping)
  410. *
  411. * @return mixed A PHP value
  412. *
  413. * @throws ParseException When reference does not exist
  414. */
  415. private function parseValue($value, $exceptionOnInvalidType, $objectSupport, $objectForMap, $context)
  416. {
  417. if (0 === strpos($value, '*')) {
  418. if (false !== $pos = strpos($value, '#')) {
  419. $value = substr($value, 1, $pos - 2);
  420. } else {
  421. $value = substr($value, 1);
  422. }
  423. if (!array_key_exists($value, $this->refs)) {
  424. throw new ParseException(sprintf('Reference "%s" does not exist.', $value), $this->currentLine);
  425. }
  426. return $this->refs[$value];
  427. }
  428. if (preg_match('/^'.self::BLOCK_SCALAR_HEADER_PATTERN.'$/', $value, $matches)) {
  429. $modifiers = isset($matches['modifiers']) ? $matches['modifiers'] : '';
  430. return $this->parseBlockScalar($matches['separator'], preg_replace('#\d+#', '', $modifiers), (int) abs($modifiers));
  431. }
  432. try {
  433. $parsedValue = Inline::parse($value, $exceptionOnInvalidType, $objectSupport, $objectForMap, $this->refs);
  434. if ('mapping' === $context && '"' !== $value[0] && "'" !== $value[0] && '[' !== $value[0] && '{' !== $value[0] && '!' !== $value[0] && false !== strpos($parsedValue, ': ')) {
  435. @trigger_error(sprintf('Using a colon in the unquoted mapping value "%s" in line %d is deprecated since Symfony 2.8 and will throw a ParseException in 3.0.', $value, $this->getRealCurrentLineNb() + 1), E_USER_DEPRECATED);
  436. // to be thrown in 3.0
  437. // throw new ParseException('A colon cannot be used in an unquoted mapping value.');
  438. }
  439. return $parsedValue;
  440. } catch (ParseException $e) {
  441. $e->setParsedLine($this->getRealCurrentLineNb() + 1);
  442. $e->setSnippet($this->currentLine);
  443. throw $e;
  444. }
  445. }
  446. /**
  447. * Parses a block scalar.
  448. *
  449. * @param string $style The style indicator that was used to begin this block scalar (| or >)
  450. * @param string $chomping The chomping indicator that was used to begin this block scalar (+ or -)
  451. * @param int $indentation The indentation indicator that was used to begin this block scalar
  452. *
  453. * @return string The text value
  454. */
  455. private function parseBlockScalar($style, $chomping = '', $indentation = 0)
  456. {
  457. $notEOF = $this->moveToNextLine();
  458. if (!$notEOF) {
  459. return '';
  460. }
  461. $isCurrentLineBlank = $this->isCurrentLineBlank();
  462. $blockLines = array();
  463. // leading blank lines are consumed before determining indentation
  464. while ($notEOF && $isCurrentLineBlank) {
  465. // newline only if not EOF
  466. if ($notEOF = $this->moveToNextLine()) {
  467. $blockLines[] = '';
  468. $isCurrentLineBlank = $this->isCurrentLineBlank();
  469. }
  470. }
  471. // determine indentation if not specified
  472. if (0 === $indentation) {
  473. if (preg_match('/^ +/', $this->currentLine, $matches)) {
  474. $indentation = strlen($matches[0]);
  475. }
  476. }
  477. if ($indentation > 0) {
  478. $pattern = sprintf('/^ {%d}(.*)$/', $indentation);
  479. while (
  480. $notEOF && (
  481. $isCurrentLineBlank ||
  482. preg_match($pattern, $this->currentLine, $matches)
  483. )
  484. ) {
  485. if ($isCurrentLineBlank && strlen($this->currentLine) > $indentation) {
  486. $blockLines[] = substr($this->currentLine, $indentation);
  487. } elseif ($isCurrentLineBlank) {
  488. $blockLines[] = '';
  489. } else {
  490. $blockLines[] = $matches[1];
  491. }
  492. // newline only if not EOF
  493. if ($notEOF = $this->moveToNextLine()) {
  494. $isCurrentLineBlank = $this->isCurrentLineBlank();
  495. }
  496. }
  497. } elseif ($notEOF) {
  498. $blockLines[] = '';
  499. }
  500. if ($notEOF) {
  501. $blockLines[] = '';
  502. $this->moveToPreviousLine();
  503. }
  504. // folded style
  505. if ('>' === $style) {
  506. $text = '';
  507. $previousLineIndented = false;
  508. $previousLineBlank = false;
  509. for ($i = 0; $i < count($blockLines); ++$i) {
  510. if ('' === $blockLines[$i]) {
  511. $text .= "\n";
  512. $previousLineIndented = false;
  513. $previousLineBlank = true;
  514. } elseif (' ' === $blockLines[$i][0]) {
  515. $text .= "\n".$blockLines[$i];
  516. $previousLineIndented = true;
  517. $previousLineBlank = false;
  518. } elseif ($previousLineIndented) {
  519. $text .= "\n".$blockLines[$i];
  520. $previousLineIndented = false;
  521. $previousLineBlank = false;
  522. } elseif ($previousLineBlank || 0 === $i) {
  523. $text .= $blockLines[$i];
  524. $previousLineIndented = false;
  525. $previousLineBlank = false;
  526. } else {
  527. $text .= ' '.$blockLines[$i];
  528. $previousLineIndented = false;
  529. $previousLineBlank = false;
  530. }
  531. }
  532. } else {
  533. $text = implode("\n", $blockLines);
  534. }
  535. // deal with trailing newlines
  536. if ('' === $chomping) {
  537. $text = preg_replace('/\n+$/', "\n", $text);
  538. } elseif ('-' === $chomping) {
  539. $text = preg_replace('/\n+$/', '', $text);
  540. }
  541. return $text;
  542. }
  543. /**
  544. * Returns true if the next line is indented.
  545. *
  546. * @return bool Returns true if the next line is indented, false otherwise
  547. */
  548. private function isNextLineIndented()
  549. {
  550. $currentIndentation = $this->getCurrentLineIndentation();
  551. $EOF = !$this->moveToNextLine();
  552. while (!$EOF && $this->isCurrentLineEmpty()) {
  553. $EOF = !$this->moveToNextLine();
  554. }
  555. if ($EOF) {
  556. return false;
  557. }
  558. $ret = false;
  559. if ($this->getCurrentLineIndentation() > $currentIndentation) {
  560. $ret = true;
  561. }
  562. $this->moveToPreviousLine();
  563. return $ret;
  564. }
  565. /**
  566. * Returns true if the current line is blank or if it is a comment line.
  567. *
  568. * @return bool Returns true if the current line is empty or if it is a comment line, false otherwise
  569. */
  570. private function isCurrentLineEmpty()
  571. {
  572. return $this->isCurrentLineBlank() || $this->isCurrentLineComment();
  573. }
  574. /**
  575. * Returns true if the current line is blank.
  576. *
  577. * @return bool Returns true if the current line is blank, false otherwise
  578. */
  579. private function isCurrentLineBlank()
  580. {
  581. return '' == trim($this->currentLine, ' ');
  582. }
  583. /**
  584. * Returns true if the current line is a comment line.
  585. *
  586. * @return bool Returns true if the current line is a comment line, false otherwise
  587. */
  588. private function isCurrentLineComment()
  589. {
  590. //checking explicitly the first char of the trim is faster than loops or strpos
  591. $ltrimmedLine = ltrim($this->currentLine, ' ');
  592. return '' !== $ltrimmedLine && $ltrimmedLine[0] === '#';
  593. }
  594. /**
  595. * Cleanups a YAML string to be parsed.
  596. *
  597. * @param string $value The input YAML string
  598. *
  599. * @return string A cleaned up YAML string
  600. */
  601. private function cleanup($value)
  602. {
  603. $value = str_replace(array("\r\n", "\r"), "\n", $value);
  604. // strip YAML header
  605. $count = 0;
  606. $value = preg_replace('#^\%YAML[: ][\d\.]+.*\n#u', '', $value, -1, $count);
  607. $this->offset += $count;
  608. // remove leading comments
  609. $trimmedValue = preg_replace('#^(\#.*?\n)+#s', '', $value, -1, $count);
  610. if ($count == 1) {
  611. // items have been removed, update the offset
  612. $this->offset += substr_count($value, "\n") - substr_count($trimmedValue, "\n");
  613. $value = $trimmedValue;
  614. }
  615. // remove start of the document marker (---)
  616. $trimmedValue = preg_replace('#^\-\-\-.*?\n#s', '', $value, -1, $count);
  617. if ($count == 1) {
  618. // items have been removed, update the offset
  619. $this->offset += substr_count($value, "\n") - substr_count($trimmedValue, "\n");
  620. $value = $trimmedValue;
  621. // remove end of the document marker (...)
  622. $value = preg_replace('#\.\.\.\s*$#', '', $value);
  623. }
  624. return $value;
  625. }
  626. /**
  627. * Returns true if the next line starts unindented collection.
  628. *
  629. * @return bool Returns true if the next line starts unindented collection, false otherwise
  630. */
  631. private function isNextLineUnIndentedCollection()
  632. {
  633. $currentIndentation = $this->getCurrentLineIndentation();
  634. $notEOF = $this->moveToNextLine();
  635. while ($notEOF && $this->isCurrentLineEmpty()) {
  636. $notEOF = $this->moveToNextLine();
  637. }
  638. if (false === $notEOF) {
  639. return false;
  640. }
  641. $ret = false;
  642. if (
  643. $this->getCurrentLineIndentation() == $currentIndentation
  644. &&
  645. $this->isStringUnIndentedCollectionItem()
  646. ) {
  647. $ret = true;
  648. }
  649. $this->moveToPreviousLine();
  650. return $ret;
  651. }
  652. /**
  653. * Returns true if the string is un-indented collection item.
  654. *
  655. * @return bool Returns true if the string is un-indented collection item, false otherwise
  656. */
  657. private function isStringUnIndentedCollectionItem()
  658. {
  659. return 0 === strpos($this->currentLine, '- ');
  660. }
  661. /**
  662. * Tests whether or not the current line is the header of a block scalar.
  663. *
  664. * @return bool
  665. */
  666. private function isBlockScalarHeader()
  667. {
  668. return (bool) preg_match('~'.self::BLOCK_SCALAR_HEADER_PATTERN.'$~', $this->currentLine);
  669. }
  670. }