-
Notifications
You must be signed in to change notification settings - Fork 0
/
Router.php
77 lines (68 loc) · 2.08 KB
/
Router.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
declare(strict_types=1);
final class Router
{
private const NO_ROUTE = 404;
/**
* @var \ArrayIterator<Route>
*/
private $routes;
/**
* Router constructor.
* @param $routes array<Route>
*/
public function __construct(array $routes = [])
{
$this->routes = new \ArrayIterator();
foreach ($routes as $route) {
$this->add($route);
}
}
public function add(Route $route): self
{
$this->routes->offsetSet($route->getName(), $route);
return $this;
}
public function matchFromPath(string $path, string $method): Route
{
foreach ($this->routes as $route) {
if ($route->match($path, $method) === false) {
continue;
}
return $route;
}
throw new \Exception(
'No route found for ' . $method,
self::NO_ROUTE
);
}
public function generateUri(string $name, array $parameters = []): string
{
if ($this->routes->offsetExists($name) === false) {
throw new \InvalidArgumentException(
sprintf('Unknown %s name route', $name)
);
}
$route = $this->routes[$name];
if ($route->hasVars() && $parameters === []) {
throw new \InvalidArgumentException(
sprintf('%s route need parameters: %s', $name, implode(',', $route->getVarsNames()))
);
}
return self::resolveUri($route, $parameters);
}
private static function resolveUri(Route $route, array $parameters): string
{
$uri = $route->getPath();
foreach ($route->getVarsNames() as $variable) {
$varName = trim($variable, '{\}');
if (array_key_exists($varName, $parameters) === false) {
throw new \InvalidArgumentException(
sprintf('%s not found in parameters to generate url', $varName)
);
}
$uri = str_replace($variable, strval($parameters[$varName]), $uri);
}
return $uri;
}
}