1 | <?php |
||
33 | class Regex implements Matcher |
||
34 | { |
||
35 | /** |
||
36 | * Regex for getting URL variables |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | const VARIABLE_REGEX = |
||
41 | "~\{ |
||
42 | \s* ([a-zA-Z0-9_]*) \s* |
||
43 | (?: |
||
44 | : \s* ([^{]+(?:\{.*?\})?) |
||
45 | )? |
||
46 | \}\??~x"; |
||
47 | |||
48 | /** |
||
49 | * Shortcuts for regex. |
||
50 | * |
||
51 | * @var array |
||
52 | */ |
||
53 | private $regexShortcuts = [ |
||
54 | ':i}' => ':[0-9]+}', |
||
55 | ':a}' => ':[0-9A-Za-z]+}', |
||
56 | ':h}' => ':[0-9A-Fa-f]+}', |
||
57 | ':c}' => ':[a-zA-Z0-9+_\-\.]+}' |
||
58 | ]; |
||
59 | |||
60 | /** |
||
61 | * Flag for enabling support of mode rewrite. |
||
62 | * |
||
63 | * @var string |
||
64 | */ |
||
65 | private $urlFormat = RouteMode::PATH_FORMAT; |
||
66 | |||
67 | /** |
||
68 | * Name of GET parameter from which the route will be get |
||
69 | * if url format is set to GET_FORMAT. |
||
70 | * |
||
71 | * @var string |
||
72 | */ |
||
73 | private $modeRewriteParameter = 'r'; |
||
74 | |||
75 | /** |
||
76 | * Instance of route collection |
||
77 | * |
||
78 | * @var \Kambo\Router\Route\Collection |
||
79 | */ |
||
80 | private $routeCollection; |
||
81 | |||
82 | /** |
||
83 | * Constructor |
||
84 | * |
||
85 | * @param \Kambo\Router\Route\Collection $routeCollection |
||
86 | * |
||
87 | */ |
||
88 | 43 | public function __construct( |
|
93 | |||
94 | /** |
||
95 | * Match request with provided routes. |
||
96 | * Get method and url from provided request and start matching. |
||
97 | * |
||
98 | * @param ServerRequest $request instance of PSR 7 compatible request object |
||
99 | * |
||
100 | * @return mixed |
||
101 | */ |
||
102 | 23 | public function matchRequest(ServerRequest $request) |
|
109 | |||
110 | /** |
||
111 | * Match url and method with provided routes. |
||
112 | * |
||
113 | * @param string $method http method |
||
114 | * @param string $url url |
||
115 | * |
||
116 | * @return mixed |
||
117 | */ |
||
118 | 16 | public function matchPathAndMethod(string $method, string $url) |
|
122 | |||
123 | /** |
||
124 | * Set format for URL resolving. |
||
125 | * If the path mode is set to a path a web server must be properly |
||
126 | * configurated, defualt value is PATH_FORMAT. |
||
127 | * |
||
128 | * @param string $urlFormat value from RouteMode enum |
||
129 | * |
||
130 | * @return self for fluent interface |
||
131 | */ |
||
132 | 8 | public function setUrlFormat(string $urlFormat) : Matcher |
|
144 | |||
145 | /** |
||
146 | * Get format for URL resolving. |
||
147 | * |
||
148 | * @return string value from RouteMode enum |
||
149 | */ |
||
150 | 2 | public function getUrlFormat() : string |
|
154 | |||
155 | // ------------ PRIVATE METHODS |
||
156 | |||
157 | /** |
||
158 | * Match method and route with provided routes. |
||
159 | * If route and method match a route is dispatch using provided dispatcher. |
||
160 | * |
||
161 | * @param string $method http method |
||
162 | * @param string $url url |
||
163 | * |
||
164 | * @return mixed |
||
165 | */ |
||
166 | 39 | private function getMatchRoute(string $method, string $url) |
|
185 | |||
186 | /** |
||
187 | * Match route by provided regex. |
||
188 | * |
||
189 | * @param string $routeRegex |
||
190 | * @param string $route |
||
191 | * |
||
192 | * @return mixed |
||
193 | */ |
||
194 | 36 | private function routeMatch(string $routeRegex, string $route) |
|
205 | |||
206 | /** |
||
207 | * Prepare regex and parameters for each of routes. |
||
208 | * |
||
209 | * @param array $routes array with instances of route object |
||
210 | * |
||
211 | * @return array transformed routes |
||
212 | */ |
||
213 | 39 | private function parseRoutes(Collection $routes) : array |
|
229 | |||
230 | /** |
||
231 | * Get route from request object. |
||
232 | * Method expect an instance of PSR 7 compatible request object. |
||
233 | * |
||
234 | * @param object $request |
||
235 | * |
||
236 | * @return string |
||
237 | */ |
||
238 | 23 | private function getUrl($request) : string |
|
255 | |||
256 | /** |
||
257 | * Prepare regex for resolving route a extract variables from route. |
||
258 | * |
||
259 | * @param string $route |
||
260 | * |
||
261 | * @return array regex and parameters |
||
262 | */ |
||
263 | 36 | private function transformRoute(string $route) : array |
|
274 | |||
275 | /** |
||
276 | * Prepare regex for resolving route a extract variables from route. |
||
277 | * |
||
278 | * @param string $route Route |
||
279 | * @param array $variables Variables |
||
280 | * |
||
281 | * @return string Transformed route |
||
282 | */ |
||
283 | 28 | private function transformRouteVariables(string $route, array $variables) : string |
|
301 | |||
302 | /** |
||
303 | * Extract variables from the route |
||
304 | * |
||
305 | * @param string $route |
||
306 | * |
||
307 | * @return array |
||
308 | */ |
||
309 | 36 | private function extractVariableRouteParts(string $route) : array |
|
321 | } |
||
322 |