Passed
Branch master (8139c8)
by Henri
07:53 queued 06:32
created
src/FilterTrait.php 1 patch
Spacing   +9 added lines, -9 removed lines patch added patch discarded remove patch
@@ -4,13 +4,13 @@  discard block
 block discarded – undo
4 4
 
5 5
 use Exception;
6 6
 
7
-trait FilterTrait{
7
+trait FilterTrait {
8 8
     protected function checkFiltering(array $route)
9 9
     {
10
-        $filters = (is_array($route['filters'])) ? $route['filters'] : [ $route['filters'] ];
10
+        $filters = (is_array($route['filters'])) ? $route['filters'] : [$route['filters']];
11 11
 
12
-        foreach($filters as $filter){
13
-            if(is_null($filter)){
12
+        foreach ($filters as $filter) {
13
+            if (is_null($filter)) {
14 14
                 continue;
15 15
             }
16 16
             $this->checkFilter($filter);
@@ -19,20 +19,20 @@  discard block
 block discarded – undo
19 19
 
20 20
     protected function checkFilter(string $filtername)
21 21
     {
22
-        if(count(explode(':',$filtername)) != 2){
22
+        if (count(explode(':', $filtername)) != 2) {
23 23
             throw new Exception("Wrongly configured filter: {$filtername}.");
24 24
         }
25 25
 
26
-        $filter = $this->checkFilClassExist(explode(':',$filtername)[0]);
26
+        $filter = $this->checkFilClassExist(explode(':', $filtername)[0]);
27 27
 
28
-        if(!$filter->check(explode(':',$filtername)[1])){
29
-            throw new Exception($filter->getMessage(explode(':',$filtername)[1]),403);
28
+        if (!$filter->check(explode(':', $filtername)[1])) {
29
+            throw new Exception($filter->getMessage(explode(':', $filtername)[1]), 403);
30 30
         }
31 31
     }
32 32
 
33 33
     protected function checkFilClassExist(string $class)
34 34
     {
35
-        if(class_exists($class)){
35
+        if (class_exists($class)) {
36 36
             return new $class();
37 37
         }
38 38
         throw new Exception("Filter {$class} not found.");
Please login to merge, or discard this patch.
src/CheckTrait.php 1 patch
Spacing   +17 added lines, -17 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
-trait CheckTrait{
7
+trait CheckTrait {
8 8
     use FilterTrait, CheckWhere;
9 9
 
10 10
     protected function checkProtocol(string $expected, string $current): bool
@@ -12,14 +12,14 @@  discard block
 block discarded – undo
12 12
         return (strtoupper($expected) === strtoupper($current));
13 13
     }
14 14
 
15
-    protected function checkName(string $routeName){
16
-        if(!array_key_exists($routeName,$this->routers)){
15
+    protected function checkName(string $routeName) {
16
+        if (!array_key_exists($routeName, $this->routers)) {
17 17
             throw new Exception('Page not found.', 404);
18 18
         }
19 19
     }
20 20
 
21
-    protected function checkTypeRole($role){
22
-        if(!is_string($role) && @get_class($role) !== 'Closure' ){
21
+    protected function checkTypeRole($role) {
22
+        if (!is_string($role) && @get_class($role) !== 'Closure') {
23 23
             throw new Exception("Improperly defined route track.");
24 24
         }
25 25
     }
@@ -31,12 +31,12 @@  discard block
 block discarded – undo
31 31
 
32 32
     protected function checkParameters(array $routeLoop, array $routeRequest): bool
33 33
     {
34
-        foreach($routeLoop as $rr => $param){
35
-            if( (substr($param,0,1) === '{') ){
36
-                $_GET[ substr($param,1,strlen($param)-2) ] = $routeRequest[$rr];    
34
+        foreach ($routeLoop as $rr => $param) {
35
+            if ((substr($param, 0, 1) === '{')) {
36
+                $_GET[substr($param, 1, strlen($param)-2)] = $routeRequest[$rr];    
37 37
             }
38 38
     
39
-            if($this->checkParameter($param, $routeRequest[$rr])){
39
+            if ($this->checkParameter($param, $routeRequest[$rr])) {
40 40
                 return false;
41 41
             }
42 42
         }
@@ -45,22 +45,22 @@  discard block
 block discarded – undo
45 45
 
46 46
     protected function checkParameter(string $routeLoop, string $routeRequest)
47 47
     {
48
-        return !( substr($routeLoop,0,1) === '{' ) and $routeLoop !== $routeRequest;
48
+        return !(substr($routeLoop, 0, 1) === '{') and $routeLoop !== $routeRequest;
49 49
     }
50 50
 
51 51
     protected function checkRole()
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(strtoupper($protocol) !== strtoupper($currentProtocol)){
62
+        foreach ($protocols as $protocol) {
63
+            if (strtoupper($protocol) !== strtoupper($currentProtocol)) {
64 64
                 continue;
65 65
             }
66 66
         }
@@ -68,9 +68,9 @@  discard block
 block discarded – undo
68 68
 
69 69
     protected function checkToHiking($route, $routeRequest, $routeLoop): bool
70 70
     {
71
-        if($this->checkNumparams($routeLoop, $routeRequest) || 
71
+        if ($this->checkNumparams($routeLoop, $routeRequest) || 
72 72
             !$this->checkParameters($routeLoop, $routeRequest) ||
73
-            !$this->checkWhere($route, $routeRequest)){
73
+            !$this->checkWhere($route, $routeRequest)) {
74 74
                 return false;
75 75
         }
76 76
         return true;
@@ -78,7 +78,7 @@  discard block
 block discarded – undo
78 78
 
79 79
     protected function hasRouteName(string $name): void
80 80
     {
81
-        if(array_key_exists($name, $this->routers)){
81
+        if (array_key_exists($name, $this->routers)) {
82 82
             throw new Exception("There is already a route with the name {$name} configured.");
83 83
         }
84 84
     }
Please login to merge, or discard this patch.
src/Helper.php 1 patch
Spacing   +31 added lines, -31 removed lines patch added patch discarded remove patch
@@ -2,7 +2,7 @@  discard block
 block discarded – undo
2 2
 
3 3
 namespace HnrAzevedo\Router;
4 4
 
5
-trait Helper{
5
+trait Helper {
6 6
     use CheckTrait, ControllerTrait;
7 7
     
8 8
     private $currentRoute = null;
@@ -25,7 +25,7 @@  discard block
 block discarded – undo
25 25
     protected function getProtocol(): string
26 26
     {
27 27
         $protocol = ((isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')) ? 'ajax' : 'get';
28
-        $protocol = (array_key_existS('HTTP_REQUESTED_METHOD',$_SERVER)) ? strtolower($_SERVER['HTTP_REQUESTED_METHOD']) : $protocol;
28
+        $protocol = (array_key_existS('HTTP_REQUESTED_METHOD', $_SERVER)) ? strtolower($_SERVER['HTTP_REQUESTED_METHOD']) : $protocol;
29 29
             
30 30
         return $protocol;
31 31
     }
@@ -40,12 +40,12 @@  discard block
 block discarded – undo
40 40
         ];
41 41
     }
42 42
 
43
-    protected function ControllerForm($controller, string $method, array $values){
43
+    protected function ControllerForm($controller, string $method, array $values) {
44 44
 		$this->checkRole();
45 45
         $method = ($method !== 'method') ? $method : $this->getData()['POST']['role'];
46
-        $data = (array_key_exists('data',$values)) ? json_decode($values['data'], true) : null;
46
+        $data = (array_key_exists('data', $values)) ? json_decode($values['data'], true) : null;
47 47
 
48
-        call_user_func_array([$controller,$method],  $data);
48
+        call_user_func_array([$controller, $method], $data);
49 49
     }
50 50
 
51 51
     protected function Controller(string $controll): void
@@ -53,7 +53,7 @@  discard block
 block discarded – undo
53 53
         $data = $this->getData();
54 54
 
55 55
         foreach ($data['GET'] as $name => $value) {
56
-            $controll = str_replace('{'.$name.'}',$value,$controll);
56
+            $controll = str_replace('{'.$name.'}', $value, $controll);
57 57
         }
58 58
 
59 59
         $this->checkControllsettable($controll);
@@ -62,30 +62,30 @@  discard block
 block discarded – undo
62 62
 
63 63
         $this->checkControllmethod($controll);
64 64
 
65
-        $controller = ucfirst(explode(':',$controll)[0]);
65
+        $controller = ucfirst(explode(':', $controll)[0]);
66 66
         $controller = new $controller();
67
-        $method = explode(':',$controll)[1];
67
+        $method = explode(':', $controll)[1];
68 68
 
69
-        if( ($this->getProtocol() == 'form') ){
69
+        if (($this->getProtocol() == 'form')) {
70 70
             $this->ControllerForm($controller, $method, $data['POST']);
71 71
         }else {
72
-            $data = (array_key_exists('data',$data['POST'])) ? json_decode($data['POST']['data'], true) : $data['GET'];
73
-            call_user_func_array([$controller,$method],  $data);
72
+            $data = (array_key_exists('data', $data['POST'])) ? json_decode($data['POST']['data'], true) : $data['GET'];
73
+            call_user_func_array([$controller, $method], $data);
74 74
         }
75 75
        
76 76
     }    
77 77
 
78
-    protected function explodeRoutes(bool $bar, string $url ,bool $bar_, string $url_): array
78
+    protected function explodeRoutes(bool $bar, string $url, bool $bar_, string $url_): array
79 79
     {   
80
-        $url = $bar ? substr($url, 0, -1) : $url ;
81
-        $url = explode('/',$url);
80
+        $url = $bar ? substr($url, 0, -1) : $url;
81
+        $url = explode('/', $url);
82 82
 
83
-        $url_ = $bar_ ? substr($url_, 0, -1) : $url_ ;
84
-        $url_ = explode('/',$url_);
83
+        $url_ = $bar_ ? substr($url_, 0, -1) : $url_;
84
+        $url_ = explode('/', $url_);
85 85
 
86
-        foreach($url as $ur => $u){
87
-            if(substr($u,0,2) === '{?'){
88
-                if(!array_key_exists($ur,$url_)){
86
+        foreach ($url as $ur => $u) {
87
+            if (substr($u, 0, 2) === '{?') {
88
+                if (!array_key_exists($ur, $url_)) {
89 89
                     $url_[$ur] = '';
90 90
                 };
91 91
             }
@@ -96,28 +96,28 @@  discard block
 block discarded – undo
96 96
 
97 97
     protected function toHiking(array $route)
98 98
     {
99
-        $this->callOnRoute($route,'beforeAll');
100
-        $this->callOnRoute($route,'before');
99
+        $this->callOnRoute($route, 'beforeAll');
100
+        $this->callOnRoute($route, 'before');
101 101
 
102
-        if(is_string($route['role'])){
102
+        if (is_string($route['role'])) {
103 103
             $this->Controller($route['role']);
104
-            $this->callOnRoute($route,'after');
105
-            $this->callOnRoute($route,'afterAll');
104
+            $this->callOnRoute($route, 'after');
105
+            $this->callOnRoute($route, 'afterAll');
106 106
             return true;
107 107
         }
108 108
 
109
-        call_user_func_array($route['role'],$this->getData()['GET']);
109
+        call_user_func_array($route['role'], $this->getData()['GET']);
110 110
 
111
-        $this->callOnRoute($route,'after');
112
-        $this->callOnRoute($route,'afterAll');
111
+        $this->callOnRoute($route, 'after');
112
+        $this->callOnRoute($route, 'afterAll');
113 113
     }
114 114
 
115
-    protected function callOnRoute(array $route,string $state)
115
+    protected function callOnRoute(array $route, string $state)
116 116
     {
117
-        if($route[$state] !== null){
118
-            if(is_string($route[$state])){
117
+        if ($route[$state] !== null) {
118
+            if (is_string($route[$state])) {
119 119
                 $this->Controller($route[$state]);
120
-            }else{
120
+            }else {
121 121
                 $route[$state]();
122 122
             }
123 123
         }
Please login to merge, or discard this patch.
src/Router.php 1 patch
Spacing   +28 added lines, -28 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, CheckWhere, DefinitionsTrait, ExtraJobsTrait;
9 9
 
10 10
     private static $instance = null;
@@ -25,7 +25,7 @@  discard block
 block discarded – undo
25 25
 
26 26
     public static function create(): Router
27 27
     {
28
-        if(!self::getInstance()->instanced){
28
+        if (!self::getInstance()->instanced) {
29 29
             self::getInstance()->instanced = true;
30 30
         }
31 31
         
@@ -38,14 +38,14 @@  discard block
 block discarded – undo
38 38
         return self::$instance;
39 39
     }
40 40
 
41
-    public function set($url ,$walking , string $protocol): Router
41
+    public function set($url, $walking, string $protocol): Router
42 42
     {
43 43
         $this->lastReturn = null;
44 44
         
45
-		$url = (substr($url,0,1) !=='/' and strlen($url) > 0) ? "/{$url}" : $url;
45
+		$url = (substr($url, 0, 1) !== '/' and strlen($url) > 0) ? "/{$url}" : $url;
46 46
 
47
-    	foreach($this->routers as $key => $value){
48
-    		if( md5($this->prefix . $value['url'] . $value['protocol'] ) === md5( $url . $protocol ) ){
47
+    	foreach ($this->routers as $key => $value) {
48
+    		if (md5($this->prefix.$value['url'].$value['protocol']) === md5($url.$protocol)) {
49 49
                 throw new Exception("There is already a route with the url {$url} and with the {$protocol} protocol configured.");
50 50
             }
51 51
         }
@@ -70,9 +70,9 @@  discard block
 block discarded – undo
70 70
         return self::getInstance();
71 71
     }
72 72
 
73
-    public static function group(string $prefix,$callback): Router
73
+    public static function group(string $prefix, $callback): Router
74 74
     {
75
-        self::getInstance()->prefix = (substr($prefix,0,1) !== '/') ? "/{$prefix}" : $prefix;
75
+        self::getInstance()->prefix = (substr($prefix, 0, 1) !== '/') ? "/{$prefix}" : $prefix;
76 76
         self::getInstance()->group = sha1(date('d/m/Y h:m:i'));
77 77
         $callback();
78 78
         self::getInstance()->group = null;
@@ -83,7 +83,7 @@  discard block
 block discarded – undo
83 83
 
84 84
     public static function where(): Router
85 85
     {
86
-        if(self::getInstance()->lastReturn){
86
+        if (self::getInstance()->lastReturn) {
87 87
             throw new Exception("It is not possible to define parameter tests for groups of routes.");
88 88
         }
89 89
 
@@ -94,7 +94,7 @@  discard block
 block discarded – undo
94 94
 
95 95
     public static function name(string $name): Router
96 96
     {
97
-        if(self::getInstance()->lastReturn){
97
+        if (self::getInstance()->lastReturn) {
98 98
             throw new Exception("There is no reason to call a {$name} route group.");
99 99
         }
100 100
 
@@ -102,11 +102,11 @@  discard block
 block discarded – undo
102 102
 
103 103
         self::getInstance()->hasRouteName($name);
104 104
 
105
-        if(in_array($name,self::getInstance()->beforeExcepts)){
105
+        if (in_array($name, self::getInstance()->beforeExcepts)) {
106 106
             $currentRoute['beforeAll'] = null;
107 107
         }
108 108
 
109
-        if(in_array($name,self::getInstance()->afterExcepts)){
109
+        if (in_array($name, self::getInstance()->afterExcepts)) {
110 110
             $currentRoute['afterAll'] = null;
111 111
         }
112 112
 
@@ -119,15 +119,15 @@  discard block
 block discarded – undo
119 119
 
120 120
     private function byName(?string $routName)
121 121
     {
122
-        if(!is_null($routName)){
122
+        if (!is_null($routName)) {
123 123
             $currentProtocol = $this->getProtocol();
124 124
 
125 125
             $this->checkName($routName);
126 126
     
127 127
             $route = $this->routers[$routName];
128 128
     
129
-            if(!$this->checkProtocol($route['protocol'], $currentProtocol)){
130
-                throw new Exception('Page not found.',404);
129
+            if (!$this->checkProtocol($route['protocol'], $currentProtocol)) {
130
+                throw new Exception('Page not found.', 404);
131 131
             }
132 132
     
133 133
             $this->checkFiltering($route);
@@ -145,11 +145,11 @@  discard block
 block discarded – undo
145 145
 
146 146
 		$currentProtocol = $instance->getInstance()->getProtocol();
147 147
 
148
-        foreach(array_reverse($instance->getInstance()->routers) as $r => $route){
148
+        foreach (array_reverse($instance->getInstance()->routers) as $r => $route) {
149 149
 
150 150
             $instance->getInstance()->currentRoute = $route;
151 151
 
152
-            if(!$instance->getInstance()->checkProtocol($route['protocol'], $currentProtocol)){
152
+            if (!$instance->getInstance()->checkProtocol($route['protocol'], $currentProtocol)) {
153 153
                 continue;
154 154
             }
155 155
 
@@ -159,11 +159,11 @@  discard block
 block discarded – undo
159 159
 
160 160
 
161 161
             $routs = $instance->getInstance()->explodeRoutes(
162
-                (substr($route['url'],strlen($route['url'])-1,1) === '/') , $route['url'],
163
-                (substr($_SERVER['REQUEST_URI'],strlen($_SERVER['REQUEST_URI'])-1,1) === '/') , $_SERVER['REQUEST_URI']
162
+                (substr($route['url'], strlen($route['url'])-1, 1) === '/'), $route['url'],
163
+                (substr($_SERVER['REQUEST_URI'], strlen($_SERVER['REQUEST_URI'])-1, 1) === '/'), $_SERVER['REQUEST_URI']
164 164
             );
165 165
 
166
-            if(!$instance->getInstance()->checkToHiking($route, $routs['routeRequest'], $routs['routeLoop'])){
166
+            if (!$instance->getInstance()->checkToHiking($route, $routs['routeRequest'], $routs['routeLoop'])) {
167 167
                 continue;
168 168
             }
169 169
 
@@ -176,18 +176,18 @@  discard block
 block discarded – undo
176 176
         
177 177
         $instance->getInstance()->currentRoute = null;
178 178
 
179
-	    throw new Exception('Page not found.',404);
179
+	    throw new Exception('Page not found.', 404);
180 180
     }
181 181
 
182 182
     public function filter($filters): Router
183 183
     {
184
-        if($this->lastReturn !== null){
184
+        if ($this->lastReturn !== null) {
185 185
             $currentGroup = end($this->routers)['group'];
186 186
 
187 187
             foreach ($this->routers as $key => $value) {
188 188
 
189
-                if($value['group'] === $currentGroup){
190
-                    $currentRoute = $this->addFilter($this->routers[$key],$filters);
189
+                if ($value['group'] === $currentGroup) {
190
+                    $currentRoute = $this->addFilter($this->routers[$key], $filters);
191 191
                     $this->routers[$key] = $currentRoute;
192 192
                 }
193 193
 
@@ -196,24 +196,24 @@  discard block
 block discarded – undo
196 196
             return $this;
197 197
         }
198 198
         
199
-        $this->routers[count($this->routers)-1] = $this->addFilter(end($this->routers),$filters);
199
+        $this->routers[count($this->routers)-1] = $this->addFilter(end($this->routers), $filters);
200 200
         return $this;
201 201
     }
202 202
 
203 203
     public static function addFilter(array $route, $filter): array
204 204
     {
205
-        if(is_null($route['filters'])){
205
+        if (is_null($route['filters'])) {
206 206
             $route['filters'] = $filter;
207 207
             return $route;
208 208
         }
209 209
 
210 210
         $filters = (is_array($filter)) ? $filter : [0 => $filter];
211 211
 
212
-        if(is_array($route['filters'])){
212
+        if (is_array($route['filters'])) {
213 213
             foreach ($route['filters'] as $key => $value) {
214 214
                 $filters[] = $value;
215 215
             }
216
-        }else{
216
+        }else {
217 217
             $filters[] = $route['filters'];
218 218
         }
219 219
 
Please login to merge, or discard this patch.
src/ControllerTrait.php 1 patch
Spacing   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -4,28 +4,28 @@
 block discarded – undo
4 4
 
5 5
 use Exception;
6 6
 
7
-trait ControllerTrait{
7
+trait ControllerTrait {
8 8
 
9 9
     protected function checkControllsettable(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 checkControllexist(string $controll)
17 17
     {
18
-        $controllname = ucfirst(explode(':',$controll)[0]);
19
-        if(!class_exists($controllname)){
18
+        $controllname = ucfirst(explode(':', $controll)[0]);
19
+        if (!class_exists($controllname)) {
20 20
             throw new Exception("No controller {$controllname} found.");
21 21
         }
22 22
     }
23 23
 
24 24
     protected function checkControllmethod(string $controll)
25 25
     {
26
-        $controllname = ucfirst(explode(':',$controll)[0]);
27
-        $method = explode(':',$controll)[1];
28
-        if(!method_exists($controllname, $method)){
26
+        $controllname = ucfirst(explode(':', $controll)[0]);
27
+        $method = explode(':', $controll)[1];
28
+        if (!method_exists($controllname, $method)) {
29 29
             throw new Exception("No method {$method} found for {$controllname}.");
30 30
         }
31 31
     }
Please login to merge, or discard this patch.
examples/Routes/default.php 1 patch
Spacing   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -3,21 +3,21 @@
 block discarded – undo
3 3
 use HnrAzevedo\Router\Router;
4 4
 
5 5
 /* Returning parameters passed via URL in anonymous functions */
6
-Router::get('/{parameter}/{otherparameter}', function($parameter, $otherparameter){
6
+Router::get('/{parameter}/{otherparameter}', function($parameter, $otherparameter) {
7 7
     echo "Parameter 1:{$parameter}, Parameter 2:{$otherparameter}.";
8 8
 });
9 9
 
10 10
 /* Passing controller and/or method via parameter in URL */
11
-Router::get('/{controller}/{method}','{controller}:{method}');
12
-Router::get('/{controller}/{method}','{controller}:method');
11
+Router::get('/{controller}/{method}', '{controller}:{method}');
12
+Router::get('/{controller}/{method}', '{controller}:method');
13 13
 
14 14
 /* Passing value via parameter */
15
-Router::get('/my-account/{teste}','Controller\\User:my_account');
15
+Router::get('/my-account/{teste}', 'Controller\\User:my_account');
16 16
 
17 17
 /* Filter example */
18
-Router::get('/my-account','Controller\\User:my_account')->filter('Filter\\User:user_in');
18
+Router::get('/my-account', 'Controller\\User:my_account')->filter('Filter\\User:user_in');
19 19
 
20 20
 /* Accessed by all protocols */
21
-Router::any('/',function(){
21
+Router::any('/', function() {
22 22
     //
23 23
 });
Please login to merge, or discard this patch.