|
12 | 12 | class RouteCollection extends Collection |
13 | 13 | { |
14 | 14 | /** |
15 | | - * Return collection of routes that matched pattern |
| 15 | + * Include routes that match patterns. |
16 | 16 | * |
17 | | - * @param array $matches |
| 17 | + * @param array <array|string> $patterns |
18 | 18 | * |
19 | 19 | * @return static |
20 | 20 | */ |
21 | | - public function filterMatch(array $matches = []) |
| 21 | + public function filterMatch(array $patterns = []) |
22 | 22 | { |
23 | | - if (!empty($matches)) { |
24 | | - return $this->filter(function ($route, $key) use ($matches) { |
25 | | - $isMatched = true; |
26 | | - |
27 | | - foreach ($matches as $match) { |
28 | | - |
29 | | - foreach ($route as $index => $value) { |
30 | | - if (!array_key_exists($index, $match)) { |
31 | | - continue; |
32 | | - } |
| 23 | + // String pattern is assumed to be path. |
| 24 | + foreach ($patterns as $key => $pattern) { |
| 25 | + if (is_string($pattern)) { |
| 26 | + $patterns[$key] = ['path' => $pattern]; |
| 27 | + } |
| 28 | + } |
33 | 29 |
|
34 | | - if (!preg_match($match[$index], $value)) { |
35 | | - $isMatched = false; |
36 | | - } |
37 | | - } |
| 30 | + return $this->filter(function ($route) use ($patterns) { |
| 31 | + // If any of patterns matches the route passes. |
| 32 | + foreach ($patterns as $pattern) { |
| 33 | + if ($this->isRouteMatchesPattern($route, $pattern)) { |
| 34 | + return true; |
38 | 35 | } |
| 36 | + } |
39 | 37 |
|
40 | | - return $isMatched; |
41 | | - }); |
42 | | - } |
43 | | - |
44 | | - return $this; |
| 38 | + // If all patterns don't match - route is filtered out. |
| 39 | + return false; |
| 40 | + }); |
45 | 41 | } |
46 | 42 |
|
| 43 | + |
47 | 44 | /** |
48 | | - * Return collection of routes except matched pattern |
| 45 | + * Exclude routes that match patterns. |
49 | 46 | * |
50 | | - * @param array $exceptions |
| 47 | + * @param array <array|string> $patterns |
51 | 48 | * |
52 | 49 | * @return static |
53 | 50 | */ |
54 | | - public function filterExcept(array $exceptions = []) |
| 51 | + public function filterExcept(array $patterns = []) |
| 52 | + { |
| 53 | + if (empty($patterns)) { |
| 54 | + return $this; |
| 55 | + } |
| 56 | + |
| 57 | + $toExclude = $this->filterMatch($patterns)->keys()->toArray(); |
| 58 | + |
| 59 | + return $this->except($toExclude); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @param array $route |
| 64 | + * @param array $pattern |
| 65 | + * @return bool |
| 66 | + */ |
| 67 | + private function isRouteMatchesPattern(array $route, array $pattern) |
55 | 68 | { |
56 | | - if (!empty($exceptions)) { |
57 | | - return $this->except($this->filterMatch($exceptions)->keys()->toArray()); |
| 69 | + foreach ($route as $key => $value) { |
| 70 | + if (! array_key_exists($key, $pattern)) { |
| 71 | + continue; |
| 72 | + } |
| 73 | + |
| 74 | + $regex = '~'.$pattern[$key].'~'; |
| 75 | + |
| 76 | + return ! ! preg_match($regex, $value); |
58 | 77 | } |
59 | 78 |
|
60 | | - return $this; |
| 79 | + return true; |
61 | 80 | } |
62 | 81 | } |
0 commit comments