Completed
Pull Request — master (#3)
by
unknown
01:36
created

RouterContainer::getRouteByName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Piface\Router;
4
5
use Piface\Router\Exception\DuplicateRouteNameException;
6
use Piface\Router\Exception\DuplicateRouteUriException;
7
use Psr\Http\Message\ServerRequestInterface;
8
9
class RouterContainer
10
{
11
    /**
12
     * Array of route objects.
13
     *
14
     * @var Route[]
15
     */
16
    private $routes = [];
17
18
    /**
19
     * index all routes which have been registered to avoid duplication.
20
     *
21
     * @var array
22
     */
23
    private $path = [];
24
25
    public function addRoute(Route $route): Route
26
    {
27
        if (\in_array($route->getPath(), $this->path, true)) {
28
            throw new DuplicateRouteUriException($route->getPath());
29
        }
30
31
        if (array_key_exists($route->getName(), $this->path)) {
32
            throw new DuplicateRouteNameException($route->getName());
33
        }
34
35
        $this->path[$route->getName()] = $route->getPath();
36
        $this->routes[$route->getName()] = $route;
37
38
        return $route;
39
    }
40
41
    /**
42
     * Check if the current object route is matching the request route.
43
     */
44
    public function match(ServerRequestInterface $request, Route $route): bool
45
    {
46
        $requestedPath = $request->getUri()->getPath();
47
        $path = $this->argsResolver($route);
48
49
        if (!preg_match("#^$path$#i", $requestedPath, $matches)) {
50
            return false;
51
        }
52
53
        array_shift($matches);
54
        $route->setParameters($matches);
55
56
        return true;
57
    }
58
59
    /**
60
     * @return string|Exception
61
     */
62
    public function generatePath(string $name, $params = [])
63
    {
64
        return $this->getRouteByName($name)->generatePath($params);
65
    }
66
67
    /**
68
     * @return Route[]
69
     */
70
    public function getRoutes(): array
71
    {
72
        return $this->routes;
73
    }
74
75
    /**
76
     * @return Route|Exception
77
     */
78
    public function getRouteByName($name) {
79
        if (!array_key_exists($name, $this->path)) {
80
            throw new RouteNotFoundException($name);
0 ignored issues
show
Bug introduced by
The type Piface\Router\RouteNotFoundException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
81
        }
82
83
        return $this->routes[$name];
84
    }
85
86
    private function argsResolver(Route $route): string
87
    {
88
        $path = $route->getPath();
89
90
        if (!empty($route->getWhere())) {
91
            foreach ($route->getWhere() as $attribute => $where) {
92
                $path = preg_replace('#{('.$attribute.')}#', '('.$where.')', $path);
93
            }
94
        }
95
        $path = preg_replace("#{([\w]+)}#", '([^/]+)', $path);
96
97
        return $path;
98
    }
99
}
100