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
|
3 |
|
public function __construct(IRoutes $routes, IRequest $request) |
54
|
|
|
{ |
55
|
3 |
|
$this->routes = []; |
|
|
|
|
56
|
3 |
|
$this->request = $request; |
57
|
3 |
|
$this->activeRoute = ''; |
58
|
3 |
|
$this->params = []; |
59
|
3 |
|
$this->matchingRoute = ''; |
60
|
3 |
|
$this->setRoutes($routes); |
61
|
3 |
|
$this->activeRoute = substr($this->request->getUri(), 1); |
62
|
3 |
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* set routes |
67
|
|
|
* |
68
|
|
|
* @param IRoutes $routes |
69
|
|
|
* @return Router |
70
|
|
|
*/ |
71
|
3 |
|
public function setRoutes(IRoutes $routes): Router |
72
|
|
|
{ |
73
|
3 |
|
$this->routes = $routes->get(); |
|
|
|
|
74
|
3 |
|
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
|
|
|
public function getParams(): array |
107
|
|
|
{ |
108
|
|
|
return $this->params; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* set params from slugs |
113
|
|
|
* |
114
|
|
|
* @return Router |
115
|
|
|
*/ |
116
|
1 |
|
public function setParams(IRoute $route, array $matches): Router |
117
|
|
|
{ |
118
|
1 |
|
$slugs = $route->getSlugs(); |
119
|
1 |
|
$slugCount = count($slugs); |
120
|
1 |
|
for ($c = 0; $c < $slugCount; $c++) { |
121
|
|
|
$slug = $slugs[$c]; |
122
|
|
|
if (false === empty($slug)) { |
123
|
|
|
$this->params[$slug] = $matches[$c]; |
124
|
|
|
} |
125
|
|
|
} |
126
|
1 |
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* return matching regexp pattern |
131
|
|
|
* |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
|
|
public function getMatchingRoute(): string |
135
|
|
|
{ |
136
|
|
|
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..