[Routing] fix setting empty requirement in Route

This commit is contained in:
Tobias Schultze 2012-09-04 06:31:46 +02:00
parent c0673d77d0
commit 1f5b7930c0
2 changed files with 16 additions and 11 deletions

View File

@ -179,7 +179,7 @@ class Route implements \Serializable
*
* @param string $name An option name
*
* @return mixed The option value
* @return mixed The option value or null when not given
*/
public function getOption($name)
{
@ -236,7 +236,7 @@ class Route implements \Serializable
*
* @param string $name A variable name
*
* @return mixed The default value
* @return mixed The default value or null when not given
*/
public function getDefault($name)
{
@ -323,7 +323,7 @@ class Route implements \Serializable
*
* @param string $key The key
*
* @return string The regex
* @return string|null The regex or null when not given
*/
public function getRequirement($key)
{
@ -352,6 +352,8 @@ class Route implements \Serializable
* Compiles the route.
*
* @return CompiledRoute A CompiledRoute instance
*
* @see RouteCompiler which is responsible for the compilation process
*/
public function compile()
{
@ -371,21 +373,21 @@ class Route implements \Serializable
private function sanitizeRequirement($key, $regex)
{
if (!is_string($regex)) {
throw new \InvalidArgumentException(sprintf('Routing requirement for "%s" must be a string', $key));
throw new \InvalidArgumentException(sprintf('Routing requirement for "%s" must be a string.', $key));
}
if ('' === $regex) {
throw new \InvalidArgumentException(sprintf('Routing requirement for "%s" cannot be empty', $key));
}
if ('^' === $regex[0]) {
$regex = substr($regex, 1);
if ('' !== $regex && '^' === $regex[0]) {
$regex = (string) substr($regex, 1); // returns false for a single character
}
if ('$' === substr($regex, -1)) {
$regex = substr($regex, 0, -1);
}
if ('' === $regex) {
throw new \InvalidArgumentException(sprintf('Routing requirement for "%s" cannot be empty.', $key));
}
return $regex;
}
}

View File

@ -112,7 +112,10 @@ class RouteTest extends \PHPUnit_Framework_TestCase
{
return array(
array(''),
array(array())
array(array()),
array('^$'),
array('^'),
array('$')
);
}