router
1<?php declare(strict_types=1);
2
3namespace Upside\Router;
4
5use Upside\Std\Option;
6use function Upside\Std\None;
7use function Upside\Std\Some;
8
9final class Router implements RouterInterface
10{
11private RouteCollector $route_collector;
12
13public function __construct(RouteCollector $route_collector)
14{
15$this->route_collector = $route_collector;
16}
17
18/**
19* @return Option<Matched>
20*/
21public function match(string $path): Option
22{
23foreach ($this->route_collector->routes() as $route) {
24$matches = [];
25if (\preg_match($route->pattern, $path, $matches)) {
26return Some(new Matched($route, \array_filter($matches, 'is_string', \ARRAY_FILTER_USE_KEY)));
27}
28}
29
30return None();
31}
32}
33