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

View File

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