protected function parsePattern($pattern)
    {
        $currentPos      = 0;
        $length          = strlen($pattern);
        $tokens          = [];
        $level           = 0;
        $quotedDelimiter = preg_quote($this->delimiter);

        while ($currentPos < $length) {
            preg_match('(\G(?P<literal>[^:{\[\]]*)(?P<token>[:\[\]]|$))', $pattern, $matches, 0, $currentPos);

            $currentPos += strlen($matches[0]);

            if (!empty($matches['literal'])) {
                $tokens[] = ['literal', $matches['literal']];
            }

            if ($matches['token'] === ':') {
                if (!preg_match(
                    '(\G(?P<name>[^:' . $quotedDelimiter . '{\[\]]+)(?:{(?P<delimiters>[^}]+)})?:?)',
                    $pattern,
                    $matches,
                    0,
                    $currentPos
                )) {
                    throw new Exception\RuntimeException('Found empty parameter name');
                }

                $tokens[] = [
                    'parameter',
                    $matches['name'],
                    isset($matches['delimiters']) ? $matches['delimiters'] : null
                ];

                $currentPos += strlen($matches[0]);
            } elseif ($matches['token'] === '[') {
                $tokens[] = ['optional-start'];
                $level++;
            } elseif ($matches['token'] === ']') {
                $tokens[] = ['optional-end'];
                $level--;

                if ($level < 0) {
                    throw new Exception\RuntimeException('Found closing bracket without matching opening bracket');
                }
            } else {
                break;
            }
        }

        if ($level > 0) {
            throw new Exception\RuntimeException('Found unbalanced brackets');
        }

        return $tokens;
    }
protected function parsePattern($pattern)
    {
        $currentPos      = 0;
        $length          = strlen($pattern);
        $tokens          = [];
        $level           = 0;
        $quotedDelimiter = preg_quote($this->delimiter);

        while ($currentPos < $length) {
            preg_match('(\G(?P<literal>[^:{\[\]]*)(?P<token>[:\[\]]|$))', $pattern, $matches, 0, $currentPos);

            $currentPos += strlen($matches[0]);

            if (!empty($matches['literal'])) {
                $tokens[] = ['literal', $matches['literal']];
            }

            if ($matches['token'] === ':') {
                if (!preg_match(
                    '(\G(?P<name>[^:' . $quotedDelimiter . '{\[\]]+)(?:{(?P<delimiters>[^}]+)})?:?)',
                    $pattern,
                    $matches,
                    0,
                    $currentPos
                )) {
                    throw new Exception\RuntimeException('Found empty parameter name');
                }

                $tokens[] = [
                    'parameter',
                    $matches['name'],
                    isset($matches['delimiters']) ? $matches['delimiters'] : null
                ];

                $currentPos += strlen($matches[0]);
            } elseif ($matches['token'] === '[') {
                $tokens[] = ['optional-start'];
                $level++;
            } elseif ($matches['token'] === ']') {
                $tokens[] = ['optional-end'];
                $level--;

                if ($level < 0) {
                    throw new Exception\RuntimeException('Found closing bracket without matching opening bracket');
                }
            } else {
                break;
            }
        }

        if ($level > 0) {
            throw new Exception\RuntimeException('Found unbalanced brackets');
        }

        return $tokens;
    }
protected function parsePattern ( $ pattern ) { $ currentPos = 0 ; $ length = strlen ( $ pattern ) ; $ tokens = [ ] ; $ level = 0 ; $ quotedDelimiter = preg_quote ( $ this -> delimiter ) ; while ( $ currentPos < $ length ) { preg_match ( '(\G(?P<literal>[^:{\[\]]*)(?P<token>[:\[\]]|$))' , $ pattern , $ matches , 0 , $ currentPos ) ; $ currentPos += strlen ( $ matches [ 0 ] ) ; if ( ! empty ( $ matches [ 'literal' ] ) ) { $ tokens [ ] = [ 'literal' , $ matches [ 'literal' ] ] ; } if ( $ matches [ 'token' ] === ':' ) { if ( ! preg_match ( '(\G(?P<name>[^:' . $ quotedDelimiter . '{\[\]]+)(?:{(?P<delimiters>[^}]+)})?:?)' , $ pattern , $ matches , 0 , $ currentPos ) ) { throw new Exception \ RuntimeException ( 'Found empty parameter name' ) ; } $ tokens [ ] = [ 'parameter' , $ matches [ 'name' ] , isset ( $ matches [ 'delimiters' ] ) ? $ matches [ 'delimiters' ] : null ] ; $ currentPos += strlen ( $ matches [ 0 ] ) ; } elseif ( $ matches [ 'token' ] === '[' ) { $ tokens [ ] = [ 'optional-start' ] ; $ level ++ ; } elseif ( $ matches [ 'token' ] === ']' ) { $ tokens [ ] = [ 'optional-end' ] ; $ level -- ; if ( $ level < 0 ) { throw new Exception \ RuntimeException ( 'Found closing bracket without matching opening bracket' ) ; } } else { break ; } } if ( $ level > 0 ) { throw new Exception \ RuntimeException ( 'Found unbalanced brackets' ) ; } return $ tokens ; }
Parses a pattern.

@param  string $pattern
@return array
@throws Exception\RuntimeException
Parses a pattern .