Passed
Push — master ( eeb79a...bf2098 )
by Henri
01:16
created
examples/index.php 2 patches
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -7,11 +7,11 @@
 block discarded – undo
7 7
 
8 8
 /* NOTE: in case of error an exception is thrown */
9 9
 
10
-try{
10
+try {
11 11
     
12 12
     Router::create()->dispatch();
13 13
 
14
-}catch(Exception $er){
14
+}catch (Exception $er) {
15 15
 
16 16
     die("Code Error: {$er->getCode()}, Line: {$er->getLine()}, File: {$er->getFile()}, Message: {$er->getMessage()}.");
17 17
 
Please login to merge, or discard this patch.
Braces   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -11,7 +11,7 @@
 block discarded – undo
11 11
     
12 12
     Router::create()->dispatch();
13 13
 
14
-}catch(Exception $er){
14
+} catch(Exception $er){
15 15
 
16 16
     die("Code Error: {$er->getCode()}, Line: {$er->getLine()}, File: {$er->getFile()}, Message: {$er->getMessage()}.");
17 17
 
Please login to merge, or discard this patch.
examples/Routes/default.php 1 patch
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -2,6 +2,6 @@
 block discarded – undo
2 2
 
3 3
 use HnrAzevedo\Router\Router;
4 4
 
5
-Router::get('/','Application:index');
5
+Router::get('/', 'Application:index');
6 6
 
7
-Router::get('/{parameter}/{teste}','Params:index');
8 7
\ No newline at end of file
8
+Router::get('/{parameter}/{teste}', 'Params:index');
9 9
\ No newline at end of file
Please login to merge, or discard this patch.
src/CheckTrait.php 1 patch
Spacing   +15 added lines, -15 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,7 +33,7 @@  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
     }
@@ -45,22 +45,22 @@  discard block
 block discarded – undo
45 45
 
46 46
     protected function check_parameter(string $route_loop, string $route_request)
47 47
     {
48
-        return !( substr($route_loop,0,1) === '{' ) and $route_loop !== $route_request;
48
+        return !(substr($route_loop, 0, 1) === '{') and $route_loop !== $route_request;
49 49
     }
50 50
 
51 51
     protected function check_role()
52 52
     {
53
-        if(!array_key_exists('role', $this->getData()['POST'])){
53
+        if (!array_key_exists('role', $this->getData()['POST'])) {
54 54
             throw new Exception('O servidor não conseguiu identificar a finalidade deste formulário.');
55 55
         }
56 56
     }
57 57
 
58 58
     protected function hasProtocol(array $route, string $currentProtocol)
59 59
     {
60
-        $protocols = ( is_array($route['protocol']) ) ? $route['protocol'] : [ $route['protocol'] ];
60
+        $protocols = (is_array($route['protocol'])) ? $route['protocol'] : [$route['protocol']];
61 61
 
62
-        foreach($protocols as $protocol){
63
-            if($protocol !== $currentProtocol){
62
+        foreach ($protocols as $protocol) {
63
+            if ($protocol !== $currentProtocol) {
64 64
                 continue;
65 65
             }
66 66
         }
Please login to merge, or discard this patch.
src/Router.php 2 patches
Indentation   +20 added lines, -20 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) 
@@ -159,17 +159,17 @@  discard block
 block discarded – undo
159 159
                 : $_SERVER['REQUEST_URI'] 
160 160
             );
161 161
 
162
-	        if($this->check_numparams($route_loop, $route_request)){
162
+            if($this->check_numparams($route_loop, $route_request)){
163 163
                 continue;
164 164
             }
165 165
 
166
-	        for($rr = 0; $rr < count($route_loop); $rr++){
166
+            for($rr = 0; $rr < count($route_loop); $rr++){
167 167
 
168
-	            if( (substr($route_loop[$rr],0,1) === '{') ){
168
+                if( (substr($route_loop[$rr],0,1) === '{') ){
169 169
                     $data[ substr($route_loop[$rr],1,strlen($route_loop[$rr])-2) ] = $route_request[$rr];    
170 170
                 }
171 171
 
172
-	            if($this->check_parameter($route_loop[$rr], $route_request[$rr])){
172
+                if($this->check_parameter($route_loop[$rr], $route_request[$rr])){
173 173
                     continue 2;
174 174
                 }
175 175
 
@@ -178,10 +178,10 @@  discard block
 block discarded – undo
178 178
             $this->check_filtering($route);
179 179
 
180 180
             $this->Controller($route['role']);
181
-	        return true;
182
-	    }
181
+            return true;
182
+        }
183 183
 
184
-	    throw new Exception('Page not found.',404);
184
+        throw new Exception('Page not found.',404);
185 185
     }
186 186
 
187 187
     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,41 +135,41 @@  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
             );
154 154
 
155 155
             $route_request = explode(
156 156
                 '/',
157
-                (substr($_SERVER['REQUEST_URI'],strlen($_SERVER['REQUEST_URI'])-1,1) === '/') 
157
+                (substr($_SERVER['REQUEST_URI'], strlen($_SERVER['REQUEST_URI'])-1, 1) === '/') 
158 158
                 ? substr($_SERVER['REQUEST_URI'], 0, -1) 
159 159
                 : $_SERVER['REQUEST_URI'] 
160 160
             );
161 161
 
162
-	        if($this->check_numparams($route_loop, $route_request)){
162
+	        if ($this->check_numparams($route_loop, $route_request)) {
163 163
                 continue;
164 164
             }
165 165
 
166
-	        for($rr = 0; $rr < count($route_loop); $rr++){
166
+	        for ($rr = 0; $rr < count($route_loop); $rr++) {
167 167
 
168
-	            if( (substr($route_loop[$rr],0,1) === '{') ){
169
-                    $data[ substr($route_loop[$rr],1,strlen($route_loop[$rr])-2) ] = $route_request[$rr];    
168
+	            if ((substr($route_loop[$rr], 0, 1) === '{')) {
169
+                    $data[substr($route_loop[$rr], 1, strlen($route_loop[$rr])-2)] = $route_request[$rr];    
170 170
                 }
171 171
 
172
-	            if($this->check_parameter($route_loop[$rr], $route_request[$rr])){
172
+	            if ($this->check_parameter($route_loop[$rr], $route_request[$rr])) {
173 173
                     continue 2;
174 174
                 }
175 175
 
@@ -181,25 +181,25 @@  discard block
 block discarded – undo
181 181
 	        return true;
182 182
 	    }
183 183
 
184
-	    throw new Exception('Page not found.',404);
184
+	    throw new Exception('Page not found.', 404);
185 185
     }
186 186
 
187 187
     public static function filter($filters): Router
188 188
     {
189
-        if(self::getInstance()->lastReturn !== null){
189
+        if (self::getInstance()->lastReturn !== null) {
190 190
             $currentGroup = end(self::getInstance()->routers)['group'];
191 191
 
192 192
             foreach (self::getInstance()->routers as $key => $value) {
193 193
 
194
-                if($value['group'] === $currentGroup){
195
-                    $currentRoute = self::getInstance()->addFilter(self::getInstance()->routers[$key],$filters);
194
+                if ($value['group'] === $currentGroup) {
195
+                    $currentRoute = self::getInstance()->addFilter(self::getInstance()->routers[$key], $filters);
196 196
                     self::getInstance()->routers[$key] = $currentRoute;
197 197
                 }
198 198
 
199 199
             }
200 200
             
201
-        }else{
202
-            self::getInstance()->routers[count(self::getInstance()->routers)-1] = self::getInstance()->addFilter(end(self::getInstance()->routers),$filters);
201
+        }else {
202
+            self::getInstance()->routers[count(self::getInstance()->routers)-1] = self::getInstance()->addFilter(end(self::getInstance()->routers), $filters);
203 203
         }
204 204
 
205 205
         return self::getInstance();
@@ -207,18 +207,18 @@  discard block
 block discarded – undo
207 207
 
208 208
     public static function addFilter(array $route, $filter): array
209 209
     {
210
-        if(is_null($route['filters'])){
210
+        if (is_null($route['filters'])) {
211 211
             $route['filters'] = $filter;
212 212
             return $route;
213 213
         }
214 214
 
215 215
         $filters = (is_array($filter)) ? $filter : [0 => $filter];
216 216
 
217
-        if(is_array($route['filters'])){
217
+        if (is_array($route['filters'])) {
218 218
             foreach ($route['filters'] as $key => $value) {
219 219
                 $filters[] = $value;
220 220
             }
221
-        }else{
221
+        }else {
222 222
             $filters[] = $route['filters'];
223 223
         }
224 224
 
@@ -231,28 +231,28 @@  discard block
 block discarded – undo
231 231
         $data = $this->getData();
232 232
 
233 233
         foreach ($data['GET'] as $name => $value) {
234
-            $controll = str_replace('{'.$name.'}',$value,$controll);
234
+            $controll = str_replace('{'.$name.'}', $value, $controll);
235 235
         }
236 236
 
237
-        $d = explode(':',$controll);
237
+        $d = explode(':', $controll);
238 238
 
239
-        if(count($d) != 2){
239
+        if (count($d) != 2) {
240 240
             throw new Exception("Controller {$controll} badly set.");
241 241
         }
242 242
 
243
-        if(!class_exists('Controllers\\' . ucfirst($d[0]))){
243
+        if (!class_exists('Controllers\\'.ucfirst($d[0]))) {
244 244
             throw new Exception("No controller {$d[0]} found.");
245 245
         }
246 246
 
247
-        if(!method_exists('Controllers\\' . ucfirst($d[0]), $d[1])){
247
+        if (!method_exists('Controllers\\'.ucfirst($d[0]), $d[1])) {
248 248
             throw new Exception("No method {$d[1]} found for {$d[0]}.");
249 249
         }
250 250
 
251
-        $controller = 'Controllers\\' . ucfirst($d[0]);
251
+        $controller = 'Controllers\\'.ucfirst($d[0]);
252 252
         $controller = new $controller();
253 253
         $method = $d[1];
254 254
 
255
-        if( ( $this->getProtocol() == 'form') ){
255
+        if (($this->getProtocol() == 'form')) {
256 256
             $this->ControllerForm($controller, $method, $data['POST']);
257 257
         }else {
258 258
             $controller->$method($data);
Please login to merge, or discard this patch.