Final Class Yiisoft\Strings\CombinedRegexp
| Inheritance | Yiisoft\ |
|---|
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\ |
|
| getCompiledPattern() | Yiisoft\ |
|
| getFlags() | Yiisoft\ |
|
| getMatchingPattern() | Returns pattern that matches the given string. | Yiisoft\ |
| getMatchingPatternPosition() | Returns position of the pattern that matches the given string. | Yiisoft\ |
| getPatterns() | Yiisoft\ |
|
| matches() | Returns true whether the given string matches any of the patterns, false - otherwise. |
Yiisoft\ |
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| throwFailedMatchException() | Yiisoft\ |
Constants
| Constant | Value | Description | Defined By |
|---|---|---|---|
| QUOTE_REPLACER | '\/' | Yiisoft\ |
|
| REGEXP_DELIMITER | '/' | Yiisoft\ |
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,
),
);
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.