58 lines
1.5 KiB
PHP

<?php
namespace Notes\Route\Attribute\Method;
use Psr\Http\Message\ServerRequestInterface;
#[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_METHOD)]
class Route implements \Notes\Attribute {
public function __construct(
public string $route,
public string $name,
public string|array $method = [ "GET", "POST" ],
public ? string $base = "",
public ? string $class = null,
public ? string $classMethod = null,
public bool $currentRoute = false,
public string $description = "",
) {}
public function getRoute() : string
{
return rtrim("/" . trim(isset($this->base) ?
"/" . trim($this->base, "/") . "/" . ltrim($this->route, "/")
:
"/" . ltrim($this->route, "/"), "/"), '/');
}
public function getRegistrableRoute() : string
{
return preg_replace('/(\=.*)(?=\})/i', '', $this->getRoute());
}
public function matchRouteName(string $name) : bool
{
return strtolower($this->name) === strtolower($name);
}
public function match(mixed $name) : bool
{
foreach((array) $name as $item) {
if ( fnmatch($item, $this->name) ) {
return true;
}
}
return false;
}
public function isRoute(string $name) {
return $this->name === $name;
}
public function currentRoute(bool|null $value = null ) {
return is_null($value) ? $value : $this->currentRoute = $value;
}
}