27 lines
746 B
PHP
27 lines
746 B
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace FDMTech;
|
|
|
|
final class Router
|
|
{
|
|
/** No decoding ambiguity: public paths are ASCII single slugs. */
|
|
public static function route(string $uri): array
|
|
{
|
|
$path = explode('?', $uri, 2)[0];
|
|
if ($path === '/') {
|
|
return ['slug' => 'home', 'path' => '/'];
|
|
}
|
|
if (!preg_match('~^/([a-z0-9]+(?:-[a-z0-9]+)*)(/?)$~D', $path, $match) || strlen($match[1]) > 160) {
|
|
return ['error' => 404];
|
|
}
|
|
if ($match[1] === 'home') {
|
|
return ['redirect' => '/'];
|
|
}
|
|
if (($match[2] ?? '') === '/') {
|
|
return ['redirect' => '/' . $match[1]];
|
|
}
|
|
return ['slug' => $match[1], 'path' => $path];
|
|
}
|
|
}
|