Passed
Push — master ( ba5d16...93f7f3 )
by Henri
01:22
created
src/Router.php 1 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, $walking, $protocol);
60 60
     }
61 61
 
62
-    public function set($url ,$walking , string $protocol): Router
62
+    public function set($url, $walking, string $protocol): 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
         }
@@ -84,9 +84,9 @@  discard block
 block discarded – undo
84 84
         return self::getInstance();
85 85
     }
86 86
 
87
-    public static function group(string $prefix,$callback): Router
87
+    public static function group(string $prefix, $callback): Router
88 88
     {
89
-        self::getInstance()->prefix = (substr($prefix,0,1) !== '/') ? "/{$prefix}" : $prefix;
89
+        self::getInstance()->prefix = (substr($prefix, 0, 1) !== '/') ? "/{$prefix}" : $prefix;
90 90
         self::getInstance()->group = sha1(date('d/m/Y h:m:i'));
91 91
         $callback();
92 92
         self::getInstance()->group = null;
@@ -97,14 +97,14 @@  discard block
 block discarded – undo
97 97
 
98 98
     public static function name(string $name): Router
99 99
     {
100
-        if(self::getInstance()->lastReturn){
100
+        if (self::getInstance()->lastReturn) {
101 101
             throw new Exception("There is no reason to call a {$name} route group.");
102 102
         }
103 103
 
104 104
         $currentRoute = end(self::getInstance()->routers);
105 105
 
106
-        foreach(self::getInstance()->routers as $key => $value){
107
-            if(array_key_exists($name, self::getInstance()->routers)){
106
+        foreach (self::getInstance()->routers as $key => $value) {
107
+            if (array_key_exists($name, self::getInstance()->routers)) {
108 108
                 throw new Exception("There is already a route with the name {$name} configured.");
109 109
             }
110 110
         }
@@ -120,7 +120,7 @@  discard block
 block discarded – undo
120 120
 
121 121
     private function byName(?string $route_name)
122 122
     {
123
-        if(!is_null($route_name)){
123
+        if (!is_null($route_name)) {
124 124
             $currentProtocol = $this->getProtocol();
125 125
 
126 126
             $this->check_name($route_name);
@@ -142,17 +142,17 @@  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
-            $route_loop = $this->explodeRoute( (substr($route['url'],strlen($route['url'])-1,1) === '/') , $route['url']);
149
+            $route_loop = $this->explodeRoute((substr($route['url'], strlen($route['url'])-1, 1) === '/'), $route['url']);
150 150
             
151 151
             $_SERVER['REQUEST_URI'] = (array_key_exists('REQUEST_URI', $_SERVER)) ? $_SERVER['REQUEST_URI'] : '';
152 152
 
153
-            $route_request = $this->explodeRoute((substr($_SERVER['REQUEST_URI'],strlen($_SERVER['REQUEST_URI'])-1,1) === '/') , $_SERVER['REQUEST_URI']);
153
+            $route_request = $this->explodeRoute((substr($_SERVER['REQUEST_URI'], strlen($_SERVER['REQUEST_URI'])-1, 1) === '/'), $_SERVER['REQUEST_URI']);
154 154
 
155
-	        if($this->check_numparams($route_loop, $route_request) || !$this->check_parameters($route_loop, $route_request)){
155
+	        if ($this->check_numparams($route_loop, $route_request) || !$this->check_parameters($route_loop, $route_request)) {
156 156
                 continue;
157 157
             }
158 158
             
@@ -162,18 +162,18 @@  discard block
 block discarded – undo
162 162
 	        return true;
163 163
 	    }
164 164
 
165
-	    throw new Exception('Page not found.',404);
165
+	    throw new Exception('Page not found.', 404);
166 166
     }
167 167
 
168 168
     public static function filter($filters): Router
169 169
     {
170
-        if(self::getInstance()->lastReturn !== null){
170
+        if (self::getInstance()->lastReturn !== null) {
171 171
             $currentGroup = end(self::getInstance()->routers)['group'];
172 172
 
173 173
             foreach (self::getInstance()->routers as $key => $value) {
174 174
 
175
-                if($value['group'] === $currentGroup){
176
-                    $currentRoute = self::getInstance()->addFilter(self::getInstance()->routers[$key],$filters);
175
+                if ($value['group'] === $currentGroup) {
176
+                    $currentRoute = self::getInstance()->addFilter(self::getInstance()->routers[$key], $filters);
177 177
                     self::getInstance()->routers[$key] = $currentRoute;
178 178
                 }
179 179
 
@@ -182,24 +182,24 @@  discard block
 block discarded – undo
182 182
             return self::getInstance();
183 183
         }
184 184
         
185
-        self::getInstance()->routers[count(self::getInstance()->routers)-1] = self::getInstance()->addFilter(end(self::getInstance()->routers),$filters);
185
+        self::getInstance()->routers[count(self::getInstance()->routers)-1] = self::getInstance()->addFilter(end(self::getInstance()->routers), $filters);
186 186
         return self::getInstance();
187 187
     }
188 188
 
189 189
     public static function addFilter(array $route, $filter): array
190 190
     {
191
-        if(is_null($route['filters'])){
191
+        if (is_null($route['filters'])) {
192 192
             $route['filters'] = $filter;
193 193
             return $route;
194 194
         }
195 195
 
196 196
         $filters = (is_array($filter)) ? $filter : [0 => $filter];
197 197
 
198
-        if(is_array($route['filters'])){
198
+        if (is_array($route['filters'])) {
199 199
             foreach ($route['filters'] as $key => $value) {
200 200
                 $filters[] = $value;
201 201
             }
202
-        }else{
202
+        }else {
203 203
             $filters[] = $route['filters'];
204 204
         }
205 205
 
Please login to merge, or discard this patch.