src/Parser/Parser.php
/**
     * Replaces variables like {{ Foo }} in a text with the actual variable content.
     *
     * @param LocationInterface     $location     The location.
     * @param string                $content      The text content.
     * @param ParseContextInterface $parseContext The parse context.
     * @param ParseErrorInterface[] $parseErrors  The parse errors.
     *
     * @return string|null The text content with variables replaces with the actual variable content or null if replace failed.
     */
Replaces variables like {{ Foo }} in a text with the actual variable content.
@param LocationInterface     $location     The location.
@param string                $content      The text content.
@param ParseContextInterface $parseContext The parse context.
@param ParseErrorInterface[] $parseErrors  The parse errors.

@return string|null The text content with variables replaces with the actual variable content or null if replace failed.
Replaces variables like {{ Foo }} in a text with the actual variable content . @param LocationInterface $location The location . @param string $content The text content . @param ParseContextInterface $parseContext The parse context . @param ParseErrorInterface [] $parseErrors The parse errors . @return string|null The text content with variables replaces with the actual variable content or null if replace failed .
private static function replaceVariables(LocationInterface $location, string $content, ParseContextInterface $parseContext, array &$parseErrors): ?string
    {
        $hasErrors = false;
        $result = preg_replace_callback(
            '/{{(.*?)}}/',
            function (array $matches) use ($location, $parseContext, &$parseErrors, &$hasErrors): string {
                $variableName = trim($matches[1]);
                $error = null;
                if ($variableName === '') {
                    $error = 'Missing variable: Missing variable name in "' . $matches[0] . '".';
                } elseif (!self::isValidVariableName($variableName)) {
                    $error = 'Invalid variable: Invalid variable name "' . $variableName . '" in "' . $matches[0] . '".';
                } elseif (!$parseContext->hasVariable($variableName)) {
                    $error = 'Invalid variable: No variable with name "' . $variableName . '" is set in "' . $matches[0] . '".';
                }
                if ($error !== null) {
                    $hasErrors = true;
                    $parseErrors[] = new ParseError($location, $error);
                    return '';
                }
                return $parseContext->getVariable($variableName);
            },
            $content,
        );
        return !$hasErrors ? $result : null;
    }
private static function replaceVariables ( LocationInterface $ location , string $ content , ParseContextInterface $ parseContext , array & $ parseErrors ) : ? string { $ hasErrors = false ; $ result = preg_replace_callback ( '/{{(.*?)}}/' , function ( array $ matches ) use ( $ location , $ parseContext , & $ parseErrors , & $ hasErrors ) : string { $ variableName = trim ( $ matches [ 1 ] ) ; $ error = null ; if ( $ variableName === '' ) { $ error = 'Missing variable: Missing variable name in "' . $ matches [ 0 ] . '".' ; } elseif ( ! self :: isValidVariableName ( $ variableName ) ) { $ error = 'Invalid variable: Invalid variable name "' . $ variableName . '" in "' . $ matches [ 0 ] . '".' ; } elseif ( ! $ parseContext -> hasVariable ( $ variableName ) ) { $ error = 'Invalid variable: No variable with name "' . $ variableName . '" is set in "' . $ matches [ 0 ] . '".' ; } if ( $ error !== null ) { $ hasErrors = true ; $ parseErrors [ ] = new ParseError ( $ location , $ error ) ; return '' ; } return $ parseContext -> getVariable ( $ variableName ) ; } , $ content , ) ; return ! $ hasErrors ? $ result : null ; }
Replaces variables like {{ Foo }} in a text with the actual variable content.
Replaces variables like {{ Foo }} in a text with the actual variable content .