From 61ad98a6e2914578cae32f249d2b496892c0f10b Mon Sep 17 00:00:00 2001
From: filippo.bertilotti <filippobertilotti@gmail.com>
Date: Mon, 27 May 2024 10:44:00 +0200
Subject: [PATCH] aggiunto controllo sul metodo post in modo da non poter raggiungere la route destroy utilizzando il get (parte 144)

---
 src/Framework/router.php |   27 ++++++++++++++++++++-------
 1 files changed, 20 insertions(+), 7 deletions(-)

diff --git a/src/Framework/router.php b/src/Framework/router.php
index 74c2fdf..e2f58e3 100644
--- a/src/Framework/router.php
+++ b/src/Framework/router.php
@@ -12,8 +12,9 @@
 
 
     }
-    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) {
@@ -22,7 +23,16 @@
 
             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;
             }
         }
         
@@ -36,12 +46,15 @@
         
         $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";
     }
 }
\ No newline at end of file

--
Gitblit v1.8.0