<?php

declare(strict_types=1);

namespace System\Routing;

final class Router
{
    /**
     * @var Route[]
     */
    private array $routes = [];

    public function get(string $uri, mixed $action): void
    {
        $this->routes[] = new Route('GET', $uri, $action);
    }

    public function post(string $uri, mixed $action): void
    {
        $this->routes[] = new Route('POST', $uri, $action);
    }

    /**
     * @return Route[]
     */
    public function routes(): array
    {
        return $this->routes;
    }

    public function match(string $method, string $uri): ?Route
    {
        foreach ($this->routes as $route) {

            if (
                $route->method === strtoupper($method)
                && $route->uri === $uri
            ) {
                return $route;
            }
        }

        return null;
    }
}