1 | <?php |
||
48 | class Route implements RouteInterface |
||
49 | { |
||
50 | /** |
||
51 | * GET request |
||
52 | * |
||
53 | * @var string |
||
54 | */ |
||
55 | const GET = 'GET'; |
||
56 | /** |
||
57 | * POST request |
||
58 | * |
||
59 | * @var string |
||
60 | */ |
||
61 | const POST = 'POST'; |
||
62 | /** |
||
63 | * PATCH request |
||
64 | * |
||
65 | * @var string |
||
66 | */ |
||
67 | const PATCH = 'PATCH'; |
||
68 | /** |
||
69 | * DELETE request |
||
70 | * |
||
71 | * @var string |
||
72 | */ |
||
73 | const DELETE = 'DELETE'; |
||
74 | /** |
||
75 | * OPTIONS request |
||
76 | * |
||
77 | * @var string |
||
78 | */ |
||
79 | const OPTIONS = 'OPTIONS'; |
||
80 | /** |
||
81 | * HEAD request |
||
82 | * |
||
83 | * @var string |
||
84 | */ |
||
85 | const HEAD = 'HEAD'; |
||
86 | |||
87 | /** |
||
88 | * Allowed HTTP verbs |
||
89 | * |
||
90 | * @var array |
||
91 | */ |
||
92 | protected static $validVerbs = [ |
||
93 | self::GET => true, |
||
94 | self::POST => true, |
||
95 | self::PATCH => true, |
||
96 | self::DELETE => true, |
||
97 | self::OPTIONS => true, |
||
98 | self::HEAD => true |
||
99 | ]; |
||
100 | /** |
||
101 | * Route name |
||
102 | * |
||
103 | * @var string |
||
104 | */ |
||
105 | protected $name; |
||
106 | /** |
||
107 | * Route path |
||
108 | * |
||
109 | * @var string |
||
110 | */ |
||
111 | protected $path; |
||
112 | /** |
||
113 | * Route action |
||
114 | * |
||
115 | * @var string|callable |
||
116 | */ |
||
117 | protected $action; |
||
118 | /** |
||
119 | * Route tokens |
||
120 | * |
||
121 | * @var array |
||
122 | */ |
||
123 | protected $tokens = []; |
||
124 | /** |
||
125 | * Route default values |
||
126 | * |
||
127 | * @var array |
||
128 | */ |
||
129 | protected $defaults = []; |
||
130 | /** |
||
131 | * Route wildcard name |
||
132 | * |
||
133 | * @var string|null |
||
134 | */ |
||
135 | protected $wildcard = null; |
||
136 | /** |
||
137 | * Route host |
||
138 | * |
||
139 | * @var string|null |
||
140 | */ |
||
141 | protected $host = null; |
||
142 | /** |
||
143 | * Allowed accept headers |
||
144 | * |
||
145 | * @var array |
||
146 | */ |
||
147 | protected $accepts = []; |
||
148 | /** |
||
149 | * Allowed HTTP verbs |
||
150 | * |
||
151 | * @var array |
||
152 | */ |
||
153 | protected $verbs = []; |
||
154 | /** |
||
155 | * Secure protocol behaviour |
||
156 | * |
||
157 | * @var boolean|null |
||
158 | */ |
||
159 | protected $secure = false; |
||
160 | /** |
||
161 | * Authentication parameters |
||
162 | * |
||
163 | * @var mixed |
||
164 | */ |
||
165 | protected $auth = null; |
||
166 | /** |
||
167 | * Custom extra parameters |
||
168 | * |
||
169 | * @var array |
||
170 | */ |
||
171 | protected $extras = []; |
||
172 | /** |
||
173 | * Object route |
||
174 | * |
||
175 | * @var bool |
||
176 | */ |
||
177 | protected $object = false; |
||
178 | |||
179 | /** |
||
180 | * Route constructor |
||
181 | * |
||
182 | * @param string|array $verbs Allowed HTTP verbs |
||
183 | * @param string $name Route name |
||
184 | * @param string $path Route path |
||
185 | * @param string|\Callable|array $action Route action |
||
186 | */ |
||
187 | 4 | public function __construct($verbs, $name, $path, $action) |
|
204 | |||
205 | /** |
||
206 | * Validate the allowed HTTP verbs |
||
207 | * |
||
208 | * @param string|array $verbs Allowed HTTP verbs |
||
209 | * @throws InvalidArgumentException If the HTTP verb list is empty |
||
210 | * @throws InvalidArgumentException If the HTTP verb is invalid |
||
211 | */ |
||
212 | 4 | protected function setAndValidateVerbs($verbs) |
|
235 | |||
236 | /** |
||
237 | * Set and validate the route name |
||
238 | * |
||
239 | * @param string $name Route name |
||
240 | * @throws InvalidArgumentException If the route name is empty |
||
241 | */ |
||
242 | 4 | protected function setAndValidateName($name) |
|
253 | |||
254 | /** |
||
255 | * Set and validate the route path |
||
256 | * |
||
257 | * @param string $path Route path |
||
258 | * @throws InvalidArgumentException If the route path is empty |
||
259 | */ |
||
260 | 4 | protected function setAndValidatePath($path) |
|
271 | |||
272 | /** |
||
273 | * Set and validate the route action |
||
274 | * |
||
275 | * @param string|\Callable|array $actions Route actions |
||
276 | */ |
||
277 | 4 | protected function setAndValidateActionList($actions) |
|
282 | |||
283 | /** |
||
284 | * Get the route name |
||
285 | * |
||
286 | * @return string Route name |
||
287 | */ |
||
288 | 4 | public function getName() |
|
292 | |||
293 | /** |
||
294 | * Get the route path |
||
295 | * |
||
296 | * @return string Route path |
||
297 | */ |
||
298 | 4 | public function getPath() |
|
302 | |||
303 | /** |
||
304 | * Get the route action |
||
305 | * |
||
306 | * @return callable|string Route action |
||
307 | */ |
||
308 | 4 | public function getAction() |
|
312 | |||
313 | /** |
||
314 | * Get the route tokens |
||
315 | * |
||
316 | * @return array Route tokens |
||
317 | */ |
||
318 | 4 | public function getTokens() |
|
322 | |||
323 | /** |
||
324 | * Set the route tokens |
||
325 | * |
||
326 | * @param array $tokens Route tokens |
||
327 | * @return Route Self reference |
||
328 | */ |
||
329 | 2 | public function setTokens(array $tokens) |
|
334 | |||
335 | /** |
||
336 | * Get the route defaults |
||
337 | * |
||
338 | * @return array Route defaults |
||
339 | */ |
||
340 | 4 | public function getDefaults() |
|
344 | |||
345 | /** |
||
346 | * Set the route defaults |
||
347 | * |
||
348 | * @param array $defaults Route defaults |
||
349 | * @return Route Self reference |
||
350 | */ |
||
351 | public function setDefaults(array $defaults) |
||
356 | |||
357 | /** |
||
358 | * Set the route wildcard name |
||
359 | * |
||
360 | * @return null|string |
||
361 | */ |
||
362 | 4 | public function getWildcard() |
|
366 | |||
367 | /** |
||
368 | * Get the route wildcard name |
||
369 | * |
||
370 | * @param null|string $wildcard Wildcard name |
||
371 | * @return Route Self reference |
||
372 | */ |
||
373 | public function setWildcard($wildcard) |
||
378 | |||
379 | /** |
||
380 | * Get the route host |
||
381 | * |
||
382 | * @return null|string Route host |
||
383 | */ |
||
384 | 4 | public function getHost() |
|
388 | |||
389 | /** |
||
390 | * Set the route host |
||
391 | * |
||
392 | * @param null|string $host Route host |
||
393 | * @return Route Self reference |
||
394 | */ |
||
395 | public function setHost($host) |
||
400 | |||
401 | /** |
||
402 | * Get the allowed Accept headers |
||
403 | * |
||
404 | * @return array Allowed Accept headers |
||
405 | */ |
||
406 | 4 | public function getAccepts() |
|
410 | |||
411 | /** |
||
412 | * Set the allowed Accept headers |
||
413 | * |
||
414 | * @param array $accepts Allowed Accept headers |
||
415 | * @return Route Self reference |
||
416 | */ |
||
417 | public function setAccepts(array $accepts) |
||
422 | |||
423 | /** |
||
424 | * Get the allowed HTTP verbs |
||
425 | * |
||
426 | * @return array Allowed HTTP verbs |
||
427 | */ |
||
428 | 4 | public function getVerbs() |
|
432 | |||
433 | /** |
||
434 | * Set the allowed HTTP verbs |
||
435 | * |
||
436 | * @param array $verbs Allowed HTTP verbs |
||
437 | * @return Route Self reference |
||
438 | */ |
||
439 | public function setVerbs(array $verbs) |
||
444 | |||
445 | /** |
||
446 | * Get the secure protocol mode |
||
447 | * |
||
448 | * @return boolean|null Secure protocol mode |
||
449 | */ |
||
450 | 4 | public function getSecure() |
|
454 | |||
455 | /** |
||
456 | * Set the secure protocol mode |
||
457 | * |
||
458 | * @param boolean|null $secure Secure protocol mode |
||
459 | * @return Route Self reference |
||
460 | */ |
||
461 | public function setSecure($secure) |
||
466 | |||
467 | /** |
||
468 | * Get the authentication information |
||
469 | * |
||
470 | * @return mixed Authentication information |
||
471 | */ |
||
472 | 4 | public function getAuth() |
|
476 | |||
477 | /** |
||
478 | * Set the authentication information |
||
479 | * |
||
480 | * @param mixed $auth Authentication information |
||
481 | * @return Route Self reference |
||
482 | */ |
||
483 | public function setAuth($auth) |
||
488 | |||
489 | /** |
||
490 | * Get the custom extra information |
||
491 | * |
||
492 | * @return array Custom extra information |
||
493 | */ |
||
494 | 4 | public function getExtras() |
|
498 | |||
499 | /** |
||
500 | * Set the custom extra information |
||
501 | * |
||
502 | * @param array $extras Custom extra information |
||
503 | * @return Route Self reference |
||
504 | */ |
||
505 | public function setExtras(array $extras) |
||
510 | |||
511 | /** |
||
512 | * Return whether this is an object route |
||
513 | * |
||
514 | * @return boolean Object route |
||
515 | */ |
||
516 | 4 | public function isObject() |
|
520 | |||
521 | /** |
||
522 | * Set and validate the route action |
||
523 | * |
||
524 | * @param string $action Route action |
||
525 | * @throws InvalidArgumentException If the route action is empty |
||
526 | * @throws InvalidArgumentException If the route action is not a class name |
||
527 | * @throws InvalidArgumentException If the route action is neither a callable nor an ActionInterface |
||
528 | */ |
||
529 | 4 | protected function setAndValidateAction($action) |
|
574 | } |
||
575 |