-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathAbstractCombinedRegexp.php
74 lines (61 loc) · 1.89 KB
/
AbstractCombinedRegexp.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
declare(strict_types=1);
namespace Yiisoft\Strings;
use Exception;
use function sprintf;
/**
* `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}.
*/
abstract class AbstractCombinedRegexp
{
/**
* @psalm-suppress MissingClassConstType
*/
public const REGEXP_DELIMITER = '/';
/**
* @psalm-suppress MissingClassConstType
*/
public const QUOTE_REPLACER = '\\/';
/**
* @return string[] Regular expressions to combine.
*/
abstract public function getPatterns(): array;
/**
* @return string Flags to apply to all regular expressions.
*/
abstract public function getFlags(): string;
/**
* @return string The compiled pattern.
*/
abstract public function getCompiledPattern(): string;
/**
* Returns `true` whether the given string matches any of the patterns, `false` - otherwise.
*/
abstract public function matches(string $string): bool;
/**
* Returns pattern that matches the given string.
* @throws Exception if the string does not match any of the patterns.
*/
abstract public function getMatchingPattern(string $string): string;
/**
* Returns position of the pattern that matches the given string.
* @throws Exception if the string does not match any of the patterns.
*/
abstract public function getMatchingPatternPosition(string $string): int;
/**
* @throws Exception
* @return never-return
*/
protected function throwFailedMatchException(string $string): void
{
throw new Exception(
sprintf(
'Failed to match pattern "%s" with string "%s".',
$this->getCompiledPattern(),
$string,
)
);
}
}