1 | <?php |
||
23 | class Matcher |
||
24 | { |
||
25 | |||
26 | /** |
||
27 | * @var Collector |
||
28 | */ |
||
29 | |||
30 | protected $collector; |
||
31 | |||
32 | /** |
||
33 | * Define a basepath to all routes. |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | |||
38 | protected $basepath = ""; |
||
39 | |||
40 | /** |
||
41 | * Construct the route dispatcher. |
||
42 | * |
||
43 | * @param Collector $collector |
||
44 | * @param string $basepath Define a Path prefix that must be excluded on matches. |
||
45 | */ |
||
46 | |||
47 | public function __construct(Collector $collector, $basepath = "") |
||
52 | |||
53 | /** |
||
54 | * Find a route that matches the given arguments. |
||
55 | * |
||
56 | * @param string $httpMethod |
||
57 | * @param string $path |
||
58 | * |
||
59 | * @throws NotFoundException |
||
60 | * @throws MethodNotAllowedException |
||
61 | * |
||
62 | * @return Route |
||
63 | */ |
||
64 | |||
65 | public function match($httpMethod, $path) |
||
79 | |||
80 | /** |
||
81 | * Find and return the request dynamic route based on the compiled data and Path. |
||
82 | * |
||
83 | * @param string $httpMethod |
||
84 | * @param string $path |
||
85 | * |
||
86 | * @return Route|false If the request match an array with the action and parameters will |
||
87 | * be returned otherwise a false will. |
||
88 | */ |
||
89 | |||
90 | protected function matchDynamicRoute($httpMethod, $path) |
||
122 | |||
123 | /** |
||
124 | * Parse the dynamic segments of the pattern and replace then for |
||
125 | * corresponding regex. |
||
126 | * |
||
127 | * @param Route $route |
||
128 | * @return Route |
||
129 | */ |
||
130 | |||
131 | protected function buildRoute(Route $route) |
||
140 | |||
141 | /** |
||
142 | * Group several dynamic routes patterns into one big regex and maps |
||
143 | * the routes to the pattern positions in the big regex. |
||
144 | * |
||
145 | * @param array $routes |
||
146 | * @return array |
||
147 | */ |
||
148 | |||
149 | protected function buildGroup(array $routes) |
||
164 | |||
165 | /** |
||
166 | * Parse an route pattern seeking for parameters and build the route regex. |
||
167 | * |
||
168 | * @param string $pattern |
||
169 | * @return array 0 => new route regex, 1 => map of parameter names |
||
170 | */ |
||
171 | |||
172 | protected function parsePlaceholders($pattern) |
||
184 | |||
185 | /** |
||
186 | * Get only the path of a given url. |
||
187 | * |
||
188 | * @param string $path The given URL |
||
189 | * |
||
190 | * @throws Exception |
||
191 | * @return string |
||
192 | */ |
||
193 | |||
194 | protected function parsePath($path) |
||
204 | |||
205 | /** |
||
206 | * Generate an HTTP error request with method not allowed or not found. |
||
207 | * |
||
208 | * @param string $httpMethod |
||
209 | * @param string $path |
||
210 | * |
||
211 | * @throws NotFoundException |
||
212 | * @throws MethodNotAllowedException |
||
213 | */ |
||
214 | |||
215 | protected function matchSimilarRoute($httpMethod, $path) |
||
226 | |||
227 | /** |
||
228 | * Verify if a static route match in another method than the requested. |
||
229 | * |
||
230 | * @param string $targetHttpMethod The HTTP method that must not be checked |
||
231 | * @param string $path The Path that must be matched. |
||
232 | * |
||
233 | * @return array |
||
234 | */ |
||
235 | |||
236 | protected function checkStaticRouteInOtherMethods($targetHttpMethod, $path) |
||
242 | |||
243 | /** |
||
244 | * Verify if a dynamic route match in another method than the requested. |
||
245 | * |
||
246 | * @param string $targetHttpMethod The HTTP method that must not be checked |
||
247 | * @param string $path The Path that must be matched. |
||
248 | * |
||
249 | * @return array |
||
250 | */ |
||
251 | |||
252 | protected function checkDynamicRouteInOtherMethods($targetHttpMethod, $path) |
||
258 | |||
259 | /** |
||
260 | * Strip the given http methods and return all the others. |
||
261 | * |
||
262 | * @param array|string |
||
263 | * @return array |
||
264 | */ |
||
265 | |||
266 | protected function getHttpMethodsBut($targetHttpMethod) |
||
270 | |||
271 | /** |
||
272 | * @return Collector |
||
273 | */ |
||
274 | |||
275 | public function getCollector() |
||
279 | |||
280 | /** |
||
281 | * @return string |
||
282 | */ |
||
283 | |||
284 | public function getBasePath() |
||
288 | |||
289 | /** |
||
290 | * Set a new basepath, this will be a prefix that must be excluded in |
||
291 | * every requested Path. |
||
292 | * |
||
293 | * @param string $basepath The new basepath |
||
294 | */ |
||
295 | |||
296 | public function setBasePath($basepath) |
||
300 | |||
301 | } |
||
302 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.