yiisoft /
strings
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Yiisoft\Strings; |
||
| 6 | |||
| 7 | use Exception; |
||
| 8 | |||
| 9 | use function sprintf; |
||
| 10 | |||
| 11 | /** |
||
| 12 | * `CombinedRegexp` optimizes matching of multiple regular expressions. |
||
| 13 | * Read more about the concept in |
||
| 14 | * {@see https://nikic.github.io/2014/02/18/Fast-request-routing-using-regular-expressions.html}. |
||
| 15 | */ |
||
| 16 | abstract class AbstractCombinedRegexp |
||
| 17 | { |
||
| 18 | public const REGEXP_DELIMITER = '/'; |
||
| 19 | public const QUOTE_REPLACER = '\\/'; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @return string[] Regular expressions to combine. |
||
| 23 | */ |
||
| 24 | abstract public function getPatterns(): array; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @return string Flags to apply to all regular expressions. |
||
| 28 | */ |
||
| 29 | abstract public function getFlags(): string; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @return string The compiled pattern. |
||
| 33 | */ |
||
| 34 | abstract public function getCompiledPattern(): string; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Returns `true` whether the given string matches any of the patterns, `false` - otherwise. |
||
| 38 | */ |
||
| 39 | abstract public function matches(string $string): bool; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Returns pattern that matches the given string. |
||
| 43 | * @throws Exception if the string does not match any of the patterns. |
||
| 44 | */ |
||
| 45 | abstract public function getMatchingPattern(string $string): string; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Returns position of the pattern that matches the given string. |
||
| 49 | * @throws Exception if the string does not match any of the patterns. |
||
| 50 | */ |
||
| 51 | abstract public function getMatchingPatternPosition(string $string): int; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @throws Exception |
||
| 55 | * @return never-return |
||
|
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
| 56 | */ |
||
| 57 | 3 | protected function throwFailedMatchException(string $string): void |
|
| 58 | { |
||
| 59 | 3 | throw new Exception( |
|
| 60 | 3 | sprintf( |
|
| 61 | 3 | 'Failed to match pattern "%s" with string "%s".', |
|
| 62 | 3 | $this->getCompiledPattern(), |
|
| 63 | 3 | $string, |
|
| 64 | 3 | ) |
|
| 65 | 3 | ); |
|
| 66 | } |
||
| 67 | } |
||
| 68 |