Commit b5e4aa96 authored by Fabien Potencier's avatar Fabien Potencier

added a way to set requirements on the routes

$app->get('/{id}', function($id) {
    // ...
})->assert('id', '\d+');
parent 737d4f0f
...@@ -64,6 +64,21 @@ class Controller ...@@ -64,6 +64,21 @@ class Controller
} }
$this->routeName = $routeName; $this->routeName = $routeName;
return $this;
}
/**
* Sets the requirement for a route variable.
*
* @param string $variable The variable name
* @param string $regexp The regexp to apply
*/
public function assert($variable, $regexp)
{
$this->route->setRequirement($variable, $regexp);
return $this;
} }
/** /**
......
...@@ -24,7 +24,9 @@ class ControllerTest extends \PHPUnit_Framework_TestCase ...@@ -24,7 +24,9 @@ class ControllerTest extends \PHPUnit_Framework_TestCase
public function testBind() public function testBind()
{ {
$controller = new Controller(new Route('/foo')); $controller = new Controller(new Route('/foo'));
$controller->bind('foo'); $ret = $controller->bind('foo');
$this->assertSame($ret, $controller);
$this->assertEquals('foo', $controller->getRouteName()); $this->assertEquals('foo', $controller->getRouteName());
} }
...@@ -38,4 +40,13 @@ class ControllerTest extends \PHPUnit_Framework_TestCase ...@@ -38,4 +40,13 @@ class ControllerTest extends \PHPUnit_Framework_TestCase
$controller->freeze(); $controller->freeze();
$controller->bind('bar'); $controller->bind('bar');
} }
public function testAssert()
{
$controller = new Controller(new Route('/foo/{bar}'));
$ret = $controller->assert('bar', '\d+');
$this->assertSame($ret, $controller);
$this->assertEquals(array('bar' => '\d+'), $controller->getRoute()->getRequirements());
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment