From d3dd2b8b3a9c92f27d675177d50d2e5f7f2c8f8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gregori=20Pi=C3=B1eres?= Date: Tue, 27 Oct 2020 15:14:27 -0400 Subject: [PATCH 1/3] Add route regex registration methods --- .../Routing/PendingResourceRegistration.php | 2 +- src/Illuminate/Routing/Route.php | 2 +- .../Routing/RouteRegexConstraintTrait.php | 48 +++++++++++++++++++ tests/Routing/RouteRegistrarTest.php | 26 ++++++++++ 4 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 src/Illuminate/Routing/RouteRegexConstraintTrait.php diff --git a/src/Illuminate/Routing/PendingResourceRegistration.php b/src/Illuminate/Routing/PendingResourceRegistration.php index 5ab78b3f08da..1a7f2f8fd8c6 100644 --- a/src/Illuminate/Routing/PendingResourceRegistration.php +++ b/src/Illuminate/Routing/PendingResourceRegistration.php @@ -7,7 +7,7 @@ class PendingResourceRegistration { - use Macroable; + use Macroable, RouteRegexConstraintTrait; /** * The resource registrar. diff --git a/src/Illuminate/Routing/Route.php b/src/Illuminate/Routing/Route.php index f377e9ff8e4b..283c2d9f57e5 100755 --- a/src/Illuminate/Routing/Route.php +++ b/src/Illuminate/Routing/Route.php @@ -21,7 +21,7 @@ class Route { - use Macroable, RouteDependencyResolverTrait; + use Macroable, RouteDependencyResolverTrait, RouteRegexConstraintTrait; /** * The URI pattern the route responds to. diff --git a/src/Illuminate/Routing/RouteRegexConstraintTrait.php b/src/Illuminate/Routing/RouteRegexConstraintTrait.php new file mode 100644 index 000000000000..de7c21fd95de --- /dev/null +++ b/src/Illuminate/Routing/RouteRegexConstraintTrait.php @@ -0,0 +1,48 @@ +where([$parameter => $regex]) + : $this->applyRouteRegex($parameter, $regex); + } + } + + /** + * Set a number as regular expression requirement on the route. + * + * @param string $parameters + * @return $this + */ + public function whereNumber(...$parameters) + { + $this->applyRouteRegex($parameters, '[0-9]+'); + + return $this; + } + + /** + * Set any character between a-z or A-Z as regular expression requirement on the route. + * + * @param string|array $parameter + * @return $this + */ + public function whereString(...$parameters) + { + $this->applyRouteRegex($parameters, '[a-zA-Z]+'); + + return $this; + } +} diff --git a/tests/Routing/RouteRegistrarTest.php b/tests/Routing/RouteRegistrarTest.php index 2f97be3e03e5..1ca31a97804f 100644 --- a/tests/Routing/RouteRegistrarTest.php +++ b/tests/Routing/RouteRegistrarTest.php @@ -594,6 +594,32 @@ public function testResourceWheres() } } + public function testWhereNumberRegistration() + { + $wheres = ['foo' => '[0-9]+', 'bar' => '[0-9]+']; + + $this->router->get('/{foo}/{bar}')->whereNumber(['foo', 'bar']); + $this->router->get('/api/{bar}/{foo}')->whereNumber('bar', 'foo'); + + /** @var \Illuminate\Routing\Route $route */ + foreach ($this->router->getRoutes() as $route) { + $this->assertEquals($wheres, $route->wheres); + } + } + + public function testWhereStringRegistration() + { + $wheres = ['foo' => '[a-zA-Z]+', 'bar' => '[a-zA-Z]+']; + + $this->router->get('/{foo}/{bar}')->whereString(['foo', 'bar']); + $this->router->get('/api/{bar}/{foo}')->whereString('bar', 'foo'); + + /** @var \Illuminate\Routing\Route $route */ + foreach ($this->router->getRoutes() as $route) { + $this->assertEquals($wheres, $route->wheres); + } + } + public function testCanSetRouteName() { $this->router->as('users.index')->get('users', function () { From 6d29bc2487b5504cfa6b5d30dadb7f2f2cc798a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gregori=20Pi=C3=B1eres?= Date: Tue, 27 Oct 2020 17:36:15 -0400 Subject: [PATCH 2/3] Rename whereString to whereAlpha in RouteRegexConstraintTrait --- src/Illuminate/Routing/RouteRegexConstraintTrait.php | 2 +- tests/Routing/RouteRegistrarTest.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Routing/RouteRegexConstraintTrait.php b/src/Illuminate/Routing/RouteRegexConstraintTrait.php index de7c21fd95de..ae0f93515b5d 100644 --- a/src/Illuminate/Routing/RouteRegexConstraintTrait.php +++ b/src/Illuminate/Routing/RouteRegexConstraintTrait.php @@ -39,7 +39,7 @@ public function whereNumber(...$parameters) * @param string|array $parameter * @return $this */ - public function whereString(...$parameters) + public function whereAlpha(...$parameters) { $this->applyRouteRegex($parameters, '[a-zA-Z]+'); diff --git a/tests/Routing/RouteRegistrarTest.php b/tests/Routing/RouteRegistrarTest.php index 1ca31a97804f..851c12aee5bc 100644 --- a/tests/Routing/RouteRegistrarTest.php +++ b/tests/Routing/RouteRegistrarTest.php @@ -611,8 +611,8 @@ public function testWhereStringRegistration() { $wheres = ['foo' => '[a-zA-Z]+', 'bar' => '[a-zA-Z]+']; - $this->router->get('/{foo}/{bar}')->whereString(['foo', 'bar']); - $this->router->get('/api/{bar}/{foo}')->whereString('bar', 'foo'); + $this->router->get('/{foo}/{bar}')->whereAlpha(['foo', 'bar']); + $this->router->get('/api/{bar}/{foo}')->whereAlpha('bar', 'foo'); /** @var \Illuminate\Routing\Route $route */ foreach ($this->router->getRoutes() as $route) { From d189e6f999bda5a6d910988ea8a6a1dded0d2db7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gregori=20Pi=C3=B1eres?= Date: Tue, 27 Oct 2020 18:24:49 -0400 Subject: [PATCH 3/3] Update tests/Routing/RouteRegistrarTest.php Co-authored-by: Marek Szymczuk --- tests/Routing/RouteRegistrarTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Routing/RouteRegistrarTest.php b/tests/Routing/RouteRegistrarTest.php index 851c12aee5bc..6596ff0355dd 100644 --- a/tests/Routing/RouteRegistrarTest.php +++ b/tests/Routing/RouteRegistrarTest.php @@ -607,7 +607,7 @@ public function testWhereNumberRegistration() } } - public function testWhereStringRegistration() + public function testWhereAlphaRegistration() { $wheres = ['foo' => '[a-zA-Z]+', 'bar' => '[a-zA-Z]+'];