Passed
Branch master (eeb79a)
by Henri
02:29 queued 01:14
created
src/Router.php 3 patches
Indentation   +24 added lines, -24 removed lines patch added patch discarded remove patch
@@ -61,23 +61,23 @@  discard block
 block discarded – undo
61 61
 
62 62
     public function set($url , $role , $protocol = null): 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
+        }
71 71
 
72
-		$route = [
73
-			'url' => $this->prefix.$url,
74
-			'role' => $role,
75
-			'protocol' => $protocol,
76
-			'filters' => null,
72
+        $route = [
73
+            'url' => $this->prefix.$url,
74
+            'role' => $role,
75
+            'protocol' => $protocol,
76
+            'filters' => null,
77 77
             'group' => self::getInstance()->group
78
-		];
78
+        ];
79 79
 
80
-		$this->routers[] = $route;		
80
+        $this->routers[] = $route;		
81 81
         
82 82
         return self::getInstance();
83 83
     }
@@ -139,13 +139,13 @@  discard block
 block discarded – undo
139 139
             return $this->byName($route_name);
140 140
         }
141 141
 
142
-		$currentProtocol = $this->getProtocol();
142
+        $currentProtocol = $this->getProtocol();
143 143
 
144 144
         foreach(array_reverse($this->routers) as $r => $route){
145 145
 
146 146
             $this->hasProtocol($route, $currentProtocol);
147 147
 
148
-	        $route_loop = explode(
148
+            $route_loop = explode(
149 149
                 '/',
150 150
                 (substr($route['url'],strlen($route['url'])-1,1) === '/') 
151 151
                     ? substr($route['url'], 0, -1) 
@@ -154,26 +154,26 @@  discard block
 block discarded – undo
154 154
 
155 155
             /* ONLY FOR DEBUG CONDITION */
156 156
             $route_request = $route_loop;
157
-	        /*$route_request = explode(
157
+            /*$route_request = explode(
158 158
                 '/',
159 159
                 (substr($_SERVER['REQUEST_URI'],strlen($_SERVER['REQUEST_URI'])-1,1) === '/') 
160 160
                 ? substr($_SERVER['REQUEST_URI'], 0, -1) 
161 161
                 : $_SERVER['REQUEST_URI'] 
162 162
             );*/
163 163
 
164
-	        if(count($route_loop) !== count($route_request)){
164
+            if(count($route_loop) !== count($route_request)){
165 165
                 continue;
166 166
             }
167 167
 
168
-	        for($rr = 0; $rr < count($route_loop); $rr++){
169
-	            $param = (substr($route_loop[$rr],0,1)==='{');
168
+            for($rr = 0; $rr < count($route_loop); $rr++){
169
+                $param = (substr($route_loop[$rr],0,1)==='{');
170 170
 
171
-	            if($param){
171
+                if($param){
172 172
                     $param_name = substr($route_loop[$rr],1,strlen($route_loop[$rr])-2);
173
-	                $data[$param_name] = $route_request[$rr];
174
-	            }
173
+                    $data[$param_name] = $route_request[$rr];
174
+                }
175 175
 
176
-	            if(!$param and $route_loop[$rr] !== $route_request[$rr]){
176
+                if(!$param and $route_loop[$rr] !== $route_request[$rr]){
177 177
                     continue 2;
178 178
                 }
179 179
             }
@@ -181,10 +181,10 @@  discard block
 block discarded – undo
181 181
             $this->check_filtering($route);
182 182
 
183 183
             $this->Controller($route['role']);
184
-	        return true;
185
-	    }
184
+            return true;
185
+        }
186 186
 
187
-	    throw new Exception('Page not found.',404);
187
+        throw new Exception('Page not found.',404);
188 188
     }
189 189
 
190 190
     public static function filter($filters): Router
Please login to merge, or discard this patch.
Spacing   +35 added lines, -35 removed lines patch added patch discarded remove patch
@@ -4,7 +4,7 @@  discard block
 block discarded – undo
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
 block discarded – undo
59 59
         return self::getInstance()->set($uri, $controll, $protocol);
60 60
     }
61 61
 
62
-    public function set($url , $role , $protocol = null): Router
62
+    public function set($url, $role, $protocol = null): 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
     	}
@@ -82,9 +82,9 @@  discard block
 block discarded – undo
82 82
         return self::getInstance();
83 83
     }
84 84
 
85
-    public static function group(string $prefix,$callback): Router
85
+    public static function group(string $prefix, $callback): Router
86 86
     {
87
-        self::getInstance()->prefix = (substr($prefix,0,1) !== '/') ? "/{$prefix}" : $prefix;
87
+        self::getInstance()->prefix = (substr($prefix, 0, 1) !== '/') ? "/{$prefix}" : $prefix;
88 88
         self::getInstance()->group = sha1(date('d/m/Y h:m:i'));
89 89
         $callback();
90 90
         self::getInstance()->group = null;
@@ -96,14 +96,14 @@  discard block
 block discarded – undo
96 96
     public static function name(string $name): Router
97 97
     {
98 98
 
99
-        if(self::getInstance()->lastReturn){
99
+        if (self::getInstance()->lastReturn) {
100 100
             throw new Exception("There is no reason to call a {$name} route group.");
101 101
         }
102 102
 
103 103
         $currentRoute = end(self::getInstance()->routers);
104 104
 
105
-        foreach(self::getInstance()->routers as $key => $value){
106
-            if(array_key_exists($name, self::getInstance()->routers)){
105
+        foreach (self::getInstance()->routers as $key => $value) {
106
+            if (array_key_exists($name, self::getInstance()->routers)) {
107 107
                 throw new Exception("There is already a route with the name {$name} configured.");
108 108
             }
109 109
         }
@@ -135,19 +135,19 @@  discard block
 block discarded – undo
135 135
 
136 136
     public function dispatch(?string $route_name = null): bool
137 137
     {
138
-        if(!is_null($route_name)){
138
+        if (!is_null($route_name)) {
139 139
             return $this->byName($route_name);
140 140
         }
141 141
 
142 142
 		$currentProtocol = $this->getProtocol();
143 143
 
144
-        foreach(array_reverse($this->routers) as $r => $route){
144
+        foreach (array_reverse($this->routers) as $r => $route) {
145 145
 
146 146
             $this->hasProtocol($route, $currentProtocol);
147 147
 
148 148
 	        $route_loop = explode(
149 149
                 '/',
150
-                (substr($route['url'],strlen($route['url'])-1,1) === '/') 
150
+                (substr($route['url'], strlen($route['url'])-1, 1) === '/') 
151 151
                     ? substr($route['url'], 0, -1) 
152 152
                     : $route['url'] 
153 153
             );
@@ -161,19 +161,19 @@  discard block
 block discarded – undo
161 161
                 : $_SERVER['REQUEST_URI'] 
162 162
             );*/
163 163
 
164
-	        if(count($route_loop) !== count($route_request)){
164
+	        if (count($route_loop) !== count($route_request)) {
165 165
                 continue;
166 166
             }
167 167
 
168
-	        for($rr = 0; $rr < count($route_loop); $rr++){
169
-	            $param = (substr($route_loop[$rr],0,1)==='{');
168
+	        for ($rr = 0; $rr < count($route_loop); $rr++) {
169
+	            $param = (substr($route_loop[$rr], 0, 1) === '{');
170 170
 
171
-	            if($param){
172
-                    $param_name = substr($route_loop[$rr],1,strlen($route_loop[$rr])-2);
171
+	            if ($param) {
172
+                    $param_name = substr($route_loop[$rr], 1, strlen($route_loop[$rr])-2);
173 173
 	                $data[$param_name] = $route_request[$rr];
174 174
 	            }
175 175
 
176
-	            if(!$param and $route_loop[$rr] !== $route_request[$rr]){
176
+	            if (!$param and $route_loop[$rr] !== $route_request[$rr]) {
177 177
                     continue 2;
178 178
                 }
179 179
             }
@@ -184,25 +184,25 @@  discard block
 block discarded – undo
184 184
 	        return true;
185 185
 	    }
186 186
 
187
-	    throw new Exception('Page not found.',404);
187
+	    throw new Exception('Page not found.', 404);
188 188
     }
189 189
 
190 190
     public static function filter($filters): Router
191 191
     {
192
-        if(self::getInstance()->lastReturn !== null){
192
+        if (self::getInstance()->lastReturn !== null) {
193 193
             $currentGroup = end(self::getInstance()->routers)['group'];
194 194
 
195 195
             foreach (self::getInstance()->routers as $key => $value) {
196 196
 
197
-                if($value['group'] === $currentGroup){
198
-                    $currentRoute = self::getInstance()->addFilter(self::getInstance()->routers[$key],$filters);
197
+                if ($value['group'] === $currentGroup) {
198
+                    $currentRoute = self::getInstance()->addFilter(self::getInstance()->routers[$key], $filters);
199 199
                     self::getInstance()->routers[$key] = $currentRoute;
200 200
                 }
201 201
 
202 202
             }
203 203
             
204
-        }else{
205
-            self::getInstance()->routers[count(self::getInstance()->routers)-1] = self::getInstance()->addFilter(end(self::getInstance()->routers),$filters);
204
+        }else {
205
+            self::getInstance()->routers[count(self::getInstance()->routers)-1] = self::getInstance()->addFilter(end(self::getInstance()->routers), $filters);
206 206
         }
207 207
 
208 208
         return self::getInstance();
@@ -210,18 +210,18 @@  discard block
 block discarded – undo
210 210
 
211 211
     public static function addFilter(array $route, $filter): array
212 212
     {
213
-        if(is_null($route['filters'])){
213
+        if (is_null($route['filters'])) {
214 214
             $route['filters'] = $filter;
215 215
             return $route;
216 216
         }
217 217
 
218 218
         $filters = (is_array($filter)) ? $filter : [0 => $filter];
219 219
 
220
-        if(is_array($route['filters'])){
220
+        if (is_array($route['filters'])) {
221 221
             foreach ($route['filters'] as $key => $value) {
222 222
                 $filters[] = $value;
223 223
             }
224
-        }else{
224
+        }else {
225 225
             $filters[] = $route['filters'];
226 226
         }
227 227
 
@@ -234,28 +234,28 @@  discard block
 block discarded – undo
234 234
         $data = $this->getData();
235 235
 
236 236
         foreach ($data['GET'] as $name => $value) {
237
-            $controll = str_replace('{'.$name.'}',$value,$controll);
237
+            $controll = str_replace('{'.$name.'}', $value, $controll);
238 238
         }
239 239
 
240
-        $d = explode(':',$controll);
240
+        $d = explode(':', $controll);
241 241
 
242
-        if(count($d) != 2){
242
+        if (count($d) != 2) {
243 243
             throw new Exception("Controller {$controll} badly set.");
244 244
         }
245 245
 
246
-        if(!class_exists('Controllers\\' . ucfirst($d[0]))){
246
+        if (!class_exists('Controllers\\'.ucfirst($d[0]))) {
247 247
             throw new Exception("No controller {$d[0]} found.");
248 248
         }
249 249
 
250
-        if(!method_exists('Controllers\\' . ucfirst($d[0]), $d[1])){
250
+        if (!method_exists('Controllers\\'.ucfirst($d[0]), $d[1])) {
251 251
             throw new Exception("No method {$d[1]} found for {$d[0]}.");
252 252
         }
253 253
 
254
-        $controller = 'Controllers\\' . ucfirst($d[0]);
254
+        $controller = 'Controllers\\'.ucfirst($d[0]);
255 255
         $controller = new $controller();
256 256
         $method = $d[1];
257 257
 
258
-        if( ( $this->getProtocol() == 'form') ){
258
+        if (($this->getProtocol() == 'form')) {
259 259
             $this->ControllerForm($controller, $method, $data['POST']);
260 260
         }else {
261 261
             $controller->$method($data);
Please login to merge, or discard this patch.
Braces   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -201,7 +201,7 @@  discard block
 block discarded – undo
201 201
 
202 202
             }
203 203
             
204
-        }else{
204
+        } else{
205 205
             self::getInstance()->routers[count(self::getInstance()->routers)-1] = self::getInstance()->addFilter(end(self::getInstance()->routers),$filters);
206 206
         }
207 207
 
@@ -221,7 +221,7 @@  discard block
 block discarded – undo
221 221
             foreach ($route['filters'] as $key => $value) {
222 222
                 $filters[] = $value;
223 223
             }
224
-        }else{
224
+        } else{
225 225
             $filters[] = $route['filters'];
226 226
         }
227 227
 
@@ -257,7 +257,7 @@  discard block
 block discarded – undo
257 257
 
258 258
         if( ( $this->getProtocol() == 'form') ){
259 259
             $this->ControllerForm($controller, $method, $data['POST']);
260
-        }else {
260
+        } else {
261 261
             $controller->$method($data);
262 262
         }
263 263
     }    
Please login to merge, or discard this patch.
src/CheckTrait.php 1 patch
Spacing   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -4,27 +4,27 @@  discard block
 block discarded – undo
4 4
 
5 5
 use Exception;
6 6
 
7
-trait CheckTrait{
7
+trait CheckTrait {
8 8
 
9 9
     protected function check_protocol(string $expected, string $current)
10 10
     {
11
-        if($expected !== $current){
12
-            throw new Exception('Page not found.',404);
11
+        if ($expected !== $current) {
12
+            throw new Exception('Page not found.', 404);
13 13
         }
14 14
     }
15 15
 
16
-    protected function check_name(string $route_name){
17
-        if(!array_key_exists($route_name,$this->routers)){
18
-            throw new Exception('Page not found.',404);
16
+    protected function check_name(string $route_name) {
17
+        if (!array_key_exists($route_name, $this->routers)) {
18
+            throw new Exception('Page not found.', 404);
19 19
         }
20 20
     }
21 21
 
22 22
     protected function check_filtering(array $route)
23 23
     {
24
-        $filters = (is_array($route['filters'])) ? $route['filters'] : [ $route['filters'] ];
24
+        $filters = (is_array($route['filters'])) ? $route['filters'] : [$route['filters']];
25 25
 
26
-        foreach($filters as $filter){
27
-            if(is_null($filter)){
26
+        foreach ($filters as $filter) {
27
+            if (is_null($filter)) {
28 28
                 continue;
29 29
             }
30 30
             $this->filter->filtering($filter);
@@ -33,24 +33,24 @@  discard block
 block discarded – undo
33 33
 
34 34
     protected function check_config()
35 35
     {
36
-        if(!defined('ROUTER_CONFIG')){
36
+        if (!defined('ROUTER_CONFIG')) {
37 37
             throw new Exception("Information for loading routes has not been defined.");
38 38
         }
39 39
     }
40 40
 
41 41
     protected function check_role()
42 42
     {
43
-        if(!array_key_exists('role', $this->getData()['POST'])){
43
+        if (!array_key_exists('role', $this->getData()['POST'])) {
44 44
             throw new Exception('O servidor não conseguiu identificar a finalidade deste formulário.');
45 45
         }
46 46
     }
47 47
 
48 48
     protected function hasProtocol(array $route, string $currentProtocol)
49 49
     {
50
-        $protocols = ( is_array($route['protocol']) ) ? $route['protocol'] : [ $route['protocol'] ];
50
+        $protocols = (is_array($route['protocol'])) ? $route['protocol'] : [$route['protocol']];
51 51
 
52
-        foreach($protocols as $protocol){
53
-            if($protocol !== $currentProtocol){
52
+        foreach ($protocols as $protocol) {
53
+            if ($protocol !== $currentProtocol) {
54 54
                 parent::continue;
55 55
             }
56 56
         }
Please login to merge, or discard this patch.
src/Helper.php 2 patches
Indentation   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -40,7 +40,7 @@
 block discarded – undo
40 40
     }
41 41
 
42 42
     protected function ControllerForm($controller, string $method, array $values){
43
-		if(Validator::execute($values)){
43
+        if(Validator::execute($values)){
44 44
 
45 45
             $this->check_role();
46 46
 
Please login to merge, or discard this patch.
Spacing   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -4,17 +4,17 @@  discard block
 block discarded – undo
4 4
 
5 5
 use HnrAzevedo\Validator\Validator;
6 6
 
7
-trait Helper{
7
+trait Helper {
8 8
     use CheckTrait;
9 9
     
10 10
     protected function getProtocol(): string
11 11
     {
12
-        if((isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')){
12
+        if ((isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')) {
13 13
             return 'ajax';
14 14
         }
15 15
         
16 16
         /* ONLY FOR DEBUG CONDITION */
17
-        if(!array_key_exists('REQUEST_METHOD',$_SERVER)){
17
+        if (!array_key_exists('REQUEST_METHOD', $_SERVER)) {
18 18
             return 'get';
19 19
         }
20 20
 
@@ -33,14 +33,14 @@  discard block
 block discarded – undo
33 33
     protected function import(string $path)
34 34
     {
35 35
         foreach (scandir($path) as $routeFile) {
36
-            if(pathinfo($path.DIRECTORY_SEPARATOR.$routeFile, PATHINFO_EXTENSION) === 'php'){
37
-                require_once($path. DIRECTORY_SEPARATOR .$routeFile);
36
+            if (pathinfo($path.DIRECTORY_SEPARATOR.$routeFile, PATHINFO_EXTENSION) === 'php') {
37
+                require_once($path.DIRECTORY_SEPARATOR.$routeFile);
38 38
             }
39 39
         }
40 40
     }
41 41
 
42
-    protected function ControllerForm($controller, string $method, array $values){
43
-		if(Validator::execute($values)){
42
+    protected function ControllerForm($controller, string $method, array $values) {
43
+		if (Validator::execute($values)) {
44 44
 
45 45
             $this->check_role();
46 46
 
Please login to merge, or discard this patch.