Final Class Yiisoft\Strings\CombinedRegexp
| Inheritance | Yiisoft\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
| 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 |
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| throwFailedMatchException() | Yiisoft\Strings\AbstractCombinedRegexp |
Constants
| Constant | Value | Description | Defined By |
|---|---|---|---|
| QUOTE_REPLACER | '\/' | Yiisoft\Strings\AbstractCombinedRegexp | |
| REGEXP_DELIMITER | '/' | Yiisoft\Strings\AbstractCombinedRegexp |
Method Details
| 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;
}
| public string getCompiledPattern ( ) | ||
| return | string |
The compiled pattern. |
|---|---|---|
public function getCompiledPattern(): string
{
return $this->compiledPattern;
}
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)];
}
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;
}
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;
}
| 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,
)
);
}
Signup or Login in order to comment.