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) {
$notEOF = $this
->moveToNextLine();
if (!$notEOF) {
return '';
}
$isCurrentLineBlank = $this
->isCurrentLineBlank();
$text = '';
// leading blank lines are consumed before determining indentation
while ($notEOF && $isCurrentLineBlank) {
// newline only if not EOF
if ($notEOF = $this
->moveToNextLine()) {
$text .= "\n";
$isCurrentLineBlank = $this
->isCurrentLineBlank();
}
}
// determine indentation if not specified
if (0 === $indentation) {
if (preg_match('/^ +/', $this->currentLine, $matches)) {
$indentation = strlen($matches[0]);
}
}
if ($indentation > 0) {
$pattern = sprintf('/^ {%d}(.*)$/', $indentation);
while ($notEOF && ($isCurrentLineBlank || preg_match($pattern, $this->currentLine, $matches))) {
if ($isCurrentLineBlank) {
$text .= substr($this->currentLine, $indentation);
}
else {
$text .= $matches[1];
}
// newline only if not EOF
if ($notEOF = $this
->moveToNextLine()) {
$text .= "\n";
$isCurrentLineBlank = $this
->isCurrentLineBlank();
}
}
}
elseif ($notEOF) {
$text .= "\n";
}
if ($notEOF) {
$this
->moveToPreviousLine();
}
// replace all non-trailing single newlines with spaces in folded blocks
if ('>' === $separator) {
preg_match('/(\\n*)$/', $text, $matches);
$text = preg_replace('/(?<!\\n)\\n(?!\\n)/', ' ', rtrim($text, "\n"));
$text .= $matches[1];
}
// deal with trailing newlines as indicated
if ('' === $indicator) {
$text = preg_replace('/\\n+$/s', "\n", $text);
}
elseif ('-' === $indicator) {
$text = preg_replace('/\\n+$/s', '', $text);
}
return $text;
}