Automattic\WooCommerce\Vendor\GraphQL\Language

BlockString::printpublic staticWC 1.0

Print a block string in the indented block form by adding a leading and trailing blank line. However, if a block string starts with whitespace and is a single-line, adding a leading blank line would strip that whitespace.

Method of the class: BlockString{}

No Hooks.

Returns

null. Nothing (null).

Usage

$result = BlockString::print( $value ): string;
$value(string) (required)
.

BlockString::print() code WC 10.9.1

public static function print(string $value): string
{
    $escapedValue = str_replace('"""', '\\"""', $value);

    // Expand a block string's raw value into independent lines.
    $lines = Utils::splitLines($escapedValue);
    $isSingleLine = count($lines) === 1;

    // If common indentation is found we can fix some of those cases by adding leading new line
    $forceLeadingNewLine = count($lines) > 1;
    foreach ($lines as $i => $line) {
        if ($i === 0) {
            continue;
        }

        if ($line !== '' && preg_match('/^\s/', $line) !== 1) {
            $forceLeadingNewLine = false;
        }
    }

    // Trailing triple quotes just looks confusing but doesn't force trailing new line
    $hasTrailingTripleQuotes = preg_match('/\\\\"""$/', $escapedValue) === 1;

    // Trailing quote (single or double) or slash forces trailing new line
    $hasTrailingQuote = preg_match('/"$/', $value) === 1 && ! $hasTrailingTripleQuotes;
    $hasTrailingSlash = preg_match('/\\\\$/', $value) === 1;
    $forceTrailingNewline = $hasTrailingQuote || $hasTrailingSlash;

    // add leading and trailing new lines only if it improves readability
    $printAsMultipleLines = ! $isSingleLine
        || mb_strlen($value) > 70
        || $forceTrailingNewline
        || $forceLeadingNewLine
        || $hasTrailingTripleQuotes;

    $result = '';

    // Format a multi-line block quote to account for leading space.
    $skipLeadingNewLine = $isSingleLine && preg_match('/^\s/', $value) === 1;
    if (($printAsMultipleLines && ! $skipLeadingNewLine) || $forceLeadingNewLine) {
        $result .= "\n";
    }

    $result .= $escapedValue;
    if ($printAsMultipleLines) {
        $result .= "\n";
    }

    return '"""' . $result . '"""';
}