Explorar el Código

[Routing] added missing Route::setRequirement()

Fabien Potencier hace 14 años
padre
commit
b2eec52429
Se han modificado 1 ficheros con 29 adiciones y 13 borrados
  1. 29 13
      src/Symfony/Component/Routing/Route.php

+ 29 - 13
src/Symfony/Component/Routing/Route.php

@@ -193,19 +193,7 @@ class Route
     {
         $this->requirements = array();
         foreach ($requirements as $key => $regex) {
-            if (is_array($regex)) {
-                throw new \InvalidArgumentException(sprintf('Routing requirements must be a string, array given for "%s"', $key));
-            }
-
-            if ('^' == $regex[0]) {
-                $regex = substr($regex, 1);
-            }
-
-            if ('$' == substr($regex, -1)) {
-                $regex = substr($regex, 0, -1);
-            }
-
-            $this->requirements[$key] = $regex;
+            $this->requirements[$key] = $this->sanitizeRequirement($key, $regex);
         }
 
         return $this;
@@ -221,6 +209,17 @@ class Route
         return isset($this->requirements[$key]) ? $this->requirements[$key] : null;
     }
 
+    /**
+     * Sets a requirement for the given key.
+     *
+     * @param string The key
+     * @param string The regex
+     */
+    public function setRequirement($key, $regex)
+    {
+        return $this->requirements[$key] = $this->sanitizeRequirement($key, $regex);
+    }
+
     /**
      * Compiles the route.
      *
@@ -240,4 +239,21 @@ class Route
 
         return $this->compiled = static::$compilers[$class]->compile($this);
     }
+
+    protected function sanitizeRequirement($key, $regex)
+    {
+        if (is_array($regex)) {
+            throw new \InvalidArgumentException(sprintf('Routing requirements must be a string, array given for "%s"', $key));
+        }
+
+        if ('^' == $regex[0]) {
+            $regex = substr($regex, 1);
+        }
+
+        if ('$' == substr($regex, -1)) {
+            $regex = substr($regex, 0, -1);
+        }
+
+        return $regex;
+    }
 }