| Total Complexity | 7 |
| Total Lines | 52 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 0 | ||
| 1 | <?php |
||
| 5 | class UrlMatcher |
||
| 6 | { |
||
| 7 | /** |
||
| 8 | * @var RouteCollection |
||
| 9 | */ |
||
| 10 | private $collection; |
||
| 11 | |||
| 12 | 4 | public function __construct(RouteCollection $collection) |
|
| 15 | 4 | } |
|
| 16 | |||
| 17 | 2 | public function match($path) |
|
| 18 | { |
||
| 19 | 2 | if ($match = $this->matchCollection($path)) { |
|
| 20 | 1 | return $match; |
|
| 21 | } |
||
| 22 | 1 | return ['controller' => 'Default', 'action' => 'notFound', 'params' => []]; |
|
| 23 | } |
||
| 24 | |||
| 25 | 4 | public function matchCollection($path) |
|
| 26 | { |
||
| 27 | 4 | foreach ($this->collection->all() as $route) { |
|
| 28 | 4 | $regex = $this->createRegex($route); |
|
| 29 | |||
| 30 | 4 | if (preg_match('#^'.$regex.'$#', $path, $matches)) { |
|
| 31 | $params = array_filter($matches, function ($key) { |
||
| 32 | 2 | return !is_int($key); |
|
| 33 | 2 | }, ARRAY_FILTER_USE_KEY); |
|
| 34 | return [ |
||
| 35 | 2 | 'controller' => $route->getDefault('controller'), |
|
| 36 | 2 | 'action' => $route->getDefault('action'), |
|
| 37 | 4 | 'params' => $params |
|
| 38 | ]; |
||
| 39 | } |
||
| 40 | } |
||
| 41 | 2 | } |
|
| 42 | |||
| 43 | 4 | private function createRegex(RouteDefinition $route): string |
|
| 57 | } |
||
| 58 | } |
||
| 59 |