protected function indexControlStructure(
        string $template,
        string $structure,
        array $additionalComponents = []
    ): string {
        $structureDepthCounter = 0;
        $levelCounter = [];

        return preg_replace_callback(
            '/\{%\s*(?<structure>' . $this->getControlStructureRegEx($structure, $additionalComponents) . ')/i',
            function (array $matches) use (&$structureDepthCounter, &$levelCounter, $additionalComponents): string {
                if (in_array($matches['structure'], $additionalComponents)) {
                    return sprintf(
                        '%s-%s-%s-',
                        $matches[0],
                        $levelCounter[$structureDepthCounter - 1],
                        ($structureDepthCounter - 1)
                    );
                }

                $levelCounter[$structureDepthCounter] = $levelCounter[$structureDepthCounter] ?? 0;
                $isEndTag = strpos($matches['structure'], 'end') === 0;
                ($isEndTag) ? --$structureDepthCounter : $levelCounter[$structureDepthCounter]++;

                return sprintf(
                    '%s-%s-%s-',
                    $matches[0],
                    $levelCounter[$structureDepthCounter],
                    ($isEndTag ? $structureDepthCounter : $structureDepthCounter++)
                );
            },
            $template
        );
    }
protected function indexControlStructure(
        string $template,
        string $structure,
        array $additionalComponents = []
    ): string {
        $structureDepthCounter = 0;
        $levelCounter = [];

        return preg_replace_callback(
            '/\{%\s*(?<structure>' . $this->getControlStructureRegEx($structure, $additionalComponents) . ')/i',
            function (array $matches) use (&$structureDepthCounter, &$levelCounter, $additionalComponents): string {
                if (in_array($matches['structure'], $additionalComponents)) {
                    return sprintf(
                        '%s-%s-%s-',
                        $matches[0],
                        $levelCounter[$structureDepthCounter - 1],
                        ($structureDepthCounter - 1)
                    );
                }

                $levelCounter[$structureDepthCounter] = $levelCounter[$structureDepthCounter] ?? 0;
                $isEndTag = strpos($matches['structure'], 'end') === 0;
                ($isEndTag) ? --$structureDepthCounter : $levelCounter[$structureDepthCounter]++;

                return sprintf(
                    '%s-%s-%s-',
                    $matches[0],
                    $levelCounter[$structureDepthCounter],
                    ($isEndTag ? $structureDepthCounter : $structureDepthCounter++)
                );
            },
            $template
        );
    }
protected function indexControlStructure ( string $ template , string $ structure , array $ additionalComponents = [ ] ) : string { $ structureDepthCounter = 0 ; $ levelCounter = [ ] ; return preg_replace_callback ( '/\{%\s*(?<structure>' . $ this -> getControlStructureRegEx ( $ structure , $ additionalComponents ) . ')/i' , function ( array $ matches ) use ( & $ structureDepthCounter , & $ levelCounter , $ additionalComponents ) : string { if ( in_array ( $ matches [ 'structure' ] , $ additionalComponents ) ) { return sprintf ( '%s-%s-%s-' , $ matches [ 0 ] , $ levelCounter [ $ structureDepthCounter - 1 ] , ( $ structureDepthCounter - 1 ) ) ; } $ levelCounter [ $ structureDepthCounter ] = $ levelCounter [ $ structureDepthCounter ] ?? 0 ; $ isEndTag = strpos ( $ matches [ 'structure' ] , 'end' ) === 0 ; ( $ isEndTag ) ? -- $ structureDepthCounter : $ levelCounter [ $ structureDepthCounter ] ++ ; return sprintf ( '%s-%s-%s-' , $ matches [ 0 ] , $ levelCounter [ $ structureDepthCounter ] , ( $ isEndTag ? $ structureDepthCounter : $ structureDepthCounter ++ ) ) ; } , $ template ) ; }
Index a control structure in a given template section so a handling of nested control structures of the same
type can be offered

@param string $template             The template section
@param string $structure            The control structure (eg. 'foreach', 'if')
@param array  $additionalComponents [optional] Holds additional components for the structure (eg. 'else')

@return string
Index a control structure in a given template section so a handling of nested control structures of the same type can be offered