Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1f5b793

Browse files
committed
[Routing] fix setting empty requirement in Route
1 parent c0673d7 commit 1f5b793

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

src/Symfony/Component/Routing/Route.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public function setOption($name, $value)
179179
*
180180
* @param string $name An option name
181181
*
182-
* @return mixed The option value
182+
* @return mixed The option value or null when not given
183183
*/
184184
public function getOption($name)
185185
{
@@ -236,7 +236,7 @@ public function addDefaults(array $defaults)
236236
*
237237
* @param string $name A variable name
238238
*
239-
* @return mixed The default value
239+
* @return mixed The default value or null when not given
240240
*/
241241
public function getDefault($name)
242242
{
@@ -323,7 +323,7 @@ public function addRequirements(array $requirements)
323323
*
324324
* @param string $key The key
325325
*
326-
* @return string The regex
326+
* @return string|null The regex or null when not given
327327
*/
328328
public function getRequirement($key)
329329
{
@@ -352,6 +352,8 @@ public function setRequirement($key, $regex)
352352
* Compiles the route.
353353
*
354354
* @return CompiledRoute A CompiledRoute instance
355+
*
356+
* @see RouteCompiler which is responsible for the compilation process
355357
*/
356358
public function compile()
357359
{
@@ -371,21 +373,21 @@ public function compile()
371373
private function sanitizeRequirement($key, $regex)
372374
{
373375
if (!is_string($regex)) {
374-
throw new \InvalidArgumentException(sprintf('Routing requirement for "%s" must be a string', $key));
375-
}
376-
377-
if ('' === $regex) {
378-
throw new \InvalidArgumentException(sprintf('Routing requirement for "%s" cannot be empty', $key));
376+
throw new \InvalidArgumentException(sprintf('Routing requirement for "%s" must be a string.', $key));
379377
}
380378

381-
if ('^' === $regex[0]) {
382-
$regex = substr($regex, 1);
379+
if ('' !== $regex && '^' === $regex[0]) {
380+
$regex = (string) substr($regex, 1); // returns false for a single character
383381
}
384382

385383
if ('$' === substr($regex, -1)) {
386384
$regex = substr($regex, 0, -1);
387385
}
388386

387+
if ('' === $regex) {
388+
throw new \InvalidArgumentException(sprintf('Routing requirement for "%s" cannot be empty.', $key));
389+
}
390+
389391
return $regex;
390392
}
391393
}

src/Symfony/Component/Routing/Tests/RouteTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,10 @@ public function getInvalidRequirements()
112112
{
113113
return array(
114114
array(''),
115-
array(array())
115+
array(array()),
116+
array('^$'),
117+
array('^'),
118+
array('$')
116119
);
117120
}
118121

0 commit comments

Comments
 (0)