@@ -45,7 +45,7 @@ |
||
45 | 45 | |
46 | 46 | protected function setRoutes(array $routes): void |
47 | 47 | { |
48 | - $this->routes = $routes; |
|
48 | + $this->routes = $routes; |
|
49 | 49 | } |
50 | 50 | |
51 | 51 | protected function getGroup(): ?string |
@@ -11,8 +11,8 @@ discard block |
||
11 | 11 | public static function where(array $wheres): Router |
12 | 12 | { |
13 | 13 | $route = self::getInstance()->inSave(); |
14 | - $route['where'] = (is_array($route['where'])) ? array_merge($route['where'],$wheres) : $wheres; |
|
15 | - self::getInstance()->updateRoute($route,array_key_last(self::getInstance()->getRoutes())); |
|
14 | + $route['where'] = (is_array($route['where'])) ? array_merge($route['where'], $wheres) : $wheres; |
|
15 | + self::getInstance()->updateRoute($route, array_key_last(self::getInstance()->getRoutes())); |
|
16 | 16 | return self::getInstance(); |
17 | 17 | } |
18 | 18 | |
@@ -22,38 +22,38 @@ discard block |
||
22 | 22 | |
23 | 23 | $this->parameters = []; |
24 | 24 | |
25 | - $uriPath .= (substr($uriPath,strlen($uriPath)-1) !== '/') ? '/' : ''; |
|
25 | + $uriPath .= (substr($uriPath, strlen($uriPath)-1) !== '/') ? '/' : ''; |
|
26 | 26 | |
27 | - $routePath = explode('/',urldecode($route['uri']->getPath())); |
|
27 | + $routePath = explode('/', urldecode($route['uri']->getPath())); |
|
28 | 28 | unset($routePath[0]); |
29 | - $uriPath = explode('/',urldecode($uriPath)); |
|
29 | + $uriPath = explode('/', urldecode($uriPath)); |
|
30 | 30 | unset($uriPath[0]); |
31 | 31 | |
32 | 32 | $corretRoute = true; |
33 | - foreach($routePath as $r => $routeFrag){ |
|
33 | + foreach ($routePath as $r => $routeFrag) { |
|
34 | 34 | $where = is_array($route['where']) ? $route['where'] : []; |
35 | 35 | $routeFrag = $this->replaceParam($where, $routeFrag, $uriPath[$r]); |
36 | 36 | |
37 | - if($routeFrag !== $uriPath[$r]){ |
|
37 | + if ($routeFrag !== $uriPath[$r]) { |
|
38 | 38 | $corretRoute = false; |
39 | 39 | } |
40 | 40 | } |
41 | 41 | |
42 | - if(!$corretRoute){ |
|
42 | + if (!$corretRoute) { |
|
43 | 43 | throw new \Exception('continue'); |
44 | 44 | } |
45 | 45 | |
46 | - $_REQUEST = array_merge($_REQUEST,$this->parameters); |
|
46 | + $_REQUEST = array_merge($_REQUEST, $this->parameters); |
|
47 | 47 | } |
48 | 48 | |
49 | 49 | private function replaceParam(array $where, string $ref, string $value): string |
50 | 50 | { |
51 | - if(((substr($ref,0,1) === '{') && (substr($ref,strlen($ref)-1) === '}'))) { |
|
52 | - $this->parameters[str_replace(['{:','{','}'],'',$ref)] = $value; |
|
51 | + if (((substr($ref, 0, 1) === '{') && (substr($ref, strlen($ref)-1) === '}'))) { |
|
52 | + $this->parameters[str_replace(['{:', '{', '}'], '', $ref)] = $value; |
|
53 | 53 | |
54 | - $this->checkValueRequire($ref,$value); |
|
54 | + $this->checkValueRequire($ref, $value); |
|
55 | 55 | |
56 | - if(array_key_exists(str_replace(['{:','{','}'],'',$ref),$where)){ |
|
56 | + if (array_key_exists(str_replace(['{:', '{', '}'], '', $ref), $where)) { |
|
57 | 57 | $this->matchParam($where, $ref, $value); |
58 | 58 | } |
59 | 59 | |
@@ -64,17 +64,17 @@ discard block |
||
64 | 64 | |
65 | 65 | private function checkValueRequire(string $ref, string $value): void |
66 | 66 | { |
67 | - if(substr($ref,0,2) !== '{:' && strlen($value) === 0){ |
|
67 | + if (substr($ref, 0, 2) !== '{:' && strlen($value) === 0) { |
|
68 | 68 | throw new \Exception('continue'); |
69 | 69 | } |
70 | 70 | } |
71 | 71 | |
72 | 72 | private function checkCount(string $routePath, string $uriPath): void |
73 | 73 | { |
74 | - $countRequest = substr_count($uriPath,'/') - substr_count($routePath,'{:'); |
|
75 | - $countRoute = substr_count($routePath,'/') - substr_count($routePath,'{:'); |
|
74 | + $countRequest = substr_count($uriPath, '/')-substr_count($routePath, '{:'); |
|
75 | + $countRoute = substr_count($routePath, '/')-substr_count($routePath, '{:'); |
|
76 | 76 | |
77 | - if($countRequest !== $countRoute){ |
|
77 | + if ($countRequest !== $countRoute) { |
|
78 | 78 | throw new \Exception('continue'); |
79 | 79 | } |
80 | 80 | } |
@@ -82,9 +82,9 @@ discard block |
||
82 | 82 | private function matchParam(array $where, string $ref, string $value): void |
83 | 83 | { |
84 | 84 | echo 3; |
85 | - if(substr($ref,0,2) === '{' || $value !== ''){ |
|
85 | + if (substr($ref, 0, 2) === '{' || $value !== '') { |
|
86 | 86 | echo 1; |
87 | - if(!preg_match("/^{$where[str_replace(['{:','{','}'],'',$ref)]}$/",$value)){ |
|
87 | + if (!preg_match("/^{$where[str_replace(['{:', '{', '}'], '', $ref)]}$/", $value)) { |
|
88 | 88 | echo 2; |
89 | 89 | throw new \Exception('continue'); |
90 | 90 | } |
@@ -57,9 +57,9 @@ discard block |
||
57 | 57 | |
58 | 58 | $routes = self::getInstance()->getRoutes(); |
59 | 59 | $routes[] = [ |
60 | - 'uri' => new Uri(self::getInstance()->getHost().self::getInstance()->getPrefix().$uri), |
|
61 | - 'action' => $closure, |
|
62 | - 'method' => strtoupper($method), |
|
60 | + 'uri' => new Uri(self::getInstance()->getHost().self::getInstance()->getPrefix().$uri), |
|
61 | + 'action' => $closure, |
|
62 | + 'method' => strtoupper($method), |
|
63 | 63 | 'middlewares' => null, |
64 | 64 | 'where' => null, |
65 | 65 | 'before' => [], |
@@ -76,7 +76,7 @@ discard block |
||
76 | 76 | private static function checkDuplicity(string $uri, string $method): void |
77 | 77 | { |
78 | 78 | foreach(self::getInstance()->getRoutes() as $route){ |
79 | - if( md5($route['uri'].$route['method']) === md5($uri.$method) ){ |
|
79 | + if( md5($route['uri'].$route['method']) === md5($uri.$method) ){ |
|
80 | 80 | throw new \RuntimeException("There is already a route with the URI {$uri} and with the {$method} METHOD configured."); |
81 | 81 | } |
82 | 82 | } |
@@ -10,28 +10,28 @@ discard block |
||
10 | 10 | |
11 | 11 | protected function hasRouteName(string $name): void |
12 | 12 | { |
13 | - if(!isset($this->routesName[$name])){ |
|
13 | + if (!isset($this->routesName[$name])) { |
|
14 | 14 | throw new \RuntimeException("There is no route named with {$name}"); |
15 | 15 | } |
16 | 16 | } |
17 | 17 | |
18 | 18 | protected function isInNameGroup(): void |
19 | 19 | { |
20 | - if(!is_null($this->getGroup())){ |
|
20 | + if (!is_null($this->getGroup())) { |
|
21 | 21 | throw new \RuntimeException("It is not allowed to assign names to groups"); |
22 | 22 | } |
23 | 23 | } |
24 | 24 | |
25 | 25 | protected function isInPseudGroup(): void |
26 | 26 | { |
27 | - if(!is_null($this->getGroup())){ |
|
27 | + if (!is_null($this->getGroup())) { |
|
28 | 28 | throw new \RuntimeException("To assign actions before or after the execution of the route, use beforeGroup or afterGroup"); |
29 | 29 | } |
30 | 30 | } |
31 | 31 | |
32 | 32 | protected function existRouteName(string $name): void |
33 | 33 | { |
34 | - if(isset($this->routesName[$name])){ |
|
34 | + if (isset($this->routesName[$name])) { |
|
35 | 35 | throw new \RuntimeException("There is already a route named with {$name}"); |
36 | 36 | } |
37 | 37 | } |
@@ -39,12 +39,12 @@ discard block |
||
39 | 39 | protected function checkMethod(array $route, $method): void |
40 | 40 | { |
41 | 41 | $hasMethod = false; |
42 | - foreach(explode('|',$route['method']) as $routeMethod){ |
|
43 | - if(@preg_match("/{$routeMethod}/",$method) !== 0 || $method === '*'){ |
|
42 | + foreach (explode('|', $route['method']) as $routeMethod) { |
|
43 | + if (@preg_match("/{$routeMethod}/", $method) !== 0 || $method === '*') { |
|
44 | 44 | $hasMethod = true; |
45 | 45 | } |
46 | 46 | } |
47 | - if(!$hasMethod){ |
|
47 | + if (!$hasMethod) { |
|
48 | 48 | throw new \Exception('This route is not released for the accessed method'); |
49 | 49 | } |
50 | 50 |