|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Component\Http; |
|
4
|
|
|
|
|
5
|
|
|
use App\Component\Http\Interfaces\IRoutes; |
|
6
|
|
|
use App\Component\Http\Interfaces\IRoute; |
|
7
|
|
|
use App\Component\Http\Interfaces\IRequest; |
|
8
|
|
|
use App\Component\Http\Interfaces\IRouter; |
|
9
|
|
|
|
|
10
|
|
|
class Router implements IRouter |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* active route |
|
14
|
|
|
* |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
private $activeRoute; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* routes collection |
|
21
|
|
|
* |
|
22
|
|
|
* @var IRoutes |
|
23
|
|
|
*/ |
|
24
|
|
|
private $routes; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* request |
|
28
|
|
|
* |
|
29
|
|
|
* @var IRequest |
|
30
|
|
|
*/ |
|
31
|
|
|
private $request = null; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* route params |
|
35
|
|
|
* |
|
36
|
|
|
* @var array |
|
37
|
|
|
*/ |
|
38
|
|
|
private $params; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* route match expr |
|
42
|
|
|
* |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
private $matchingRoute; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* instanciate |
|
49
|
|
|
* |
|
50
|
|
|
* @param IRoutes $routes |
|
51
|
|
|
* @param IRequest $request |
|
52
|
|
|
*/ |
|
53
|
5 |
|
public function __construct(IRoutes $routes, IRequest $request) |
|
54
|
|
|
{ |
|
55
|
5 |
|
$this->routes = []; |
|
|
|
|
|
|
56
|
5 |
|
$this->request = $request; |
|
57
|
5 |
|
$this->activeRoute = ''; |
|
58
|
5 |
|
$this->params = []; |
|
59
|
5 |
|
$this->matchingRoute = ''; |
|
60
|
5 |
|
$this->setRoutes($routes); |
|
61
|
5 |
|
$this->activeRoute = substr($this->request->getUri(), 1); |
|
62
|
5 |
|
return $this; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* set routes |
|
67
|
|
|
* |
|
68
|
|
|
* @param IRoutes $routes |
|
69
|
|
|
* @return Router |
|
70
|
|
|
*/ |
|
71
|
5 |
|
public function setRoutes(IRoutes $routes): Router |
|
72
|
|
|
{ |
|
73
|
5 |
|
$this->routes = $routes->get(); |
|
|
|
|
|
|
74
|
5 |
|
return $this; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* compile |
|
79
|
|
|
* |
|
80
|
|
|
* @return array |
|
81
|
|
|
*/ |
|
82
|
1 |
|
public function compile(): array |
|
83
|
|
|
{ |
|
84
|
1 |
|
$routes = $this->routes; |
|
85
|
1 |
|
$routesLength = count($routes); |
|
|
|
|
|
|
86
|
1 |
|
for ($i = 0; $i < $routesLength; $i++) { |
|
87
|
1 |
|
$route = $routes[$i]; |
|
88
|
1 |
|
$matches = []; |
|
89
|
1 |
|
$pattern = $route->getExpr(); |
|
90
|
1 |
|
$match = preg_match($pattern, $this->activeRoute, $matches); |
|
91
|
1 |
|
if ($match) { |
|
92
|
1 |
|
$this->matchingRoute = $pattern; |
|
93
|
1 |
|
array_shift($matches); |
|
94
|
1 |
|
$this->setParams($route, $matches); |
|
95
|
1 |
|
return $matches; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
1 |
|
return []; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* return slugs params |
|
103
|
|
|
* |
|
104
|
|
|
* @return array |
|
105
|
|
|
*/ |
|
106
|
1 |
|
public function getParams(): array |
|
107
|
|
|
{ |
|
108
|
1 |
|
return $this->params; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* set params from slugs |
|
113
|
|
|
* |
|
114
|
|
|
* @return Router |
|
115
|
|
|
*/ |
|
116
|
2 |
|
public function setParams(IRoute $route, array $matches): Router |
|
117
|
|
|
{ |
|
118
|
2 |
|
$slugs = $route->getSlugs(); |
|
119
|
2 |
|
$slugCount = count($slugs); |
|
120
|
2 |
|
for ($c = 0; $c < $slugCount; $c++) { |
|
121
|
1 |
|
$slug = $slugs[$c]; |
|
122
|
1 |
|
if (false === empty($slug)) { |
|
123
|
1 |
|
$this->params[$slug] = $matches[$c]; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
2 |
|
return $this; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* return matching regexp pattern |
|
131
|
|
|
* |
|
132
|
|
|
* @return string |
|
133
|
|
|
*/ |
|
134
|
1 |
|
public function getMatchingRoute(): string |
|
135
|
|
|
{ |
|
136
|
1 |
|
return $this->matchingRoute; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..