Passed
Push — master ( bf2098...3f372a )
by Henri
01:15
created
src/Helper.php 2 patches
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.
src/Router.php 3 patches
Indentation   +18 added lines, -18 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,7 +160,7 @@  discard block
 block discarded – undo
160 160
                 : $_SERVER['REQUEST_URI'] 
161 161
             );
162 162
 
163
-	        if($this->check_numparams($route_loop, $route_request)){
163
+            if($this->check_numparams($route_loop, $route_request)){
164 164
                 continue;
165 165
             }
166 166
 
@@ -169,7 +169,7 @@  discard block
 block discarded – undo
169 169
                 //    $data[ substr($param,1,strlen($param)-2) ] = $route_request[$rr];    
170 170
                 //}
171 171
 
172
-	            if($this->check_parameter($param, $route_request[$rr])){
172
+                if($this->check_parameter($param, $route_request[$rr])){
173 173
                     continue 2;
174 174
                 }
175 175
             }
@@ -177,10 +177,10 @@  discard block
 block discarded – undo
177 177
             $this->check_filtering($route);
178 178
 
179 179
             $this->Controller($route['role']);
180
-	        return true;
181
-	    }
180
+            return true;
181
+        }
182 182
 
183
-	    throw new Exception('Page not found.',404);
183
+        throw new Exception('Page not found.',404);
184 184
     }
185 185
 
186 186
     public static function filter($filters): Router
Please login to merge, or discard this patch.
Spacing   +25 added lines, -25 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,34 +142,34 @@  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)){
163
+	        if ($this->check_numparams($route_loop, $route_request)) {
164 164
                 continue;
165 165
             }
166 166
 
167
-            foreach($route_loop as $rr => $param){
167
+            foreach ($route_loop as $rr => $param) {
168 168
                 //if( (substr($param,0,1) === '{') ){
169 169
                 //    $data[ substr($param,1,strlen($param)-2) ] = $route_request[$rr];    
170 170
                 //}
171 171
 
172
-	            if($this->check_parameter($param, $route_request[$rr])){
172
+	            if ($this->check_parameter($param, $route_request[$rr])) {
173 173
                     continue 2;
174 174
                 }
175 175
             }
@@ -180,18 +180,18 @@  discard block
 block discarded – undo
180 180
 	        return true;
181 181
 	    }
182 182
 
183
-	    throw new Exception('Page not found.',404);
183
+	    throw new Exception('Page not found.', 404);
184 184
     }
185 185
 
186 186
     public static function filter($filters): Router
187 187
     {
188
-        if(self::getInstance()->lastReturn !== null){
188
+        if (self::getInstance()->lastReturn !== null) {
189 189
             $currentGroup = end(self::getInstance()->routers)['group'];
190 190
 
191 191
             foreach (self::getInstance()->routers as $key => $value) {
192 192
 
193
-                if($value['group'] === $currentGroup){
194
-                    $currentRoute = self::getInstance()->addFilter(self::getInstance()->routers[$key],$filters);
193
+                if ($value['group'] === $currentGroup) {
194
+                    $currentRoute = self::getInstance()->addFilter(self::getInstance()->routers[$key], $filters);
195 195
                     self::getInstance()->routers[$key] = $currentRoute;
196 196
                 }
197 197
 
@@ -200,24 +200,24 @@  discard block
 block discarded – undo
200 200
             return self::getInstance();
201 201
         }
202 202
         
203
-        self::getInstance()->routers[count(self::getInstance()->routers)-1] = self::getInstance()->addFilter(end(self::getInstance()->routers),$filters);
203
+        self::getInstance()->routers[count(self::getInstance()->routers)-1] = self::getInstance()->addFilter(end(self::getInstance()->routers), $filters);
204 204
         return self::getInstance();
205 205
     }
206 206
 
207 207
     public static function addFilter(array $route, $filter): array
208 208
     {
209
-        if(is_null($route['filters'])){
209
+        if (is_null($route['filters'])) {
210 210
             $route['filters'] = $filter;
211 211
             return $route;
212 212
         }
213 213
 
214 214
         $filters = (is_array($filter)) ? $filter : [0 => $filter];
215 215
 
216
-        if(is_array($route['filters'])){
216
+        if (is_array($route['filters'])) {
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.
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.
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.