| | |
| | | |
| | | |
| | | } |
| | | public function match (string $path,): array|bool { |
| | | |
| | | public function match (string $path, string $method): array|bool { |
| | | $path = urldecode($path); |
| | | |
| | | $path = trim($path, "/"); |
| | | |
| | | foreach ($this->routes as $route) { |
| | |
| | | |
| | | if(preg_match($pattern, $path, $matches)) { |
| | | $matches = array_filter($matches, "is_string", ARRAY_FILTER_USE_KEY); |
| | | return $matches; |
| | | |
| | | $params = array_merge($matches, $route["params"]); |
| | | |
| | | if(array_key_exists("method", $params)) { |
| | | if(strtolower($method) !== strtolower($params["method"])) { |
| | | continue; |
| | | } |
| | | } |
| | | |
| | | return $params; |
| | | } |
| | | } |
| | | |
| | |
| | | |
| | | $segments = array_map(function(string $segment) :string { |
| | | |
| | | preg_match("#^\{([a-z][a-z0-9]*)\}$#", $segment, $matches); |
| | | $segment = "(?<" . $matches[1] . ">[a-z]+)"; |
| | | |
| | | if(preg_match("#^\{([a-z][a-z0-9]*)\}$#", $segment, $matches)) { |
| | | return "(?<" . $matches[1] . ">[^/]*)"; |
| | | } |
| | | if(preg_match("#^\{([a-z][a-z0-9]*):(.+)\}$#", $segment, $matches)) { |
| | | return "(?<" . $matches[1] . ">". $matches[2] ."[^/]*)"; |
| | | } |
| | | return $segment; |
| | | }, $segments); |
| | | |
| | | return "#^" . implode("/", $segments) . "$#"; |
| | | return "#^" . implode("/", $segments) . "$#iu"; |
| | | } |
| | | } |