<?php
namespace App\Core;

class Route
{
    function __construct(
        public string   $method,
        public string   $pattern,
        public $handler,
        public $middlewares,
    ) {

    }
  
    public function match(string $method, string $uri): ?array
    {
        if ($method !== $this->method) {
            return null;
        }
        $regex = preg_replace(
            '#\{([a-zA-Z_][a-zA-Z0-9_-]*)\}#',
            '(?<$1>[^/]+)',
            $this->pattern
        );
        $regex = "#^{$regex}/?$#"; 
        
        if (preg_match($regex, $uri, $matches)) {
            return array_filter($matches, 'is_string', ARRAY_FILTER_USE_KEY);
        }
        return null;
    }
}
