Passed
Branch master (0fcf32)
by Henri
01:14
created
src/Helper.php 3 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   +11 added lines, -11 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, ControllerTrait;
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
 
@@ -55,7 +55,7 @@  discard block
 block discarded – undo
55 55
         $data = $this->getData();
56 56
 
57 57
         foreach ($data['GET'] as $name => $value) {
58
-            $controll = str_replace('{'.$name.'}',$value,$controll);
58
+            $controll = str_replace('{'.$name.'}', $value, $controll);
59 59
         }
60 60
 
61 61
         $this->check_controllsettable($controll);
@@ -64,11 +64,11 @@  discard block
 block discarded – undo
64 64
 
65 65
         $this->check_controllmethod($controll);
66 66
 
67
-        $controller = 'Controllers\\' . ucfirst(explode(':',$controll)[0]);
67
+        $controller = 'Controllers\\'.ucfirst(explode(':', $controll)[0]);
68 68
         $controller = new $controller();
69
-        $method = explode(':',$controll)[1];
69
+        $method = explode(':', $controll)[1];
70 70
 
71
-        if( ( $this->getProtocol() == 'form') ){
71
+        if (($this->getProtocol() == 'form')) {
72 72
             $this->ControllerForm($controller, $method, $data['POST']);
73 73
         }else {
74 74
             $controller->$method($data);
Please login to merge, or discard this patch.
Braces   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -70,7 +70,7 @@
 block discarded – undo
70 70
 
71 71
         if( ( $this->getProtocol() == 'form') ){
72 72
             $this->ControllerForm($controller, $method, $data['POST']);
73
-        }else {
73
+        } else {
74 74
             $controller->$method($data);
75 75
         }
76 76
     }    
Please login to merge, or discard this patch.
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/Router.php 3 patches
Braces   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -217,7 +217,7 @@
 block discarded – undo
217 217
             foreach ($route['filters'] as $key => $value) {
218 218
                 $filters[] = $value;
219 219
             }
220
-        }else{
220
+        } else{
221 221
             $filters[] = $route['filters'];
222 222
         }
223 223
 
Please login to merge, or discard this patch.
Indentation   +17 added lines, -17 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
     }
@@ -140,13 +140,13 @@  discard block
 block discarded – undo
140 140
     {
141 141
         $this->byName($route_name);
142 142
 
143
-		$currentProtocol = $this->getProtocol();
143
+        $currentProtocol = $this->getProtocol();
144 144
 
145 145
         foreach(array_reverse($this->routers) as $r => $route){
146 146
 
147 147
             $this->hasProtocol($route, $currentProtocol);
148 148
 
149
-	        $route_loop = explode(
149
+            $route_loop = explode(
150 150
                 '/',
151 151
                 (substr($route['url'],strlen($route['url'])-1,1) === '/') 
152 152
                     ? substr($route['url'], 0, -1) 
@@ -160,17 +160,17 @@  discard block
 block discarded – undo
160 160
                 : $_SERVER['REQUEST_URI'] 
161 161
             );
162 162
 
163
-	        if($this->check_numparams($route_loop, $route_request) || $this->check_parameters($route_loop, $route_request)){
163
+            if($this->check_numparams($route_loop, $route_request) || $this->check_parameters($route_loop, $route_request)){
164 164
                 continue;
165 165
             }
166 166
             
167 167
             $this->check_filtering($route);
168 168
 
169 169
             $this->Controller($route['role']);
170
-	        return true;
171
-	    }
170
+            return true;
171
+        }
172 172
 
173
-	    throw new Exception('Page not found.',404);
173
+        throw new Exception('Page not found.',404);
174 174
     }
175 175
 
176 176
     public static function filter($filters): Router
Please login to merge, or discard this patch.
Spacing   +23 added lines, -23 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
         }
@@ -119,7 +119,7 @@  discard block
 block discarded – undo
119 119
 
120 120
     private function byName(?string $route_name)
121 121
     {
122
-        if(!is_null($route_name)){
122
+        if (!is_null($route_name)) {
123 123
             $currentProtocol = $this->getProtocol();
124 124
 
125 125
             $this->check_name($route_name);
@@ -142,25 +142,25 @@  discard block
 block discarded – undo
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 149
 	        $route_loop = explode(
150 150
                 '/',
151
-                (substr($route['url'],strlen($route['url'])-1,1) === '/') 
151
+                (substr($route['url'], strlen($route['url'])-1, 1) === '/') 
152 152
                     ? substr($route['url'], 0, -1) 
153 153
                     : $route['url'] 
154 154
             );
155 155
 
156 156
             $route_request = explode(
157 157
                 '/',
158
-                (substr($_SERVER['REQUEST_URI'],strlen($_SERVER['REQUEST_URI'])-1,1) === '/') 
158
+                (substr($_SERVER['REQUEST_URI'], strlen($_SERVER['REQUEST_URI'])-1, 1) === '/') 
159 159
                 ? substr($_SERVER['REQUEST_URI'], 0, -1) 
160 160
                 : $_SERVER['REQUEST_URI'] 
161 161
             );
162 162
 
163
-	        if($this->check_numparams($route_loop, $route_request) || $this->check_parameters($route_loop, $route_request)){
163
+	        if ($this->check_numparams($route_loop, $route_request) || $this->check_parameters($route_loop, $route_request)) {
164 164
                 continue;
165 165
             }
166 166
             
@@ -170,18 +170,18 @@  discard block
 block discarded – undo
170 170
 	        return true;
171 171
 	    }
172 172
 
173
-	    throw new Exception('Page not found.',404);
173
+	    throw new Exception('Page not found.', 404);
174 174
     }
175 175
 
176 176
     public static function filter($filters): Router
177 177
     {
178
-        if(self::getInstance()->lastReturn !== null){
178
+        if (self::getInstance()->lastReturn !== null) {
179 179
             $currentGroup = end(self::getInstance()->routers)['group'];
180 180
 
181 181
             foreach (self::getInstance()->routers as $key => $value) {
182 182
 
183
-                if($value['group'] === $currentGroup){
184
-                    $currentRoute = self::getInstance()->addFilter(self::getInstance()->routers[$key],$filters);
183
+                if ($value['group'] === $currentGroup) {
184
+                    $currentRoute = self::getInstance()->addFilter(self::getInstance()->routers[$key], $filters);
185 185
                     self::getInstance()->routers[$key] = $currentRoute;
186 186
                 }
187 187
 
@@ -190,24 +190,24 @@  discard block
 block discarded – undo
190 190
             return self::getInstance();
191 191
         }
192 192
         
193
-        self::getInstance()->routers[count(self::getInstance()->routers)-1] = self::getInstance()->addFilter(end(self::getInstance()->routers),$filters);
193
+        self::getInstance()->routers[count(self::getInstance()->routers)-1] = self::getInstance()->addFilter(end(self::getInstance()->routers), $filters);
194 194
         return self::getInstance();
195 195
     }
196 196
 
197 197
     public static function addFilter(array $route, $filter): array
198 198
     {
199
-        if(is_null($route['filters'])){
199
+        if (is_null($route['filters'])) {
200 200
             $route['filters'] = $filter;
201 201
             return $route;
202 202
         }
203 203
 
204 204
         $filters = (is_array($filter)) ? $filter : [0 => $filter];
205 205
 
206
-        if(is_array($route['filters'])){
206
+        if (is_array($route['filters'])) {
207 207
             foreach ($route['filters'] as $key => $value) {
208 208
                 $filters[] = $value;
209 209
             }
210
-        }else{
210
+        }else {
211 211
             $filters[] = $route['filters'];
212 212
         }
213 213
 
Please login to merge, or discard this patch.
src/ControllerTrait.php 1 patch
Spacing   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -4,26 +4,26 @@
 block discarded – undo
4 4
 
5 5
 use Exception;
6 6
 
7
-trait ControllerTrait{
7
+trait ControllerTrait {
8 8
 
9 9
     protected function check_controllsettable(string $controll)
10 10
     {
11
-        if(count(explode(':',$controll)) != 2){
11
+        if (count(explode(':', $controll)) != 2) {
12 12
             throw new Exception("Controller {$controll} badly set.");
13 13
         }
14 14
     }
15 15
 
16 16
     protected function check_controllexist(string $controll)
17 17
     {
18
-        if(!class_exists('Controllers\\' . ucfirst(explode(':',$controll)[0]))){
19
-            throw new Exception("No controller ".explode(':',$controll)[0]." found.");
18
+        if (!class_exists('Controllers\\'.ucfirst(explode(':', $controll)[0]))) {
19
+            throw new Exception("No controller ".explode(':', $controll)[0]." found.");
20 20
         }
21 21
     }
22 22
 
23 23
     protected function check_controllmethod(string $controll)
24 24
     {
25
-        if(!method_exists('Controllers\\' . ucfirst(explode(':',$controll)[0]), explode(':',$controll)[1])){
26
-            throw new Exception("No method ".explode(':',$controll)[1]." found for ".explode(':',$controll)[0].".");
25
+        if (!method_exists('Controllers\\'.ucfirst(explode(':', $controll)[0]), explode(':', $controll)[1])) {
26
+            throw new Exception("No method ".explode(':', $controll)[1]." found for ".explode(':', $controll)[0].".");
27 27
         }
28 28
     }
29 29
 
Please login to merge, or discard this patch.
src/CheckTrait.php 1 patch
Spacing   +19 added lines, -19 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,12 +45,12 @@  discard block
 block discarded – undo
45 45
 
46 46
     protected function check_parameters(array $route_loop, array $route_request)
47 47
     {
48
-        foreach($route_loop as $rr => $param){
49
-            if( (substr($param,0,1) === '{') ){
50
-                $data[ substr($param,1,strlen($param)-2) ] = $route_request[$rr];    
48
+        foreach ($route_loop as $rr => $param) {
49
+            if ((substr($param, 0, 1) === '{')) {
50
+                $data[substr($param, 1, strlen($param)-2)] = $route_request[$rr];    
51 51
             }
52 52
     
53
-            if($this->check_parameter($param, $route_request[$rr])){
53
+            if ($this->check_parameter($param, $route_request[$rr])) {
54 54
                 return false;
55 55
             }
56 56
         }
@@ -58,22 +58,22 @@  discard block
 block discarded – undo
58 58
 
59 59
     protected function check_parameter(string $route_loop, string $route_request)
60 60
     {
61
-        return !( substr($route_loop,0,1) === '{' ) and $route_loop !== $route_request;
61
+        return !(substr($route_loop, 0, 1) === '{') and $route_loop !== $route_request;
62 62
     }
63 63
 
64 64
     protected function check_role()
65 65
     {
66
-        if(!array_key_exists('role', $this->getData()['POST'])){
66
+        if (!array_key_exists('role', $this->getData()['POST'])) {
67 67
             throw new Exception('O servidor não conseguiu identificar a finalidade deste formulário.');
68 68
         }
69 69
     }
70 70
 
71 71
     protected function hasProtocol(array $route, string $currentProtocol)
72 72
     {
73
-        $protocols = ( is_array($route['protocol']) ) ? $route['protocol'] : [ $route['protocol'] ];
73
+        $protocols = (is_array($route['protocol'])) ? $route['protocol'] : [$route['protocol']];
74 74
 
75
-        foreach($protocols as $protocol){
76
-            if($protocol !== $currentProtocol){
75
+        foreach ($protocols as $protocol) {
76
+            if ($protocol !== $currentProtocol) {
77 77
                 continue;
78 78
             }
79 79
         }
Please login to merge, or discard this patch.