<?php

declare(strict_types=1);

namespace Fennec\Routing;

/**
 * Route "compilée" : association entre un chemin, des méthodes HTTP, et l'action de controller qui y répond.
 */
final class RouteDefinition
{
    /**
     * @param list<string> $methods
     * @param class-string $controllerClass
     */
    public function __construct(
        public readonly string $path,
        public readonly array $methods,
        public readonly string $controllerClass,
        public readonly string $controllerMethod,
    ) {
    }

    /**
     * @param array{path: string, methods: list<string>, controllerClass: string, controllerMethod: string} $data
     */
    public static function fromArray(array $data): self
    {
        return new self(
            $data['path'],
            $data['methods'],
            $data['controllerClass'],
            $data['controllerMethod'],
        );
    }

    /**
     * @return array{path: string, methods: list<string>, controllerClass: string, controllerMethod: string}
     */
    public function toArray(): array
    {
        return [
            'path' => $this->path,
            'methods' => $this->methods,
            'controllerClass' => $this->controllerClass,
            'controllerMethod' => $this->controllerMethod,
        ];
    }
}
