You are here

private function Parser::parseFoldedScalar in Plug 7

Parses a folded scalar.

Parameters

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 -):

int $indentation The indentation that was used to begin this folded scalar:

Return value

string The text value

1 call to Parser::parseFoldedScalar()
Parser::parseValue in lib/Symfony/yaml/Symfony/Component/Yaml/Parser.php
Parses a YAML value.

File

lib/Symfony/yaml/Symfony/Component/Yaml/Parser.php, line 485

Class

Parser
Parser parses YAML strings to convert them to PHP arrays.

Namespace

Symfony\Component\Yaml

Code

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+$/', "\n", $text);
  }
  elseif ('-' === $indicator) {
    $text = preg_replace('/\\n+$/', '', $text);
  }
  return $text;
}