2023-09-08 20:40:48 +00:00
|
|
|
<?php
|
|
|
|
namespace Misuzu\URLs;
|
|
|
|
|
2023-09-10 00:04:53 +00:00
|
|
|
use Attribute;
|
|
|
|
use InvalidArgumentException;
|
|
|
|
use ReflectionObject;
|
|
|
|
|
|
|
|
#[Attribute(Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
|
2023-09-08 20:40:48 +00:00
|
|
|
class URLInfo {
|
|
|
|
public function __construct(
|
|
|
|
private string $name,
|
|
|
|
private string $path,
|
2023-09-10 00:04:53 +00:00
|
|
|
private array $query = [],
|
|
|
|
private string $fragment = ''
|
|
|
|
) {
|
|
|
|
if($path === '')
|
|
|
|
throw new InvalidArgumentException('$path may not be empty.');
|
|
|
|
if($path[0] !== '/')
|
|
|
|
throw new InvalidArgumentException('$path must begin with /.');
|
|
|
|
}
|
2023-09-08 20:40:48 +00:00
|
|
|
|
|
|
|
public function getName(): string {
|
|
|
|
return $this->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getPath(): string {
|
|
|
|
return $this->path;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function hasQuery(): bool {
|
|
|
|
return !empty($this->query);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getQuery(): array {
|
|
|
|
return $this->query;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function hasFragment(): bool {
|
|
|
|
return $this->fragment !== '';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFragment(): string {
|
|
|
|
return $this->fragment;
|
|
|
|
}
|
2023-09-10 00:04:53 +00:00
|
|
|
|
|
|
|
public static function handleAttributes(URLRegistry $urls, object $source): void {
|
|
|
|
$objectInfo = new ReflectionObject($source);
|
|
|
|
$methodInfos = $objectInfo->getMethods();
|
|
|
|
|
|
|
|
foreach($methodInfos as $methodInfo) {
|
|
|
|
$attrInfos = $methodInfo->getAttributes(URLInfo::class);
|
|
|
|
foreach($attrInfos as $attrInfo)
|
|
|
|
$urls->registerInfo($attrInfo->newInstance());
|
|
|
|
}
|
|
|
|
}
|
2023-09-08 20:40:48 +00:00
|
|
|
}
|