From e3d936b79bfe45c9dbe5ecdae467ac7ef8bf24b3 Mon Sep 17 00:00:00 2001 From: filippo.bertilotti <filippobertilotti@gmail.com> Date: Wed, 15 May 2024 10:53:47 +0200 Subject: [PATCH] aggiunta strict type per evitare errori sui tipi (parte 93) --- src/Framework/router.php | 28 +++++++++++++++++----------- 1 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/Framework/router.php b/src/Framework/router.php index ad3f6fa..63596c4 100644 --- a/src/Framework/router.php +++ b/src/Framework/router.php @@ -12,37 +12,43 @@ } - public function match (string $path,): array|bool { + public function match (string $path): array|bool { + $path = urldecode($path); + + $path = trim($path, "/"); foreach ($this->routes as $route) { - $pattern = "#^/(?<controller>[a-z]+)/(?<action>[a-z]+)$#"; - echo $pattern, "\n", $route["path"],"\n"; - - $this->getPatternFromRoutePath($route["path"]); + $pattern = $this->getPatternFromRoutePath($route["path"]); if(preg_match($pattern, $path, $matches)) { $matches = array_filter($matches, "is_string", ARRAY_FILTER_USE_KEY); - return $matches; + + $params = array_merge($matches, $route["params"]); + + return $params; } } return false; } - private function getPatternFromRoutePath(string $route_path) { + private function getPatternFromRoutePath(string $route_path): string { $route_path = trim("$route_path", "/"); $segments = explode("/", $route_path); $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); - print_r($segments); + return "#^" . implode("/", $segments) . "$#iu"; } } \ No newline at end of file -- Gitblit v1.8.0