Parses a folded scalar.
string $separator The separator that was used to begin this folded scalar (| or >):
string $indicator The indicator that was used to begin this folded scalar (+ or -):
integer $indentation The indentation that was used to begin this folded scalar:
string The text value
private function parseFoldedScalar($separator, $indicator = '', $indentation = 0) {
$separator = '|' == $separator ? "\n" : ' ';
$text = '';
$notEOF = $this
->moveToNextLine();
while ($notEOF && $this
->isCurrentLineBlank()) {
$text .= "\n";
$notEOF = $this
->moveToNextLine();
}
if (!$notEOF) {
return '';
}
if (!preg_match('#^(?P<indent>' . ($indentation ? str_repeat(' ', $indentation) : ' +') . ')(?P<text>.*)$#u', $this->currentLine, $matches)) {
$this
->moveToPreviousLine();
return '';
}
$textIndent = $matches['indent'];
$previousIndent = 0;
$text .= $matches['text'] . $separator;
while ($this->currentLineNb + 1 < count($this->lines)) {
$this
->moveToNextLine();
if (preg_match('#^(?P<indent> {' . strlen($textIndent) . ',})(?P<text>.+)$#u', $this->currentLine, $matches)) {
if (' ' == $separator && $previousIndent != $matches['indent']) {
$text = substr($text, 0, -1) . "\n";
}
$previousIndent = $matches['indent'];
$text .= str_repeat(' ', $diff = strlen($matches['indent']) - strlen($textIndent)) . $matches['text'] . ($diff ? "\n" : $separator);
}
elseif (preg_match('#^(?P<text> *)$#', $this->currentLine, $matches)) {
$text .= preg_replace('#^ {1,' . strlen($textIndent) . '}#', '', $matches['text']) . "\n";
}
else {
$this
->moveToPreviousLine();
break;
}
}
if (' ' == $separator) {
// replace last separator by a newline
$text = preg_replace('/ (\\n*)$/', "\n\$1", $text);
}
switch ($indicator) {
case '':
$text = preg_replace('#\\n+$#s', "\n", $text);
break;
case '+':
break;
case '-':
$text = preg_replace('#\\n+$#s', '', $text);
break;
}
return $text;
}