Skip to content

Commit 648d88b

Browse files
author
Fady Michel
committedApr 12, 2021
add url generator class
1 parent 5ae2dad commit 648d88b

File tree

4 files changed

+72
-41
lines changed

4 files changed

+72
-41
lines changed
 

‎src/Helper.php

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace DevCoder;
4+
5+
class Helper
6+
{
7+
public static function trimPath(string $path): string
8+
{
9+
return '/' . rtrim(ltrim(trim($path), '/'), '/');
10+
}
11+
}

‎src/Route.php

+1-6
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function match(string $path, string $method): bool
6565
$regex = str_replace($variable, '(?P<' . $varName . '>[^/]++)', $regex);
6666
}
6767

68-
if (in_array($method, $this->getMethods()) && preg_match('#^' . $regex . '$#sD', self::trimPath($path), $matches)) {
68+
if (in_array($method, $this->getMethods()) && preg_match('#^' . $regex . '$#sD', Helper::trimPath($path), $matches)) {
6969
$values = array_filter($matches, static function ($key) {
7070
return is_string($key);
7171
}, ARRAY_FILTER_USE_KEY);
@@ -112,9 +112,4 @@ public function getVars(): array
112112
{
113113
return $this->vars;
114114
}
115-
116-
private static function trimPath(string $path): string
117-
{
118-
return '/' . rtrim(ltrim(trim($path), '/'), '/');
119-
}
120115
}

‎src/Router.php

+11-35
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,28 @@ final class Router implements RouterInterface
1212
private const NO_ROUTE = 404;
1313

1414
/**
15-
* @var array<Route>
15+
* @var \ArrayIterator<Route>
1616
*/
17-
private $routes = [];
17+
private $routes;
18+
19+
/**
20+
* @var UrlGenerator
21+
*/
22+
private $urlGenerator;
1823

1924
/**
2025
* Router constructor.
2126
* @param $routes array<Route>
2227
*/
2328
public function __construct(array $routes = [])
2429
{
25-
foreach ($routes as $route) {
26-
$this->add($route);
27-
}
30+
$this->routes = new \ArrayIterator(array_unique($routes));
31+
$this->urlGenerator = new UrlGenerator($this->routes);
2832
}
2933

3034
public function add(Route $route): self
3135
{
32-
if (in_array($route, $this->routes) === false) {
33-
$this->routes[$route->getName()] = $route;
34-
}
36+
$this->routes->offsetSet($route->getName(), $route);
3537
return $this;
3638
}
3739

@@ -57,32 +59,6 @@ public function matchFromPath(string $path, string $method): Route
5759

5860
public function generateUri(string $name, array $parameters = []): string
5961
{
60-
if (array_key_exists($name, $this->routes) === false) {
61-
throw new \InvalidArgumentException(
62-
sprintf('Unknown %s name route', $name)
63-
);
64-
}
65-
$route = $this->routes[$name];
66-
if ($route->hasVars() && $parameters === []) {
67-
throw new \InvalidArgumentException(
68-
sprintf('%s route need parameters: %s', $name, implode(',', $route->getVarsNames()))
69-
);
70-
}
71-
return self::resolveUri($route, $parameters);
72-
}
73-
74-
private static function resolveUri(Route $route, array $parameters): string
75-
{
76-
$uri = $route->getPath();
77-
foreach ($route->getVarsNames() as $variable) {
78-
$varName = trim($variable, '{\}');
79-
if (array_key_exists($varName, $parameters) === false) {
80-
throw new \InvalidArgumentException(
81-
sprintf('%s not found in parameters to generate url', $varName)
82-
);
83-
}
84-
$uri = str_replace($variable, $parameters[$varName], $uri);
85-
}
86-
return $uri;
62+
return $this->urlGenerator->generate($name, $parameters);
8763
}
8864
}

‎src/UrlGenerator.php

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DevCoder;
6+
7+
final class UrlGenerator
8+
{
9+
/**
10+
* @var \ArrayIterator<Route>
11+
*/
12+
private $routes;
13+
14+
public function __construct(\ArrayIterator $routes)
15+
{
16+
$this->routes = $routes;
17+
}
18+
19+
public function generate(string $name, array $parameters = []): string
20+
{
21+
if (array_key_exists($name, $this->routes) === false) {
22+
throw new \InvalidArgumentException(
23+
sprintf('Unknown %s name route', $name)
24+
);
25+
}
26+
$route = $this->routes[$name];
27+
if ($route->hasVars() && $parameters === []) {
28+
throw new \InvalidArgumentException(
29+
sprintf('%s route need parameters: %s', $name, implode(',', $route->getVarsNames()))
30+
);
31+
}
32+
return self::resolveUri($route, $parameters);
33+
}
34+
35+
private static function resolveUri(Route $route, array $parameters): string
36+
{
37+
$uri = $route->getPath();
38+
foreach ($route->getVarsNames() as $variable) {
39+
$varName = trim($variable, '{\}');
40+
if (array_key_exists($varName, $parameters) === false) {
41+
throw new \InvalidArgumentException(
42+
sprintf('%s not found in parameters to generate url', $varName)
43+
);
44+
}
45+
$uri = str_replace($variable, $parameters[$varName], $uri);
46+
}
47+
return $uri;
48+
}
49+
}

0 commit comments

Comments
 (0)
Please sign in to comment.