@@ -4,31 +4,31 @@ discard block |
||
4 | 4 | |
5 | 5 | use Exception; |
6 | 6 | |
7 | -trait CheckTrait{ |
|
7 | +trait CheckTrait { |
|
8 | 8 | use FilterTrait; |
9 | 9 | |
10 | 10 | protected function check_protocol(string $expected, string $current) |
11 | 11 | { |
12 | - if($expected !== $current){ |
|
13 | - throw new Exception('Page not found.',404); |
|
12 | + if ($expected !== $current) { |
|
13 | + throw new Exception('Page not found.', 404); |
|
14 | 14 | } |
15 | 15 | } |
16 | 16 | |
17 | - protected function check_name(string $route_name){ |
|
18 | - if(!array_key_exists($route_name,$this->routers)){ |
|
19 | - throw new Exception('Page not found.',404); |
|
17 | + protected function check_name(string $route_name) { |
|
18 | + if (!array_key_exists($route_name, $this->routers)) { |
|
19 | + throw new Exception('Page not found.', 404); |
|
20 | 20 | } |
21 | 21 | } |
22 | 22 | |
23 | - protected function check_typeRole($role){ |
|
24 | - if(!is_string($role) && @get_class($role) !== 'Closure' ){ |
|
23 | + protected function check_typeRole($role) { |
|
24 | + if (!is_string($role) && @get_class($role) !== 'Closure') { |
|
25 | 25 | throw new Exception("Improperly defined route track."); |
26 | 26 | } |
27 | 27 | } |
28 | 28 | |
29 | 29 | protected function check_config() |
30 | 30 | { |
31 | - if(!defined('ROUTER_CONFIG')){ |
|
31 | + if (!defined('ROUTER_CONFIG')) { |
|
32 | 32 | throw new Exception("Information for loading routes has not been defined."); |
33 | 33 | } |
34 | 34 | } |
@@ -40,12 +40,12 @@ discard block |
||
40 | 40 | |
41 | 41 | protected function check_parameters(array $route_loop, array $route_request): bool |
42 | 42 | { |
43 | - foreach($route_loop as $rr => $param){ |
|
44 | - if( (substr($param,0,1) === '{') ){ |
|
45 | - $_GET[ substr($param,1,strlen($param)-2) ] = $route_request[$rr]; |
|
43 | + foreach ($route_loop as $rr => $param) { |
|
44 | + if ((substr($param, 0, 1) === '{')) { |
|
45 | + $_GET[substr($param, 1, strlen($param)-2)] = $route_request[$rr]; |
|
46 | 46 | } |
47 | 47 | |
48 | - if($this->check_parameter($param, $route_request[$rr])){ |
|
48 | + if ($this->check_parameter($param, $route_request[$rr])) { |
|
49 | 49 | return false; |
50 | 50 | } |
51 | 51 | } |
@@ -54,22 +54,22 @@ discard block |
||
54 | 54 | |
55 | 55 | protected function check_parameter(string $route_loop, string $route_request) |
56 | 56 | { |
57 | - return !( substr($route_loop,0,1) === '{' ) and $route_loop !== $route_request; |
|
57 | + return !(substr($route_loop, 0, 1) === '{') and $route_loop !== $route_request; |
|
58 | 58 | } |
59 | 59 | |
60 | 60 | protected function check_role() |
61 | 61 | { |
62 | - if(!array_key_exists('role', $this->getData()['POST'])){ |
|
62 | + if (!array_key_exists('role', $this->getData()['POST'])) { |
|
63 | 63 | throw new Exception('O servidor não conseguiu identificar a finalidade deste formulário.'); |
64 | 64 | } |
65 | 65 | } |
66 | 66 | |
67 | 67 | protected function hasProtocol(array $route, string $currentProtocol) |
68 | 68 | { |
69 | - $protocols = ( is_array($route['protocol']) ) ? $route['protocol'] : [ $route['protocol'] ]; |
|
69 | + $protocols = (is_array($route['protocol'])) ? $route['protocol'] : [$route['protocol']]; |
|
70 | 70 | |
71 | - foreach($protocols as $protocol){ |
|
72 | - if($protocol !== $currentProtocol){ |
|
71 | + foreach ($protocols as $protocol) { |
|
72 | + if ($protocol !== $currentProtocol) { |
|
73 | 73 | continue; |
74 | 74 | } |
75 | 75 | } |
@@ -2,16 +2,16 @@ discard block |
||
2 | 2 | |
3 | 3 | namespace HnrAzevedo\Router; |
4 | 4 | |
5 | -trait Helper{ |
|
5 | +trait Helper { |
|
6 | 6 | use CheckTrait, ControllerTrait; |
7 | 7 | |
8 | 8 | protected function getProtocol(): string |
9 | 9 | { |
10 | - if((isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')){ |
|
10 | + if ((isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')) { |
|
11 | 11 | return 'ajax'; |
12 | 12 | } |
13 | 13 | |
14 | - return (array_key_existS('REQUEST_METHOD',$_SESSION)) ? strtolower($_SERVER['REQUEST_METHOD']) : 'get'; |
|
14 | + return (array_key_existS('REQUEST_METHOD', $_SESSION)) ? strtolower($_SERVER['REQUEST_METHOD']) : 'get'; |
|
15 | 15 | } |
16 | 16 | |
17 | 17 | protected function getData(): ?array |
@@ -27,13 +27,13 @@ discard block |
||
27 | 27 | protected function import(string $path) |
28 | 28 | { |
29 | 29 | foreach (scandir($path) as $routeFile) { |
30 | - if(pathinfo($path.DIRECTORY_SEPARATOR.$routeFile, PATHINFO_EXTENSION) === 'php'){ |
|
31 | - require_once($path. DIRECTORY_SEPARATOR .$routeFile); |
|
30 | + if (pathinfo($path.DIRECTORY_SEPARATOR.$routeFile, PATHINFO_EXTENSION) === 'php') { |
|
31 | + require_once($path.DIRECTORY_SEPARATOR.$routeFile); |
|
32 | 32 | } |
33 | 33 | } |
34 | 34 | } |
35 | 35 | |
36 | - protected function ControllerForm($controller, string $method, array $values){ |
|
36 | + protected function ControllerForm($controller, string $method, array $values) { |
|
37 | 37 | $this->check_role(); |
38 | 38 | $role = ($method !== 'method') ? $method : $this->getData()['POST']['role']; |
39 | 39 | $data = (!is_null($values)) ? json_decode($values['data']) : null; |
@@ -45,7 +45,7 @@ discard block |
||
45 | 45 | $data = $this->getData(); |
46 | 46 | |
47 | 47 | foreach ($data['GET'] as $name => $value) { |
48 | - $controll = str_replace('{'.$name.'}',$value,$controll); |
|
48 | + $controll = str_replace('{'.$name.'}', $value, $controll); |
|
49 | 49 | } |
50 | 50 | |
51 | 51 | $this->check_controllsettable($controll); |
@@ -54,11 +54,11 @@ discard block |
||
54 | 54 | |
55 | 55 | $this->check_controllmethod($controll); |
56 | 56 | |
57 | - $controller = ROUTER_CONFIG['controller.namespace'].'\\'. ucfirst(explode(':',$controll)[0]); |
|
57 | + $controller = ROUTER_CONFIG['controller.namespace'].'\\'.ucfirst(explode(':', $controll)[0]); |
|
58 | 58 | $controller = new $controller(); |
59 | - $method = explode(':',$controll)[1]; |
|
59 | + $method = explode(':', $controll)[1]; |
|
60 | 60 | |
61 | - if( ( $this->getProtocol() == 'form') ){ |
|
61 | + if (($this->getProtocol() == 'form')) { |
|
62 | 62 | $this->ControllerForm($controller, $method, $data['POST']); |
63 | 63 | }else { |
64 | 64 | $controller->$method($data); |
@@ -67,12 +67,12 @@ discard block |
||
67 | 67 | |
68 | 68 | protected function explodeRoute(bool $bar, string $url): array |
69 | 69 | { |
70 | - return explode( '/', $bar ? substr($url, 0, -1) : $url ); |
|
70 | + return explode('/', $bar ? substr($url, 0, -1) : $url); |
|
71 | 71 | } |
72 | 72 | |
73 | 73 | protected function toHiking($walking) |
74 | 74 | { |
75 | - if(is_string($walking)){ |
|
75 | + if (is_string($walking)) { |
|
76 | 76 | $this->Controller($walking); |
77 | 77 | return true; |
78 | 78 | } |
@@ -4,7 +4,7 @@ discard block |
||
4 | 4 | |
5 | 5 | use Exception; |
6 | 6 | |
7 | -class Router{ |
|
7 | +class Router { |
|
8 | 8 | use Helper, CheckTrait; |
9 | 9 | |
10 | 10 | private static $instance = null; |
@@ -59,12 +59,12 @@ discard block |
||
59 | 59 | return self::getInstance()->set($uri, $walking, $protocol); |
60 | 60 | } |
61 | 61 | |
62 | - public function set($url ,$walking , string $protocol): Router |
|
62 | + public function set($url, $walking, string $protocol): Router |
|
63 | 63 | { |
64 | - $url = (substr($url,0,1) !=='/' and strlen($url) > 0) ? "/{$url}" : $url; |
|
64 | + $url = (substr($url, 0, 1) !== '/' and strlen($url) > 0) ? "/{$url}" : $url; |
|
65 | 65 | |
66 | - foreach($this->routers as $key => $value){ |
|
67 | - if( md5($this->prefix . $value['url'] . $value['protocol'] ) === md5( $url . $protocol ) ){ |
|
66 | + foreach ($this->routers as $key => $value) { |
|
67 | + if (md5($this->prefix.$value['url'].$value['protocol']) === md5($url.$protocol)) { |
|
68 | 68 | throw new Exception("There is already a route with the url {$url} and with the {$protocol} protocol configured."); |
69 | 69 | } |
70 | 70 | } |
@@ -84,9 +84,9 @@ discard block |
||
84 | 84 | return self::getInstance(); |
85 | 85 | } |
86 | 86 | |
87 | - public static function group(string $prefix,$callback): Router |
|
87 | + public static function group(string $prefix, $callback): Router |
|
88 | 88 | { |
89 | - self::getInstance()->prefix = (substr($prefix,0,1) !== '/') ? "/{$prefix}" : $prefix; |
|
89 | + self::getInstance()->prefix = (substr($prefix, 0, 1) !== '/') ? "/{$prefix}" : $prefix; |
|
90 | 90 | self::getInstance()->group = sha1(date('d/m/Y h:m:i')); |
91 | 91 | $callback(); |
92 | 92 | self::getInstance()->group = null; |
@@ -97,14 +97,14 @@ discard block |
||
97 | 97 | |
98 | 98 | public static function name(string $name): Router |
99 | 99 | { |
100 | - if(self::getInstance()->lastReturn){ |
|
100 | + if (self::getInstance()->lastReturn) { |
|
101 | 101 | throw new Exception("There is no reason to call a {$name} route group."); |
102 | 102 | } |
103 | 103 | |
104 | 104 | $currentRoute = end(self::getInstance()->routers); |
105 | 105 | |
106 | - foreach(self::getInstance()->routers as $key => $value){ |
|
107 | - if(array_key_exists($name, self::getInstance()->routers)){ |
|
106 | + foreach (self::getInstance()->routers as $key => $value) { |
|
107 | + if (array_key_exists($name, self::getInstance()->routers)) { |
|
108 | 108 | throw new Exception("There is already a route with the name {$name} configured."); |
109 | 109 | } |
110 | 110 | } |
@@ -120,7 +120,7 @@ discard block |
||
120 | 120 | |
121 | 121 | private function byName(?string $route_name) |
122 | 122 | { |
123 | - if(!is_null($route_name)){ |
|
123 | + if (!is_null($route_name)) { |
|
124 | 124 | $currentProtocol = $this->getProtocol(); |
125 | 125 | |
126 | 126 | $this->check_name($route_name); |
@@ -142,17 +142,17 @@ discard block |
||
142 | 142 | |
143 | 143 | $currentProtocol = $this->getProtocol(); |
144 | 144 | |
145 | - foreach(array_reverse($this->routers) as $r => $route){ |
|
145 | + foreach (array_reverse($this->routers) as $r => $route) { |
|
146 | 146 | |
147 | 147 | $this->hasProtocol($route, $currentProtocol); |
148 | 148 | |
149 | - $route_loop = $this->explodeRoute( (substr($route['url'],strlen($route['url'])-1,1) === '/') , $route['url']); |
|
149 | + $route_loop = $this->explodeRoute((substr($route['url'], strlen($route['url'])-1, 1) === '/'), $route['url']); |
|
150 | 150 | |
151 | 151 | $_SERVER['REQUEST_URI'] = (array_key_exists('REQUEST_URI', $_SERVER)) ? $_SERVER['REQUEST_URI'] : ''; |
152 | 152 | |
153 | - $route_request = $this->explodeRoute((substr($_SERVER['REQUEST_URI'],strlen($_SERVER['REQUEST_URI'])-1,1) === '/') , $_SERVER['REQUEST_URI']); |
|
153 | + $route_request = $this->explodeRoute((substr($_SERVER['REQUEST_URI'], strlen($_SERVER['REQUEST_URI'])-1, 1) === '/'), $_SERVER['REQUEST_URI']); |
|
154 | 154 | |
155 | - if($this->check_numparams($route_loop, $route_request) || !$this->check_parameters($route_loop, $route_request)){ |
|
155 | + if ($this->check_numparams($route_loop, $route_request) || !$this->check_parameters($route_loop, $route_request)) { |
|
156 | 156 | continue; |
157 | 157 | } |
158 | 158 | |
@@ -162,18 +162,18 @@ discard block |
||
162 | 162 | return true; |
163 | 163 | } |
164 | 164 | |
165 | - throw new Exception('Page not found.',404); |
|
165 | + throw new Exception('Page not found.', 404); |
|
166 | 166 | } |
167 | 167 | |
168 | 168 | public static function filter($filters): Router |
169 | 169 | { |
170 | - if(self::getInstance()->lastReturn !== null){ |
|
170 | + if (self::getInstance()->lastReturn !== null) { |
|
171 | 171 | $currentGroup = end(self::getInstance()->routers)['group']; |
172 | 172 | |
173 | 173 | foreach (self::getInstance()->routers as $key => $value) { |
174 | 174 | |
175 | - if($value['group'] === $currentGroup){ |
|
176 | - $currentRoute = self::getInstance()->addFilter(self::getInstance()->routers[$key],$filters); |
|
175 | + if ($value['group'] === $currentGroup) { |
|
176 | + $currentRoute = self::getInstance()->addFilter(self::getInstance()->routers[$key], $filters); |
|
177 | 177 | self::getInstance()->routers[$key] = $currentRoute; |
178 | 178 | } |
179 | 179 | |
@@ -182,24 +182,24 @@ discard block |
||
182 | 182 | return self::getInstance(); |
183 | 183 | } |
184 | 184 | |
185 | - self::getInstance()->routers[count(self::getInstance()->routers)-1] = self::getInstance()->addFilter(end(self::getInstance()->routers),$filters); |
|
185 | + self::getInstance()->routers[count(self::getInstance()->routers)-1] = self::getInstance()->addFilter(end(self::getInstance()->routers), $filters); |
|
186 | 186 | return self::getInstance(); |
187 | 187 | } |
188 | 188 | |
189 | 189 | public static function addFilter(array $route, $filter): array |
190 | 190 | { |
191 | - if(is_null($route['filters'])){ |
|
191 | + if (is_null($route['filters'])) { |
|
192 | 192 | $route['filters'] = $filter; |
193 | 193 | return $route; |
194 | 194 | } |
195 | 195 | |
196 | 196 | $filters = (is_array($filter)) ? $filter : [0 => $filter]; |
197 | 197 | |
198 | - if(is_array($route['filters'])){ |
|
198 | + if (is_array($route['filters'])) { |
|
199 | 199 | foreach ($route['filters'] as $key => $value) { |
200 | 200 | $filters[] = $value; |
201 | 201 | } |
202 | - }else{ |
|
202 | + }else { |
|
203 | 203 | $filters[] = $route['filters']; |
204 | 204 | } |
205 | 205 |
@@ -4,7 +4,7 @@ |
||
4 | 4 | |
5 | 5 | use HnrAzevedo\Router\Controller; |
6 | 6 | |
7 | -class User extends Controller{ |
|
7 | +class User extends Controller { |
|
8 | 8 | |
9 | 9 | public function my_account(array $data): void |
10 | 10 | { |
@@ -5,14 +5,14 @@ discard block |
||
5 | 5 | use HnrAzevedo\Validator\Validator; |
6 | 6 | use Exception; |
7 | 7 | |
8 | -class Controller{ |
|
8 | +class Controller { |
|
9 | 9 | use Helper; |
10 | 10 | |
11 | 11 | protected array $fail = []; |
12 | 12 | |
13 | 13 | private function check_method(string $method): void |
14 | 14 | { |
15 | - if(!method_exists($this,$method)){ |
|
15 | + if (!method_exists($this, $method)) { |
|
16 | 16 | throw new Exception("{$method} not found in ".get_class($this)."."); |
17 | 17 | } |
18 | 18 | } |
@@ -21,7 +21,7 @@ discard block |
||
21 | 21 | { |
22 | 22 | $this->ValidateData(); |
23 | 23 | |
24 | - if($this->check_failData()){ |
|
24 | + if ($this->check_failData()) { |
|
25 | 25 | return false; |
26 | 26 | } |
27 | 27 | |
@@ -38,8 +38,8 @@ discard block |
||
38 | 38 | { |
39 | 39 | $valid = Validator::execute($this->getData()['POST']['data']); |
40 | 40 | |
41 | - if(!$valid){ |
|
42 | - foreach(Validator::getErrors() as $err => $message){ |
|
41 | + if (!$valid) { |
|
42 | + foreach (Validator::getErrors() as $err => $message) { |
|
43 | 43 | $this->fail[] = [ |
44 | 44 | 'input' => array_keys($message)[0], |
45 | 45 | 'message' => $message[array_keys($message)[0]] |
@@ -50,12 +50,12 @@ discard block |
||
50 | 50 | |
51 | 51 | private function check_failData(): bool |
52 | 52 | { |
53 | - if(count($this->fail) > 0 ){ |
|
53 | + if (count($this->fail) > 0) { |
|
54 | 54 | echo json_encode([ |
55 | 55 | 'error'=> $this->fail |
56 | 56 | ]); |
57 | 57 | } |
58 | - return (count($this->fail) > 0 ); |
|
58 | + return (count($this->fail) > 0); |
|
59 | 59 | } |
60 | 60 | |
61 | 61 | } |
62 | 62 | \ No newline at end of file |