Skip to content

Commit

Permalink
[Routing] added missing Route::setRequirement()
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Nov 30, 2010
1 parent 87846f1 commit b2eec52
Showing 1 changed file with 29 additions and 13 deletions.
42 changes: 29 additions & 13 deletions src/Symfony/Component/Routing/Route.php
Expand Up @@ -193,19 +193,7 @@ public function setRequirements(array $requirements)
{
$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);

This comment has been minimized.

Copy link
@trompette

trompette Dec 1, 2010

Contributor

Why not use setRequirement() here ?

}

return $this;
Expand All @@ -221,6 +209,17 @@ public function getRequirement($key)
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.
*
Expand All @@ -240,4 +239,21 @@ public function compile()

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;
}
}

0 comments on commit b2eec52

Please sign in to comment.