Total Complexity | 6 |
Total Lines | 42 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
7 | trait HandlesRegularExpressions |
||
8 | { |
||
9 | protected function matchesRegularExpression(string $subject) |
||
10 | { |
||
11 | return (bool) preg_match($this->getRegularExpression(), $subject, $this->matches); |
||
12 | } |
||
13 | |||
14 | /** |
||
15 | * We do not want to create the regular expression on our own, |
||
16 | * so we just use Symfonys Route for this. |
||
17 | * |
||
18 | * @return string |
||
19 | */ |
||
20 | protected function getRegularExpression(): string |
||
21 | { |
||
22 | $route = new Route($this->pattern); |
||
23 | $route->setRequirements($this->wheres); |
||
24 | |||
25 | $regex = $route->compile()->getRegex(); |
||
26 | |||
27 | $regex = preg_replace('/^(#|{)\^\/(.*)/', '$1^$2', $regex); |
||
28 | |||
29 | $regex = str_replace('>[^/]+)', '>.+)', $regex); |
||
30 | |||
31 | $regex = str_replace('$#sD', '$#sDi', $regex); |
||
32 | $regex = str_replace('$}sD', '$}sDi', $regex); |
||
33 | |||
34 | return $regex; |
||
35 | } |
||
36 | |||
37 | public function where($name, $expression = null) |
||
38 | { |
||
39 | foreach ($this->parseWhere($name, $expression) as $name => $expression) { |
||
|
|||
40 | $this->wheres[$name] = $expression; |
||
41 | } |
||
42 | |||
43 | return $this; |
||
44 | } |
||
45 | |||
46 | protected function parseWhere($name, $expression) |
||
49 | } |
||
50 | } |
||
51 |