Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Parser.php

function Parser::getNextEmbedBlock

Returns the next embed block of YAML.

Parameters

int|null $indentation The indent level at which the block is to be read, or null for default:

bool $inSequence True if the enclosing data structure is a sequence:

Throws

ParseException When indentation problem are detected

1 call to Parser::getNextEmbedBlock()
Parser::doParse in vendor/symfony/yaml/Parser.php

File

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

Class

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

Namespace

Symfony\Component\Yaml

Code

private function getNextEmbedBlock(?int $indentation = null, bool $inSequence = false) : string {
    $oldLineIndentation = $this->getCurrentLineIndentation();
    if (!$this->moveToNextLine()) {
        return '';
    }
    if (null === $indentation) {
        $newIndent = null;
        $movements = 0;
        do {
            $EOF = false;
            // empty and comment-like lines do not influence the indentation depth
            if ($this->isCurrentLineEmpty() || $this->isCurrentLineComment()) {
                $EOF = !$this->moveToNextLine();
                if (!$EOF) {
                    ++$movements;
                }
            }
            else {
                $newIndent = $this->getCurrentLineIndentation();
            }
        } while (!$EOF && null === $newIndent);
        for ($i = 0; $i < $movements; ++$i) {
            $this->moveToPreviousLine();
        }
        $unindentedEmbedBlock = $this->isStringUnIndentedCollectionItem();
        if (!$this->isCurrentLineEmpty() && 0 === $newIndent && !$unindentedEmbedBlock) {
            throw new ParseException('Indentation problem.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
        }
    }
    else {
        $newIndent = $indentation;
    }
    $data = [];
    if ($this->getCurrentLineIndentation() >= $newIndent) {
        $data[] = substr($this->currentLine, $newIndent ?? 0);
    }
    elseif ($this->isCurrentLineEmpty() || $this->isCurrentLineComment()) {
        $data[] = $this->currentLine;
    }
    else {
        $this->moveToPreviousLine();
        return '';
    }
    if ($inSequence && $oldLineIndentation === $newIndent && isset($data[0][0]) && '-' === $data[0][0]) {
        // the previous line contained a dash but no item content, this line is a sequence item with the same indentation
        // and therefore no nested list or mapping
        $this->moveToPreviousLine();
        return '';
    }
    $isItUnindentedCollection = $this->isStringUnIndentedCollectionItem();
    $isItComment = $this->isCurrentLineComment();
    while ($this->moveToNextLine()) {
        if ($isItComment && !$isItUnindentedCollection) {
            $isItUnindentedCollection = $this->isStringUnIndentedCollectionItem();
            $isItComment = $this->isCurrentLineComment();
        }
        $indent = $this->getCurrentLineIndentation();
        if ($isItUnindentedCollection && !$this->isCurrentLineEmpty() && !$this->isStringUnIndentedCollectionItem() && $newIndent === $indent) {
            $this->moveToPreviousLine();
            break;
        }
        if ($this->isCurrentLineBlank()) {
            $data[] = substr($this->currentLine, $newIndent ?? 0);
            continue;
        }
        if ($indent >= $newIndent) {
            $data[] = substr($this->currentLine, $newIndent ?? 0);
        }
        elseif ($this->isCurrentLineComment()) {
            $data[] = $this->currentLine;
        }
        elseif (0 == $indent) {
            $this->moveToPreviousLine();
            break;
        }
        else {
            throw new ParseException('Indentation problem.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
        }
    }
    return implode("\n", $data);
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal