Skip to content

Commit f786a14

Browse files
authored
Merge pull request #43 from PhpSlides/dev
fixes errors
2 parents 903d00a + 1216034 commit f786a14

File tree

10 files changed

+270
-351
lines changed

10 files changed

+270
-351
lines changed

Router/MapRoute.php

+57-89
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use PhpSlides\Foundation\Application;
77
use PhpSlides\Interface\MapInterface;
88

9-
109
/**
1110
* Class MapRoute
1211
*
@@ -34,7 +33,6 @@ class MapRoute extends Controller implements MapInterface
3433
*/
3534
private static array $method;
3635

37-
3836
/**
3937
* Matches the given HTTP method and route against the current request URI.
4038
*
@@ -64,32 +62,25 @@ public function match(string $method, string|array $route): bool|array
6462
* ----------------------------------------------
6563
*/
6664
self::$request_uri = strtolower(
67-
preg_replace("/(^\/)|(\/$)/", '', Application::$request_uri),
65+
preg_replace("/(^\/)|(\/$)/", '', Application::$request_uri),
6866
);
6967
self::$request_uri = empty(self::$request_uri) ? '/' : self::$request_uri;
7068

7169
self::$route = is_array($route)
72-
? $route
73-
: strtolower(preg_replace("/(^\/)|(\/$)/", '', $route));
70+
? $route
71+
: strtolower(preg_replace("/(^\/)|(\/$)/", '', $route));
7472

7573
// Firstly, resolve route with pattern
76-
if (is_array(self::$route))
77-
{
78-
foreach (self::$route as $value)
79-
{
80-
if (str_starts_with('pattern:', $value))
81-
{
74+
if (is_array(self::$route)) {
75+
foreach (self::$route as $value) {
76+
if (str_starts_with($value, 'pattern:')) {
77+
if ($p = $this->pattern()) {
78+
return $p;
79+
}
8280
$pattern = true;
8381
}
8482
}
85-
86-
if ($pattern === true)
87-
{
88-
return $this->pattern();
89-
}
90-
}
91-
else if (str_starts_with('pattern:', self::$route))
92-
{
83+
} elseif (str_starts_with(self::$route, 'pattern:')) {
9384
return $this->pattern();
9485
}
9586

@@ -101,14 +92,16 @@ public function match(string $method, string|array $route): bool|array
10192
$paramKey = [];
10293

10394
// finding if there is any {?} parameter in $route
104-
if (is_string(self::$route))
105-
{
95+
if (is_string(self::$route)) {
10696
preg_match_all('/(?<={).+?(?=})/', self::$route, $paramMatches);
10797
}
10898

10999
// if the route does not contain any param call routing();
110-
if (empty($paramMatches) || is_array(self::$route))
111-
{
100+
if (
101+
empty($paramMatches) ||
102+
empty($paramMatches[0] ?? []) ||
103+
is_array(self::$route)
104+
) {
112105
/**
113106
* ------------------------------------------------------
114107
* | Check if $callback is a callable function
@@ -120,8 +113,7 @@ public function match(string $method, string|array $route): bool|array
120113
}
121114

122115
// setting parameters names
123-
foreach ($paramMatches[0] as $key)
124-
{
116+
foreach ($paramMatches[0] as $key) {
125117
$paramKey[] = $key;
126118
}
127119

@@ -132,10 +124,8 @@ public function match(string $method, string|array $route): bool|array
132124
$indexNum = [];
133125

134126
// storing index number, where {?} parameter is required with the help of regex
135-
foreach ($uri as $index => $param)
136-
{
137-
if (preg_match('/{.*}/', $param))
138-
{
127+
foreach ($uri as $index => $param) {
128+
if (preg_match('/{.*}/', $param)) {
139129
$indexNum[] = $index;
140130
}
141131
}
@@ -146,22 +136,19 @@ public function match(string $method, string|array $route): bool|array
146136
* ----------------------------------------------------------------------------------
147137
*/
148138
$reqUri = explode('/', self::$request_uri);
149-
150139
/**
151140
* ----------------------------------------------------------------------------------
152141
* | Running for each loop to set the exact index number with reg expression this will help in matching route
153142
* ----------------------------------------------------------------------------------
154143
*/
155-
foreach ($indexNum as $key => $index)
156-
{
144+
foreach ($indexNum as $key => $index) {
157145
/**
158146
* --------------------------------------------------------------------------------
159147
* | In case if req uri with param index is empty then return because URL is not valid for this route
160148
* --------------------------------------------------------------------------------
161149
*/
162150

163-
if (empty($reqUri[$index]))
164-
{
151+
if (empty($reqUri[$index])) {
165152
return false;
166153
}
167154

@@ -185,30 +172,27 @@ public function match(string $method, string|array $route): bool|array
185172
$reqUri = str_replace('/', '\\/', $reqUri);
186173

187174
// now matching route with regex
188-
if (preg_match("/$reqUri/", self::$route))
189-
{
175+
if (preg_match("/$reqUri/", self::$route)) {
190176
// checks if the requested method is of the given route
191177
if (
192-
!in_array($_SERVER['REQUEST_METHOD'], self::$method) &&
193-
!in_array('*', self::$method)
194-
)
195-
{
178+
!in_array($_SERVER['REQUEST_METHOD'], self::$method) &&
179+
!in_array('*', self::$method)
180+
) {
196181
http_response_code(405);
197182
exit('Method Not Allowed');
198183
}
199184

200185
return [
201-
'method' => $_SERVER['REQUEST_METHOD'],
202-
'route' => self::$route,
203-
'params_value' => $req_value,
204-
'params' => $req,
186+
'method' => $_SERVER['REQUEST_METHOD'],
187+
'route' => self::$route,
188+
'params_value' => $req_value,
189+
'params' => $req,
205190
];
206191
}
207192

208193
return false;
209194
}
210195

211-
212196
/**
213197
* Matches the current request URI and method against the defined routes.
214198
*
@@ -219,53 +203,44 @@ public function match(string $method, string|array $route): bool|array
219203
*
220204
* @return bool|array Returns an array with 'method' and 'route' keys if a match is found, otherwise false.
221205
*/
222-
private function match_routing (): bool|array
206+
private function match_routing(): bool|array
223207
{
224208
$uri = [];
225209
$str_route = '';
226210

227-
if (is_array(self::$route))
228-
{
229-
for ($i = 0; $i < count(self::$route); $i++)
230-
{
211+
if (is_array(self::$route)) {
212+
for ($i = 0; $i < count(self::$route); $i++) {
231213
$each_route = preg_replace("/(^\/)|(\/$)/", '', self::$route[$i]);
232214

233215
empty($each_route)
234-
? array_push($uri, strtolower('/'))
235-
: array_push($uri, strtolower($each_route));
216+
? array_push($uri, strtolower('/'))
217+
: array_push($uri, strtolower($each_route));
236218
}
237-
}
238-
else
239-
{
219+
} else {
240220
$str_route = empty(self::$route) ? '/' : self::$route;
241221
}
242222

243223
if (
244-
in_array(self::$request_uri, $uri) ||
245-
self::$request_uri === $str_route
246-
)
247-
{
224+
in_array(self::$request_uri, $uri) ||
225+
self::$request_uri === $str_route
226+
) {
248227
if (
249-
!in_array($_SERVER['REQUEST_METHOD'], self::$method) &&
250-
!in_array('*', haystack: self::$method)
251-
)
252-
{
228+
!in_array($_SERVER['REQUEST_METHOD'], self::$method) &&
229+
!in_array('*', haystack: self::$method)
230+
) {
253231
http_response_code(405);
254232
exit('Method Not Allowed');
255233
}
256234

257235
return [
258-
'method' => $_SERVER['REQUEST_METHOD'],
259-
'route' => self::$route,
236+
'method' => $_SERVER['REQUEST_METHOD'],
237+
'route' => self::$route,
260238
];
261-
}
262-
else
263-
{
239+
} else {
264240
return false;
265241
}
266242
}
267243

268-
269244
/**
270245
* Validates and matches a route pattern.
271246
*
@@ -276,18 +251,14 @@ private function match_routing (): bool|array
276251
*
277252
* @return array|bool The matched pattern as an array if found, or false if no match is found.
278253
*/
279-
private function pattern (): array|bool
254+
private function pattern(): array|bool
280255
{
281-
if (is_array(self::$route))
282-
{
283-
foreach (self::$route as $value)
284-
{
285-
if (str_starts_with('pattern:', $value))
286-
{
256+
if (is_array(self::$route)) {
257+
foreach (self::$route as $value) {
258+
if (str_starts_with('pattern:', $value)) {
287259
$matched = $this->validatePattern($value);
288260

289-
if ($matched)
290-
{
261+
if ($matched) {
291262
return $matched;
292263
}
293264
}
@@ -298,31 +269,28 @@ private function pattern (): array|bool
298269
return $this->validatePattern(self::$route);
299270
}
300271

301-
302272
/**
303273
* Validates the given pattern against the request URI and checks the request method.
304274
*
305275
* @param string $pattern The pattern to validate.
306276
* @return array|bool Returns an array with the request method and route if the pattern matches, otherwise false.
307277
*/
308-
private function validatePattern (string $pattern): array|bool
278+
private function validatePattern(string $pattern): array|bool
309279
{
310-
$pattern = trim(substr($pattern, 8));
280+
$pattern = preg_replace("/(^\/)|(\/$)/", '', trim(substr($pattern, 8)));
311281

312-
if (fnmatch($pattern, self::$request_uri))
313-
{
282+
if (fnmatch($pattern, self::$request_uri)) {
314283
if (
315-
!in_array($_SERVER['REQUEST_METHOD'], self::$method) &&
316-
!in_array('*', self::$method)
317-
)
318-
{
284+
!in_array($_SERVER['REQUEST_METHOD'], self::$method) &&
285+
!in_array('*', self::$method)
286+
) {
319287
http_response_code(405);
320288
exit('Method Not Allowed');
321289
}
322290

323291
return [
324-
'method' => $_SERVER['REQUEST_METHOD'],
325-
'route' => self::$route,
292+
'method' => $_SERVER['REQUEST_METHOD'],
293+
'route' => self::$route,
326294
];
327295
}
328296
return false;

0 commit comments

Comments
 (0)