@@ -43,7 +43,7 @@ discard block |
||
| 43 | 43 | /** |
| 44 | 44 | * Returns the uri(the pattern path). |
| 45 | 45 | * |
| 46 | - * @return mixed |
|
| 46 | + * @return string |
|
| 47 | 47 | */ |
| 48 | 48 | |
| 49 | 49 | public function getUri() { |
@@ -53,7 +53,7 @@ discard block |
||
| 53 | 53 | /** |
| 54 | 54 | * Returns the request method registered. |
| 55 | 55 | * |
| 56 | - * @return mixed |
|
| 56 | + * @return string |
|
| 57 | 57 | */ |
| 58 | 58 | |
| 59 | 59 | public function getMethod() { |
@@ -63,7 +63,7 @@ discard block |
||
| 63 | 63 | /** |
| 64 | 64 | * Returns the callback associated to the pattern. |
| 65 | 65 | * |
| 66 | - * @return mixed |
|
| 66 | + * @return callable |
|
| 67 | 67 | */ |
| 68 | 68 | |
| 69 | 69 | public function getResponse() { |
@@ -106,11 +106,11 @@ |
||
| 106 | 106 | */ |
| 107 | 107 | |
| 108 | 108 | private function isValidMiddleware() { |
| 109 | - foreach($this->middlewares as $key => $value) { |
|
| 110 | - if(!in_array($key, $this->middlewareTypes)) { |
|
| 109 | + foreach ($this->middlewares as $key => $value) { |
|
| 110 | + if (!in_array($key, $this->middlewareTypes)) { |
|
| 111 | 111 | throw new InvalidMiddleware("Only before and after middleware types are valid"); |
| 112 | 112 | } |
| 113 | - if(!is_callable($value)) { |
|
| 113 | + if (!is_callable($value)) { |
|
| 114 | 114 | throw new InvalidMiddleware("The middleware must be callable"); |
| 115 | 115 | } |
| 116 | 116 | } |
@@ -20,6 +20,9 @@ |
||
| 20 | 20 | */ |
| 21 | 21 | const PARAMETER_IDENTIFIER = ':'; |
| 22 | 22 | |
| 23 | + /** |
|
| 24 | + * @param string $path |
|
| 25 | + */ |
|
| 23 | 26 | public function __construct($path) { |
| 24 | 27 | $this->path = $path; |
| 25 | 28 | } |
@@ -65,14 +65,14 @@ discard block |
||
| 65 | 65 | */ |
| 66 | 66 | |
| 67 | 67 | public function countParams($pattern) { |
| 68 | - if($this->hasParams($pattern) === false) { |
|
| 68 | + if ($this->hasParams($pattern) === false) { |
|
| 69 | 69 | return 0; |
| 70 | 70 | } |
| 71 | 71 | |
| 72 | 72 | $pattern = str_split($pattern); // split string in characters |
| 73 | 73 | $totalParams = 0; |
| 74 | - for($i = 0; $i < count($pattern); $i++) { |
|
| 75 | - if($pattern[$i] === self::PARAMETER_IDENTIFIER) { |
|
| 74 | + for ($i = 0; $i < count($pattern); $i++) { |
|
| 75 | + if ($pattern[$i] === self::PARAMETER_IDENTIFIER) { |
|
| 76 | 76 | $totalParams++; |
| 77 | 77 | } |
| 78 | 78 | } |
@@ -90,15 +90,15 @@ discard block |
||
| 90 | 90 | */ |
| 91 | 91 | |
| 92 | 92 | public function getParams($pattern) { |
| 93 | - if(!$this->hasParams($pattern)) { |
|
| 93 | + if (!$this->hasParams($pattern)) { |
|
| 94 | 94 | return []; |
| 95 | 95 | } |
| 96 | 96 | |
| 97 | 97 | $pattern = explode('/', $pattern); |
| 98 | 98 | $path = explode('/', $this->path); |
| 99 | 99 | $params = []; |
| 100 | - for($i = 0; $i < count($pattern); $i++) { |
|
| 101 | - if(strpos($pattern[$i], self::PARAMETER_IDENTIFIER) !== false) { |
|
| 100 | + for ($i = 0; $i < count($pattern); $i++) { |
|
| 101 | + if (strpos($pattern[$i], self::PARAMETER_IDENTIFIER) !== false) { |
|
| 102 | 102 | $params[substr($pattern[$i], 1)] = $path[$i]; |
| 103 | 103 | } |
| 104 | 104 | } |
@@ -117,20 +117,20 @@ discard block |
||
| 117 | 117 | $pattern = explode('/', $pattern); |
| 118 | 118 | $path = explode('/', $this->path); |
| 119 | 119 | |
| 120 | - if(count($pattern) !== count($path)) { |
|
| 120 | + if (count($pattern) !== count($path)) { |
|
| 121 | 121 | return false; |
| 122 | 122 | } |
| 123 | 123 | |
| 124 | 124 | $found = true; |
| 125 | 125 | $index = 0; |
| 126 | - while($found && $index < count($pattern)) { |
|
| 126 | + while ($found && $index < count($pattern)) { |
|
| 127 | 127 | $check = true; |
| 128 | - if(strpos($pattern[$index], self::PARAMETER_IDENTIFIER) !== false) { |
|
| 128 | + if (strpos($pattern[$index], self::PARAMETER_IDENTIFIER) !== false) { |
|
| 129 | 129 | $check = false; |
| 130 | 130 | } |
| 131 | 131 | |
| 132 | - if($check) { |
|
| 133 | - if (!preg_match('/^'. $pattern[$index] .'$/', $path[$index])) { |
|
| 132 | + if ($check) { |
|
| 133 | + if (!preg_match('/^'.$pattern[$index].'$/', $path[$index])) { |
|
| 134 | 134 | $found = false; |
| 135 | 135 | } |
| 136 | 136 | } |
@@ -104,11 +104,11 @@ discard block |
||
| 104 | 104 | $found = false; |
| 105 | 105 | $counter = 0; |
| 106 | 106 | |
| 107 | - if(count($this->routes) === 0) { |
|
| 107 | + if (count($this->routes) === 0) { |
|
| 108 | 108 | return $found; |
| 109 | 109 | } |
| 110 | 110 | |
| 111 | - while($found === false && $counter < count($this->routes)) { |
|
| 111 | + while ($found === false && $counter < count($this->routes)) { |
|
| 112 | 112 | if ($this->routes[$counter]->getUri() === $uri && $this->routes[$counter]->getMethod() === $method) { |
| 113 | 113 | $found = true; |
| 114 | 114 | } |
@@ -129,7 +129,7 @@ discard block |
||
| 129 | 129 | */ |
| 130 | 130 | |
| 131 | 131 | private function addOrder($uri, $method, $response, array $middleware = []) { |
| 132 | - if($this->find($uri, $method)) { // search if exists an apparition |
|
| 132 | + if ($this->find($uri, $method)) { // search if exists an apparition |
|
| 133 | 133 | return false; |
| 134 | 134 | } |
| 135 | 135 | |
@@ -207,7 +207,7 @@ discard block |
||
| 207 | 207 | */ |
| 208 | 208 | |
| 209 | 209 | public function notFound($func) { |
| 210 | - if(is_callable($func)) { |
|
| 210 | + if (is_callable($func)) { |
|
| 211 | 211 | $this->notFound = $func; |
| 212 | 212 | } |
| 213 | 213 | } |
@@ -225,15 +225,15 @@ discard block |
||
| 225 | 225 | public function run() { |
| 226 | 226 | $found = false; |
| 227 | 227 | $counter = 0; |
| 228 | - while($found === false && $counter < count($this->routes)) { |
|
| 229 | - if($this->routerParser->match($this->routes[$counter]->getUri()) && $this->routes[$counter]->getMethod() === $this->requestMethod) { |
|
| 228 | + while ($found === false && $counter < count($this->routes)) { |
|
| 229 | + if ($this->routerParser->match($this->routes[$counter]->getUri()) && $this->routes[$counter]->getMethod() === $this->requestMethod) { |
|
| 230 | 230 | $found = true; |
| 231 | 231 | } else { |
| 232 | 232 | $counter++; |
| 233 | 233 | } |
| 234 | 234 | } |
| 235 | 235 | |
| 236 | - if($found) { |
|
| 236 | + if ($found) { |
|
| 237 | 237 | // todo if exists before middleware, execute it. |
| 238 | 238 | $params = $this->routerParser->getParams($this->routes[$counter]->getUri()); |
| 239 | 239 | $response = call_user_func_array($this->routes[$counter]->getResponse(), $params); |