49 $this->currentLineNb = -1;
50 $this->currentLine =
'';
51 $this->lines = explode(
"\n", $this->
cleanup($value));
62 if (preg_match(
'#^\t+#', $this->currentLine))
67 $isRef = $isInPlace = $isProcessed =
false;
68 if (preg_match(
'#^\-((?P<leadspaces>\s+)(?P<value>.+?))?\s*$#', $this->currentLine, $values))
70 if (isset($values[
'value']) && preg_match(
'#^&(?P<ref>[^ ]+) *(?P<value>.*)#', $values[
'value'], $matches))
72 $isRef = $matches[
'ref'];
73 $values[
'value'] = $matches[
'value'];
77 if (!isset($values[
'value']) ||
'' == trim($values[
'value'],
' ') || 0 === strpos(ltrim($values[
'value'],
' '),
'#'))
86 if (isset($values[
'leadspaces'])
87 &&
' ' == $values[
'leadspaces']
95 $block = $values[
'value'];
101 $data[] = $parser->parse($block);
105 $data[] = $this->
parseValue($values[
'value']);
109 else if (preg_match(
'#^(?P<key>'.
Inline::REGEX_QUOTED_STRING.
'|[^ \'"].*?) *\:(\s+(?P<value>.+?))?\s*$#', $this->currentLine, $values))
115 if (isset($values[
'value']) &&
'*' === substr($values[
'value'], 0, 1))
117 $isInPlace = substr($values[
'value'], 1);
118 if (!array_key_exists($isInPlace, $this->refs))
125 if (isset($values[
'value']) && $values[
'value'] !==
'')
127 $value = $values[
'value'];
136 $parsed = $parser->parse($value);
139 if (!is_array($parsed))
143 else if (isset($parsed[0]))
146 foreach (array_reverse($parsed) as $parsedItem)
148 if (!is_array($parsedItem))
152 $merged = array_merge($parsedItem, $merged);
158 $merged = array_merge($merge, $parsed);
161 $isProcessed = $merged;
164 else if (isset($values[
'value']) && preg_match(
'#^&(?P<ref>[^ ]+) *(?P<value>.*)#', $values[
'value'], $matches))
166 $isRef = $matches[
'ref'];
167 $values[
'value'] = $matches[
'value'];
173 $data = $isProcessed;
176 else if (!isset($values[
'value']) ||
'' == trim($values[
'value'],
' ') || 0 === strpos(ltrim($values[
'value'],
' '),
'#'))
195 $data = $this->refs[$isInPlace];
199 $data[$key] = $this->
parseValue($values[
'value']);
206 if (2 == count($this->lines) && empty($this->lines[1]))
209 if (is_array($value))
211 $first = reset($value);
212 if (
'*' === substr($first, 0, 1))
215 foreach ($value as $alias)
217 $data[] = $this->refs[substr($alias, 1)];
226 switch (preg_last_error())
228 case PREG_INTERNAL_ERROR:
229 $error =
'Internal PCRE error on line';
231 case PREG_BACKTRACK_LIMIT_ERROR:
232 $error =
'pcre.backtrack_limit reached on line';
234 case PREG_RECURSION_LIMIT_ERROR:
235 $error =
'pcre.recursion_limit reached on line';
237 case PREG_BAD_UTF8_ERROR:
238 $error =
'Malformed UTF-8 data on line';
240 case PREG_BAD_UTF8_OFFSET_ERROR:
241 $error =
'Offset doesn\'t correspond to the begin of a valid UTF-8 code point on line';
244 $error =
'Unable to parse line';
252 $this->refs[$isRef] = end($data);
256 return empty($data) ? null : $data;
290 if (
null === $indentation)
301 $newIndent = $indentation;
304 $data = array(substr($this->currentLine, $newIndent));
312 $data[] = substr($this->currentLine, $newIndent);
320 if (preg_match(
'#^(?P<text> *)$#', $this->currentLine, $match))
323 $data[] = $match[
'text'];
325 else if ($indent >= $newIndent)
327 $data[] = substr($this->currentLine, $newIndent);
329 else if (0 == $indent)
341 return implode(
"\n", $data);
376 if (
'*' === substr($value, 0, 1))
378 if (
false !== $pos = strpos($value,
'#'))
380 $value = substr($value, 1, $pos - 2);
384 $value = substr($value, 1);
387 if (!array_key_exists($value, $this->refs))
389 throw new ParserException(sprintf(
'Reference "%s" does not exist (%s).', $value, $this->currentLine));
391 return $this->refs[$value];
394 if (preg_match(
'/^(?P<separator>\||>)(?P<modifiers>\+|\-|\d+|\+\d+|\-\d+|\d+\+|\d+\-)?(?P<comments> +#.*)?$/', $value, $matches))
396 $modifiers = isset($matches[
'modifiers']) ? $matches[
'modifiers'] :
'';
398 return $this->
parseFoldedScalar($matches[
'separator'], preg_replace(
'#\d+#',
'', $modifiers), intval(abs($modifiers)));
417 $separator =
'|' == $separator ?
"\n" :
' ';
434 if (!preg_match(
'#^(?P<indent>'.($indentation ? str_repeat(
' ', $indentation) :
' +').
')(?P<text>.*)$#', $this->currentLine, $matches))
441 $textIndent = $matches[
'indent'];
444 $text .= $matches[
'text'].$separator;
445 while ($this->currentLineNb + 1 < count($this->lines))
449 if (preg_match(
'#^(?P<indent> {'.strlen($textIndent).
',})(?P<text>.+)$#', $this->currentLine, $matches))
451 if (
' ' == $separator && $previousIndent != $matches[
'indent'])
453 $text = substr($text, 0, -1).
"\n";
455 $previousIndent = $matches[
'indent'];
457 $text .= str_repeat(
' ', $diff = strlen($matches[
'indent']) - strlen($textIndent)).$matches[
'text'].($diff ?
"\n" : $separator);
459 else if (preg_match(
'#^(?P<text> *)$#', $this->currentLine, $matches))
461 $text .= preg_replace(
'#^ {1,'.strlen($textIndent).
'}#',
'', $matches[
'text']).
"\n";
471 if (
' ' == $separator)
474 $text = preg_replace(
'/ (\n*)$/',
"\n$1", $text);
480 $text = preg_replace(
'#\n+$#s',
"\n", $text);
485 $text = preg_replace(
'#\n+$#s',
'', $text);
564 $value = str_replace(array(
"\r\n",
"\r"),
"\n", $value);
566 if (!preg_match(
"#\n$#", $value))
573 $value = preg_replace(
'#^\%YAML[: ][\d\.]+.*\n#s',
'', $value, -1, $count);
574 $this->offset += $count;
577 $trimmedValue = preg_replace(
'#^((\#.*?\n)|(\-\-\-.*?\n))*#s',
'', $value, -1, $count);
581 $this->offset += substr_count($value,
"\n") - substr_count($trimmedValue,
"\n");
582 $value = $trimmedValue;