1 | <?php |
||
17 | class Route |
||
18 | { |
||
19 | /** |
||
20 | * Router |
||
21 | * |
||
22 | * @var Router |
||
23 | */ |
||
24 | protected $router; |
||
25 | |||
26 | |||
27 | /** |
||
28 | * Continue propagation if match |
||
29 | * |
||
30 | * @var bool |
||
31 | */ |
||
32 | protected $continue_propagation = false; |
||
33 | |||
34 | |||
35 | /** |
||
36 | * Route path |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $path; |
||
41 | |||
42 | |||
43 | /** |
||
44 | * Class to be instanced if route match was found |
||
45 | * |
||
46 | * @var string|object |
||
47 | */ |
||
48 | protected $class; |
||
49 | |||
50 | |||
51 | /** |
||
52 | * Method to be executed if route match was found |
||
53 | * |
||
54 | * @var string |
||
55 | */ |
||
56 | protected $method; |
||
57 | |||
58 | |||
59 | /** |
||
60 | * Found request parameters |
||
61 | * |
||
62 | * @var array |
||
63 | */ |
||
64 | protected $params = []; |
||
65 | |||
66 | |||
67 | /** |
||
68 | * Instance route |
||
69 | * |
||
70 | * @param string $path |
||
71 | * @param string $class |
||
72 | * @param string $method |
||
73 | * @param array $params |
||
74 | * @return void |
||
|
|||
75 | */ |
||
76 | public function __construct(string $path, $class, ? string $method = null, array $params = []) |
||
77 | { |
||
78 | $this->setPath($path); |
||
79 | $this->setClass($class); |
||
80 | $this->setMethod($method); |
||
81 | $this->setParams($params); |
||
82 | } |
||
83 | |||
84 | |||
85 | /** |
||
86 | * Check if route does match the current http request |
||
87 | * |
||
88 | * @return bool |
||
89 | */ |
||
90 | public function match(): bool |
||
91 | { |
||
92 | $regex = preg_replace_callback('#({([A-Z0-9a-z]+)\:\#(.+?)\#})|\{(.+?)\}#', function($match) { |
||
93 | if (count($match) === 4) { |
||
94 | return '(?<'.$match[2].'>'.$match[3].'+)'; |
||
95 | } else { |
||
96 | return '(?<'.end($match).'>\w+)'; |
||
97 | } |
||
98 | }, $this->path); |
||
99 | |||
100 | if (preg_match('#^'.$regex.'#', $this->router->getPath(), $matches)) { |
||
101 | foreach ($matches as $key => $value) { |
||
102 | if (!is_int($key)) { |
||
103 | $this->params[$key] = $value; |
||
104 | } |
||
105 | } |
||
106 | |||
107 | return true; |
||
108 | } |
||
109 | |||
110 | return false; |
||
111 | } |
||
112 | |||
113 | |||
114 | /** |
||
115 | * Get callable |
||
116 | * |
||
117 | * @param array $constructor |
||
118 | * @return array |
||
119 | */ |
||
120 | public function getCallable($constructor = []): array |
||
134 | |||
135 | |||
136 | /** |
||
137 | * Build method name |
||
138 | * |
||
139 | * @param string $name |
||
140 | * @return string |
||
141 | */ |
||
142 | protected function buildMethodName(? string $name) : string |
||
143 | { |
||
144 | $result = $this->router->getVerb(); |
||
145 | |||
146 | if ($name !== null) { |
||
147 | $split = explode('-', $name); |
||
148 | foreach ($split as $part) { |
||
149 | $result .= ucfirst($part); |
||
150 | } |
||
151 | } |
||
152 | |||
153 | return $result; |
||
154 | } |
||
155 | |||
156 | |||
157 | /** |
||
158 | * Get http router |
||
159 | * |
||
160 | * @return Router |
||
161 | */ |
||
162 | public function getRouter(): Router |
||
166 | |||
167 | |||
168 | /** |
||
169 | * Set http router |
||
170 | * |
||
171 | * @param Router $router |
||
172 | * @return Route |
||
173 | */ |
||
174 | public function setRouter(Router $router): Route |
||
179 | |||
180 | |||
181 | /** |
||
182 | * Get path |
||
183 | * |
||
184 | * @return string |
||
185 | */ |
||
186 | public function getPath(): string |
||
190 | |||
191 | |||
192 | /** |
||
193 | * Set path |
||
194 | * |
||
195 | * @param string $path |
||
196 | * @return Route |
||
197 | */ |
||
198 | public function setPath(string $path): Route |
||
203 | |||
204 | |||
205 | /** |
||
206 | * Get class |
||
207 | * |
||
208 | * @return string |
||
209 | */ |
||
210 | public function getClass(): string |
||
218 | |||
219 | |||
220 | /** |
||
221 | * Set clas |
||
222 | * |
||
223 | * @param string|object $class |
||
224 | * @return Route |
||
225 | */ |
||
226 | public function setClass($class): Route |
||
231 | |||
232 | |||
233 | /** |
||
234 | * Convert camelCase to dashes |
||
235 | * |
||
236 | * @param string $value |
||
237 | * @return string |
||
238 | */ |
||
239 | protected function camelCase2Dashes(string $value): string |
||
243 | |||
244 | |||
245 | /** |
||
246 | * Get method |
||
247 | * |
||
248 | * @return string |
||
249 | */ |
||
250 | public function getMethod(): string |
||
271 | |||
272 | |||
273 | /** |
||
274 | * Set method |
||
275 | * |
||
276 | * @param string $method |
||
277 | * @return Route |
||
278 | */ |
||
279 | public function setMethod(? string $method) : Route |
||
284 | |||
285 | |||
286 | /** |
||
287 | * Set params |
||
288 | * |
||
289 | * @param array $params |
||
290 | * @return Route |
||
291 | */ |
||
292 | public function setParams(array $params): Route |
||
297 | |||
298 | |||
299 | /** |
||
300 | * Get translated regex variable values |
||
301 | * |
||
302 | * @return array |
||
303 | */ |
||
304 | public function getParams(): array |
||
308 | |||
309 | |||
310 | /** |
||
311 | * Continue propagation after match |
||
312 | * |
||
313 | * @return bool |
||
314 | */ |
||
315 | public function continueAfterMatch(): bool |
||
319 | |||
320 | |||
321 | /** |
||
322 | * Halt propagation after match |
||
323 | * |
||
324 | * @param bool $next |
||
325 | * @return Route |
||
326 | */ |
||
327 | public function continuePropagation($next = true): Route |
||
332 | } |
||
333 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.