| 1 | <?php |
||
| 9 | class Router |
||
| 10 | { |
||
| 11 | /** @var array<Routable> */ |
||
| 12 | private array $routes; |
||
|
|
|||
| 13 | |||
| 14 | public function __construct() |
||
| 15 | { |
||
| 16 | $this->routes = []; |
||
| 17 | } |
||
| 18 | |||
| 19 | public function addRoute(Routable $routable): void |
||
| 20 | { |
||
| 21 | $this->routes[] = $routable; |
||
| 22 | } |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @return array<Routable> |
||
| 26 | */ |
||
| 27 | public function getRoutes(): array |
||
| 28 | { |
||
| 29 | return $this->routes; |
||
| 30 | } |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @param ServerRequestInterface $serverRequest |
||
| 34 | * @return Routable |
||
| 35 | * @throws RouteNotFoundException |
||
| 36 | */ |
||
| 37 | public function resolve(ServerRequestInterface $serverRequest) |
||
| 38 | { |
||
| 39 | foreach ($this->routes as $route) { |
||
| 40 | if ($route->isHttpVerbAndPathAMatch($serverRequest->getMethod(), $serverRequest->getRequestTarget())) { |
||
| 41 | return $route; |
||
| 42 | } |
||
| 43 | } |
||
| 44 | |||
| 45 | throw new RouteNotFoundException('Route not found'); |
||
| 46 | } |
||
| 47 | } |
||
| 48 |