YamlParser::parseFoldedScalar PHP Method

parseFoldedScalar() protected method

Parses a folded scalar.
protected parseFoldedScalar ( $separator, $indicator = '', $indentation ) : string
return string The text value
    protected 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>.*)$#', $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>.+)$#', $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);
            } else {
                if (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;
    }