You are here

private function Parser::parseBlockScalar in Loft Data Grids 7.2

Same name and namespace in other branches
  1. 6.2 vendor/symfony/yaml/Symfony/Component/Yaml/Parser.php \Symfony\Component\Yaml\Parser::parseBlockScalar()

Parses a block scalar.

Parameters

string $style The style indicator that was used to begin this block scalar (| or >):

string $chomping The chomping indicator that was used to begin this block scalar (+ or -):

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

Return value

string The text value

1 call to Parser::parseBlockScalar()
Parser::parseValue in vendor/symfony/yaml/Parser.php
Parses a YAML value.

File

vendor/symfony/yaml/Parser.php, line 563

Class

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

Namespace

Symfony\Component\Yaml

Code

private function parseBlockScalar($style, $chomping = '', $indentation = 0) {
  $notEOF = $this
    ->moveToNextLine();
  if (!$notEOF) {
    return '';
  }
  $isCurrentLineBlank = $this
    ->isCurrentLineBlank();
  $blockLines = array();

  // leading blank lines are consumed before determining indentation
  while ($notEOF && $isCurrentLineBlank) {

    // newline only if not EOF
    if ($notEOF = $this
      ->moveToNextLine()) {
      $blockLines[] = '';
      $isCurrentLineBlank = $this
        ->isCurrentLineBlank();
    }
  }

  // determine indentation if not specified
  if (0 === $indentation) {
    if (self::preg_match('/^ +/', $this->currentLine, $matches)) {
      $indentation = strlen($matches[0]);
    }
  }
  if ($indentation > 0) {
    $pattern = sprintf('/^ {%d}(.*)$/', $indentation);
    while ($notEOF && ($isCurrentLineBlank || self::preg_match($pattern, $this->currentLine, $matches))) {
      if ($isCurrentLineBlank && strlen($this->currentLine) > $indentation) {
        $blockLines[] = substr($this->currentLine, $indentation);
      }
      elseif ($isCurrentLineBlank) {
        $blockLines[] = '';
      }
      else {
        $blockLines[] = $matches[1];
      }

      // newline only if not EOF
      if ($notEOF = $this
        ->moveToNextLine()) {
        $isCurrentLineBlank = $this
          ->isCurrentLineBlank();
      }
    }
  }
  elseif ($notEOF) {
    $blockLines[] = '';
  }
  if ($notEOF) {
    $blockLines[] = '';
    $this
      ->moveToPreviousLine();
  }
  elseif (!$notEOF && !$this
    ->isCurrentLineLastLineInDocument()) {
    $blockLines[] = '';
  }

  // folded style
  if ('>' === $style) {
    $text = '';
    $previousLineIndented = false;
    $previousLineBlank = false;
    for ($i = 0, $blockLinesCount = count($blockLines); $i < $blockLinesCount; ++$i) {
      if ('' === $blockLines[$i]) {
        $text .= "\n";
        $previousLineIndented = false;
        $previousLineBlank = true;
      }
      elseif (' ' === $blockLines[$i][0]) {
        $text .= "\n" . $blockLines[$i];
        $previousLineIndented = true;
        $previousLineBlank = false;
      }
      elseif ($previousLineIndented) {
        $text .= "\n" . $blockLines[$i];
        $previousLineIndented = false;
        $previousLineBlank = false;
      }
      elseif ($previousLineBlank || 0 === $i) {
        $text .= $blockLines[$i];
        $previousLineIndented = false;
        $previousLineBlank = false;
      }
      else {
        $text .= ' ' . $blockLines[$i];
        $previousLineIndented = false;
        $previousLineBlank = false;
      }
    }
  }
  else {
    $text = implode("\n", $blockLines);
  }

  // deal with trailing newlines
  if ('' === $chomping) {
    $text = preg_replace('/\\n+$/', "\n", $text);
  }
  elseif ('-' === $chomping) {
    $text = preg_replace('/\\n+$/', '', $text);
  }
  return $text;
}