index/src/Http/Routing/ResolvedRouteInfo.php

40 lines
1,001 B
PHP

<?php
// ResolvedRouteInfo.php
// Created: 2024-03-28
// Updated: 2024-03-28
namespace Index\Http\Routing;
class ResolvedRouteInfo {
public function __construct(
private array $middlewares,
private array $supportedMethods,
private ?callable $handler,
private array $args,
) {}
public function runMiddleware(array $args): mixed {
foreach($this->middlewares as $middleware) {
$result = $middleware[0](...array_merge($args, $middleware[1]));
if($result !== null)
return $result;
}
}
public function hasHandler(): bool {
return $this->handler !== null;
}
public function hasOtherMethods(): bool {
return !empty($this->supportedMethods);
}
public function getSupportedMethods(): array {
return $this->supportedMethods;
}
public function dispatch(array $args): mixed {
return $this->handler(...array_merge($args, $this->args));
}
}