0 follower

Final Class Yiisoft\Strings\CombinedRegexp

InheritanceYiisoft\Strings\CombinedRegexp » Yiisoft\Strings\AbstractCombinedRegexp

CombinedRegexp optimizes matching of multiple regular expressions.

Read more about the concept in {@see https://nikic.github.io/2014/02/18/Fast-request-routing-using-regular-expressions.html}.

Public Methods

Hide inherited methods

Method Description Defined By
__construct() Yiisoft\Strings\CombinedRegexp
getCompiledPattern() Yiisoft\Strings\CombinedRegexp
getFlags() Yiisoft\Strings\CombinedRegexp
getMatchingPattern() Returns pattern that matches the given string. Yiisoft\Strings\CombinedRegexp
getMatchingPatternPosition() Returns position of the pattern that matches the given string. Yiisoft\Strings\CombinedRegexp
getPatterns() Yiisoft\Strings\CombinedRegexp
matches() Returns true whether the given string matches any of the patterns, false - otherwise. Yiisoft\Strings\CombinedRegexp

Constants

Hide inherited constants

Constant Value Description Defined By
QUOTE_REPLACER '\/' Yiisoft\Strings\AbstractCombinedRegexp
REGEXP_DELIMITER '/' Yiisoft\Strings\AbstractCombinedRegexp

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( string[] $patterns, string $flags '' )
$patterns string[]

Regular expressions to combine.

$flags string

Flags to apply to all regular expressions.

                public function __construct(
    array $patterns,
    private readonly string $flags = ''
) {
    if (empty($patterns)) {
        throw new InvalidArgumentException('At least one pattern should be specified.');
    }
    $this->patterns = array_values($patterns);
    $this->compiledPattern = $this->compilePatterns($this->patterns) . $this->flags;
}

            
getCompiledPattern() public method

public string getCompiledPattern ( )
return string

The compiled pattern.

                public function getCompiledPattern(): string
{
    return $this->compiledPattern;
}

            
getFlags() public method

public string getFlags ( )

                public function getFlags(): string
{
    return $this->flags;
}

            
getMatchingPattern() public method

Returns pattern that matches the given string.

public string getMatchingPattern ( string $string )
$string string
throws Exception

if the string does not match any of the patterns.

                public function getMatchingPattern(string $string): string
{
    return $this->patterns[$this->getMatchingPatternPosition($string)];
}

            
getMatchingPatternPosition() public method

Returns position of the pattern that matches the given string.

public integer getMatchingPatternPosition ( string $string )
$string string
throws Exception

if the string does not match any of the patterns.

                public function getMatchingPatternPosition(string $string): int
{
    $match = preg_match($this->compiledPattern, $string, $matches);
    if ($match !== 1) {
        $this->throwFailedMatchException($string);
    }
    return count($matches) - 1;
}

            
getPatterns() public method

public array getPatterns ( )

                public function getPatterns(): array
{
    return $this->patterns;
}

            
matches() public method

Returns true whether the given string matches any of the patterns, false - otherwise.

public boolean matches ( string $string )
$string string

                public function matches(string $string): bool
{
    return preg_match($this->compiledPattern, $string) === 1;
}

            
throwFailedMatchException() protected method
protected void throwFailedMatchException ( string $string )
$string string
throws Exception

                protected function throwFailedMatchException(string $string): void
{
    throw new Exception(
        sprintf(
            'Failed to match pattern "%s" with string "%s".',
            $this->getCompiledPattern(),
            $string,
        )
    );
}