1 | <?php |
||
28 | final class Router |
||
29 | { |
||
30 | const HTML = 1; |
||
31 | const JSON = 2; |
||
32 | const TEXT = 3; |
||
33 | const XML = 4; |
||
34 | const REDIRECT = 5; |
||
35 | const DOWNLOAD = 6; |
||
36 | const CUSTOM = 7; |
||
37 | |||
38 | const OPTIONS = 'OPTIONS'; |
||
39 | const HEAD = 'HEAD'; |
||
40 | const GET = 'GET'; |
||
41 | const POST = 'POST'; |
||
42 | const PUT = 'PUT'; |
||
43 | const DELETE = 'DELETE'; |
||
44 | const PATCH = 'PATCH'; |
||
45 | |||
46 | /** |
||
47 | * Routes array to be registered. |
||
48 | * Some routes may have aliases to be used in templating system |
||
49 | * Route item can be defined using array key as an alias key. |
||
50 | * Each route item is an array has items respectively: Request Method, Request Uri, Controller/Action, Return Type. |
||
51 | * |
||
52 | * @var array |
||
53 | */ |
||
54 | private $routes = []; |
||
55 | |||
56 | /** |
||
57 | * Aliases array to be registered. |
||
58 | * |
||
59 | * @var array |
||
60 | */ |
||
61 | private $aliases = []; |
||
62 | |||
63 | /** |
||
64 | * HTTP request Method |
||
65 | * |
||
66 | * @var string |
||
67 | */ |
||
68 | private $method; |
||
69 | |||
70 | /** |
||
71 | * Request Uri |
||
72 | * |
||
73 | * @var string |
||
74 | */ |
||
75 | private $requestedPath; |
||
76 | |||
77 | /** |
||
78 | * Default return type if not noted in the $routes |
||
79 | * |
||
80 | * @var string |
||
81 | */ |
||
82 | private $defaultReturnType; |
||
83 | |||
84 | /** |
||
85 | * @var null|string |
||
86 | */ |
||
87 | private $cachedFile; |
||
88 | |||
89 | /** |
||
90 | * Valid Request Methods array. |
||
91 | * Make sures about requested methods. |
||
92 | * |
||
93 | * @var array |
||
94 | */ |
||
95 | private static $validRequestMethods = [ |
||
96 | 'GET', |
||
97 | 'OPTIONS', |
||
98 | 'HEAD', |
||
99 | 'POST', |
||
100 | 'PUT', |
||
101 | 'DELETE', |
||
102 | 'PATCH' |
||
103 | ]; |
||
104 | |||
105 | /** |
||
106 | * Router constructor. |
||
107 | * Create new router. |
||
108 | * |
||
109 | * @param int $defaultReturnType |
||
110 | * @param string $method |
||
111 | * @param string $requestedPath |
||
112 | * @param string $folder |
||
113 | * @param string $cachedFile |
||
114 | * @throws UnexpectedValueException |
||
115 | */ |
||
116 | public function __construct( |
||
132 | |||
133 | /** |
||
134 | * Remove sub folder from requestedPath if defined |
||
135 | * |
||
136 | * @param string $requestPath |
||
137 | * @param string $folder |
||
138 | * @return string |
||
139 | */ |
||
140 | private function extractFolder(string $requestPath, string $folder) : string |
||
150 | |||
151 | /** |
||
152 | * Add route to routes list |
||
153 | * |
||
154 | * @param string|array requestMethods |
||
155 | * @param string $route |
||
156 | * @param string $action |
||
157 | * @param int $returnType |
||
158 | * @param string $alias |
||
159 | * @throws InvalidArgumentException |
||
160 | * @throws UnexpectedValueException |
||
161 | */ |
||
162 | public function add( |
||
181 | |||
182 | /** |
||
183 | * @param string $method |
||
184 | * @param array $args |
||
185 | * @throws UnexpectedValueException |
||
186 | * @throws InvalidArgumentException |
||
187 | */ |
||
188 | public function __call(string $method, array $args) : void |
||
200 | |||
201 | /** |
||
202 | * @param int|null $returnType |
||
203 | * @return int |
||
204 | */ |
||
205 | private function determineReturnType(?int $returnType) : int |
||
212 | |||
213 | /** |
||
214 | * @param string $requestMethod |
||
215 | * Checks if request method is valid |
||
216 | * @throws UnexpectedValueException; |
||
217 | */ |
||
218 | private function checkRequestMethodIsValid(string $requestMethod) : void |
||
225 | |||
226 | /** |
||
227 | * @param $requestMethod |
||
228 | * @throws InvalidArgumentException |
||
229 | */ |
||
230 | private function checkRequestMethodParameterType($requestMethod) : void |
||
241 | |||
242 | |||
243 | /** |
||
244 | * Get router data that includes route info and aliases |
||
245 | * |
||
246 | * @return array |
||
247 | * @throws RuntimeException |
||
248 | */ |
||
249 | public function getRoute() : array |
||
261 | |||
262 | } |
||
263 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.