Passed
Push — master ( b0ffba...d5ae6f )
by Maxime
38s queued 10s
created

RouterContainer::addRoute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 14
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
namespace Piface\Router;
4
5
use Piface\Router\Exception\DuplicateRouteNameException;
6
use Piface\Router\Exception\DuplicateRoutePathException;
7
use Piface\Router\Exception\RouteNotFoundException;
8
use Psr\Http\Message\ServerRequestInterface;
9
10
class RouterContainer
11
{
12
    /**
13
     * Array of route objects.
14
     *
15
     * @var Route[]
16
     */
17
    private $routes = [];
18
19
    /**
20
     * index all routes which have been registered to avoid duplication.
21
     * The array is built like this : ['route_name' => 'route_path'].
22
     *
23
     * @var array
24
     */
25
    private $paths = [];
26
27
    public function addRoute(Route $route): Route
28
    {
29
        if (\in_array($route->getPath(), $this->paths, true)) {
30
            throw new DuplicateRoutePathException($route->getPath());
31
        }
32
33
        if (array_key_exists($route->getName(), $this->paths)) {
34
            throw new DuplicateRouteNameException($route->getName());
35
        }
36
37
        $this->paths[$route->getName()] = $route->getPath();
38
        $this->routes[$route->getName()] = $route;
39
40
        return $route;
41
    }
42
43
    /**
44
     * Check if the current object route is matching the request route.
45
     */
46
    public function match(ServerRequestInterface $request, Route $route): bool
47
    {
48
        $requestedPath = $request->getUri()->getPath();
49
        $path = $this->argsResolver($route);
50
51
        if (!preg_match("#^$path$#i", $requestedPath, $matches)) {
52
            return false;
53
        }
54
55
        array_shift($matches);
56
        $route->setParameters($matches);
57
58
        return true;
59
    }
60
61
    /**
62
     * @return string|Exception
63
     */
64
    public function generatePath(string $name, $params = [])
65
    {
66
        return $this->getRouteByName($name)->generatePath($params);
67
    }
68
69
    /**
70
     * @return Route[]
71
     */
72
    public function getRoutes(): array
73
    {
74
        return $this->routes;
75
    }
76
77
    /**
78
     * @return Route|Exception
79
     */
80
    public function getRouteByName(string $name)
81
    {
82
        if (!array_key_exists($name, $this->paths)) {
83
            throw new RouteNotFoundException($name);
84
        }
85
86
        return $this->routes[$name];
87
    }
88
89
    private function argsResolver(Route $route): string
90
    {
91
        $path = $route->getPath();
92
93
        if (!empty($route->getWhere())) {
94
            foreach ($route->getWhere() as $attribute => $where) {
95
                $path = preg_replace('#{('.$attribute.')}#', '('.$where.')', $path);
96
            }
97
        }
98
        $path = preg_replace("#{([\w]+)}#", '([^/]+)', $path);
99
100
        return $path;
101
    }
102
}
103