2026-02-13 21:07:59 +00:00
|
|
|
<?php
|
|
|
|
|
|
2026-02-13 23:26:31 +00:00
|
|
|
namespace Atlas\Router;
|
2026-02-13 21:07:59 +00:00
|
|
|
|
2026-02-14 04:04:38 +00:00
|
|
|
/**
|
|
|
|
|
* Represents a complete route definition with matching patterns, handlers, and metadata.
|
|
|
|
|
*/
|
2026-02-14 23:27:29 +00:00
|
|
|
class RouteDefinition implements \JsonSerializable, \Serializable
|
2026-02-13 21:07:59 +00:00
|
|
|
{
|
2026-02-14 23:27:29 +00:00
|
|
|
public function serialize(): string
|
|
|
|
|
{
|
|
|
|
|
return serialize($this->__serialize());
|
|
|
|
|
}
|
2026-02-13 21:07:59 +00:00
|
|
|
|
2026-02-14 23:27:29 +00:00
|
|
|
public function unserialize(string $data): void
|
2026-02-13 21:07:59 +00:00
|
|
|
{
|
2026-02-14 23:27:29 +00:00
|
|
|
$this->__unserialize(unserialize($data));
|
2026-02-13 21:07:59 +00:00
|
|
|
}
|
|
|
|
|
|
2026-02-14 23:27:29 +00:00
|
|
|
public function __serialize(): array
|
2026-02-13 21:07:59 +00:00
|
|
|
{
|
2026-02-14 23:27:29 +00:00
|
|
|
return [
|
|
|
|
|
'method' => $this->method,
|
|
|
|
|
'pattern' => $this->pattern,
|
|
|
|
|
'path' => $this->path,
|
|
|
|
|
'handler' => $this->handler,
|
|
|
|
|
'name' => $this->name,
|
|
|
|
|
'middleware' => $this->middleware,
|
|
|
|
|
'validation' => $this->validation,
|
|
|
|
|
'defaults' => $this->defaults,
|
|
|
|
|
'module' => $this->module,
|
|
|
|
|
'attributes' => $this->attributes,
|
|
|
|
|
];
|
2026-02-13 21:07:59 +00:00
|
|
|
}
|
|
|
|
|
|
2026-02-14 23:27:29 +00:00
|
|
|
public function __unserialize(array $data): void
|
2026-02-13 21:07:59 +00:00
|
|
|
{
|
2026-02-14 23:27:29 +00:00
|
|
|
$this->method = $data['method'];
|
|
|
|
|
$this->pattern = $data['pattern'];
|
|
|
|
|
$this->path = $data['path'];
|
|
|
|
|
$this->handler = $data['handler'];
|
|
|
|
|
$this->name = $data['name'];
|
|
|
|
|
$this->middleware = $data['middleware'];
|
|
|
|
|
$this->validation = $data['validation'];
|
|
|
|
|
$this->defaults = $data['defaults'];
|
|
|
|
|
$this->module = $data['module'];
|
|
|
|
|
$this->attributes = $data['attributes'];
|
2026-02-13 21:07:59 +00:00
|
|
|
}
|
|
|
|
|
|
2026-02-14 04:04:38 +00:00
|
|
|
/**
|
2026-02-14 23:27:29 +00:00
|
|
|
* @internal
|
2026-02-14 04:04:38 +00:00
|
|
|
*/
|
2026-02-14 23:27:29 +00:00
|
|
|
public function setModule(?string $module): void
|
2026-02-13 21:07:59 +00:00
|
|
|
{
|
2026-02-14 23:27:29 +00:00
|
|
|
$this->module = $module;
|
2026-02-13 21:07:59 +00:00
|
|
|
}
|
2026-02-14 23:27:29 +00:00
|
|
|
public function __construct(
|
|
|
|
|
private string $method,
|
|
|
|
|
private string $pattern,
|
|
|
|
|
private string $path,
|
|
|
|
|
private mixed $handler,
|
|
|
|
|
private string|null $name = null,
|
|
|
|
|
private array $middleware = [],
|
|
|
|
|
private array $validation = [],
|
|
|
|
|
private array $defaults = [],
|
|
|
|
|
private string|null $module = null,
|
|
|
|
|
private array $attributes = []
|
|
|
|
|
) {}
|
2026-02-13 21:07:59 +00:00
|
|
|
|
2026-02-14 23:27:29 +00:00
|
|
|
public function getMethod(): string { return $this->method; }
|
|
|
|
|
public function getPattern(): string { return $this->pattern; }
|
|
|
|
|
public function getPath(): string { return $this->path; }
|
|
|
|
|
public function getHandler(): mixed { return $this->handler; }
|
|
|
|
|
public function getName(): ?string { return $this->name; }
|
|
|
|
|
|
|
|
|
|
public function name(string $name): self
|
2026-02-13 21:07:59 +00:00
|
|
|
{
|
2026-02-14 23:27:29 +00:00
|
|
|
$this->name = $name;
|
|
|
|
|
return $this;
|
2026-02-13 21:07:59 +00:00
|
|
|
}
|
|
|
|
|
|
2026-02-14 23:27:29 +00:00
|
|
|
public function getMiddleware(): array { return $this->middleware; }
|
|
|
|
|
|
|
|
|
|
public function middleware(string|array $middleware): self
|
2026-02-13 21:07:59 +00:00
|
|
|
{
|
2026-02-14 23:27:29 +00:00
|
|
|
if (is_string($middleware)) {
|
|
|
|
|
$this->middleware[] = $middleware;
|
|
|
|
|
} else {
|
|
|
|
|
$this->middleware = array_merge($this->middleware, $middleware);
|
|
|
|
|
}
|
|
|
|
|
return $this;
|
2026-02-13 21:07:59 +00:00
|
|
|
}
|
|
|
|
|
|
2026-02-14 23:27:29 +00:00
|
|
|
public function getValidation(): array { return $this->validation; }
|
|
|
|
|
|
|
|
|
|
public function valid(array|string $param, array|string $rules = []): self
|
2026-02-13 21:07:59 +00:00
|
|
|
{
|
2026-02-14 23:27:29 +00:00
|
|
|
if (is_array($param)) {
|
|
|
|
|
foreach ($param as $p => $r) {
|
|
|
|
|
$this->valid($p, $r);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$this->validation[$param] = is_string($rules) ? [$rules] : $rules;
|
|
|
|
|
}
|
|
|
|
|
return $this;
|
2026-02-13 21:07:59 +00:00
|
|
|
}
|
|
|
|
|
|
2026-02-14 23:27:29 +00:00
|
|
|
public function getDefaults(): array { return $this->defaults; }
|
|
|
|
|
|
|
|
|
|
public function default(string $param, mixed $value): self
|
2026-02-13 21:07:59 +00:00
|
|
|
{
|
2026-02-14 23:27:29 +00:00
|
|
|
$this->defaults[$param] = $value;
|
|
|
|
|
return $this;
|
2026-02-13 21:07:59 +00:00
|
|
|
}
|
|
|
|
|
|
2026-02-14 23:27:29 +00:00
|
|
|
public function getModule(): ?string { return $this->module; }
|
|
|
|
|
|
|
|
|
|
public function getAttributes(): array { return $this->attributes; }
|
|
|
|
|
|
|
|
|
|
public function attr(string $key, mixed $value): self
|
2026-02-13 21:07:59 +00:00
|
|
|
{
|
2026-02-14 23:27:29 +00:00
|
|
|
$this->attributes[$key] = $value;
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function meta(array $data): self
|
|
|
|
|
{
|
|
|
|
|
$this->attributes = array_merge($this->attributes, $data);
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function jsonSerialize(): mixed
|
|
|
|
|
{
|
|
|
|
|
return $this->toArray();
|
2026-02-13 21:07:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function toArray(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'method' => $this->method,
|
|
|
|
|
'pattern' => $this->pattern,
|
|
|
|
|
'path' => $this->path,
|
2026-02-14 23:27:29 +00:00
|
|
|
'handler' => is_callable($this->handler) ? 'Closure' : $this->handler,
|
2026-02-13 21:07:59 +00:00
|
|
|
'name' => $this->name,
|
|
|
|
|
'middleware' => $this->middleware,
|
|
|
|
|
'validation' => $this->validation,
|
|
|
|
|
'defaults' => $this->defaults,
|
|
|
|
|
'module' => $this->module,
|
|
|
|
|
'attributes' => $this->attributes
|
|
|
|
|
];
|
|
|
|
|
}
|
2026-02-14 23:27:29 +00:00
|
|
|
}
|