Automattic\WooCommerce\Vendor\GraphQL\Language

BlockString::dedentBlockStringLinespublic staticWC 1.0

Produces the value of a block string from its parsed raw value, similar to CoffeeScript's block string, Python's docstring trim or Ruby's strip_heredoc.

This implements the Automattic\WooCommerce\Vendor\GraphQL spec's BlockStringValue() static algorithm.

Method of the class: BlockString{}

No Hooks.

Returns

null. Nothing (null).

Usage

$result = BlockString::dedentBlockStringLines( $rawString ): string;
$rawString(string) (required)
.

BlockString::dedentBlockStringLines() code WC 10.9.1

public static function dedentBlockStringLines(string $rawString): string
{
    $lines = Utils::splitLines($rawString);

    // Remove common indentation from all lines but first.
    $commonIndent = self::getIndentation($rawString);
    $linesLength = count($lines);

    if ($commonIndent > 0) {
        for ($i = 1; $i < $linesLength; ++$i) {
            $lines[$i] = mb_substr($lines[$i], $commonIndent);
        }
    }

    // Remove leading and trailing blank lines.
    $startLine = 0;
    while ($startLine < $linesLength && self::isBlank($lines[$startLine])) {
        ++$startLine;
    }

    $endLine = $linesLength;
    while ($endLine > $startLine && self::isBlank($lines[$endLine - 1])) {
        --$endLine;
    }

    // Return a string of the lines joined with U+000A.
    return implode("\n", array_slice($lines, $startLine, $endLine - $startLine));
}