From c2df0d5faddeb7e58d1832c1c1f0f309619969af Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 28 Oct 2020 08:08:07 -0500 Subject: [PATCH] formatting --- ...eatesRegularExpressionRouteConstraints.php | 56 +++++++++++++++++++ .../Routing/PendingResourceRegistration.php | 2 +- src/Illuminate/Routing/Route.php | 2 +- .../Routing/RouteRegexConstraintTrait.php | 48 ---------------- tests/Routing/RouteRegistrarTest.php | 4 +- 5 files changed, 60 insertions(+), 52 deletions(-) create mode 100644 src/Illuminate/Routing/CreatesRegularExpressionRouteConstraints.php delete mode 100644 src/Illuminate/Routing/RouteRegexConstraintTrait.php diff --git a/src/Illuminate/Routing/CreatesRegularExpressionRouteConstraints.php b/src/Illuminate/Routing/CreatesRegularExpressionRouteConstraints.php new file mode 100644 index 000000000000..b7c0f1749184 --- /dev/null +++ b/src/Illuminate/Routing/CreatesRegularExpressionRouteConstraints.php @@ -0,0 +1,56 @@ +assignExpressionToParameters($parameters, '[0-9]+'); + } + + /** + * Specify that the given route parameters must be alphabetic. + * + * @param array|string $parameters + * @return $this + */ + public function whereAlpha($parameters) + { + return $this->assignExpressionToParameters($parameters, '[a-zA-Z]+'); + } + + /** + * Specify that the given route parameters must be UUIDs. + * + * @param array|string $parameters + * @return $this + */ + public function whereUuid($parameters) + { + return $this->assignExpressionToParameters($parameters, '[\da-fA-F]{8}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{12}'); + } + + /** + * Apply the given regular expression to the given parameters. + * + * @param array|string $parameters + * @param string $expression + * @return $this + */ + protected function assignExpressionToParameters($parameters, $expression) + { + return $this->where(collect(Arr::wrap($parameters)) + ->mapWithKeys(function ($parameter) use ($expression) { + return [$parameter => $expression]; + })->all()); + } +} diff --git a/src/Illuminate/Routing/PendingResourceRegistration.php b/src/Illuminate/Routing/PendingResourceRegistration.php index 1a7f2f8fd8c6..3b6c97e2042d 100644 --- a/src/Illuminate/Routing/PendingResourceRegistration.php +++ b/src/Illuminate/Routing/PendingResourceRegistration.php @@ -7,7 +7,7 @@ class PendingResourceRegistration { - use Macroable, RouteRegexConstraintTrait; + use CreatesRegularExpressionRouteConstraints, Macroable; /** * The resource registrar. diff --git a/src/Illuminate/Routing/Route.php b/src/Illuminate/Routing/Route.php index 283c2d9f57e5..29df0bfefd8c 100755 --- a/src/Illuminate/Routing/Route.php +++ b/src/Illuminate/Routing/Route.php @@ -21,7 +21,7 @@ class Route { - use Macroable, RouteDependencyResolverTrait, RouteRegexConstraintTrait; + use CreatesRegularExpressionRouteConstraints, Macroable, RouteDependencyResolverTrait; /** * The URI pattern the route responds to. diff --git a/src/Illuminate/Routing/RouteRegexConstraintTrait.php b/src/Illuminate/Routing/RouteRegexConstraintTrait.php deleted file mode 100644 index ae0f93515b5d..000000000000 --- a/src/Illuminate/Routing/RouteRegexConstraintTrait.php +++ /dev/null @@ -1,48 +0,0 @@ -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 whereAlpha(...$parameters) - { - $this->applyRouteRegex($parameters, '[a-zA-Z]+'); - - return $this; - } -} diff --git a/tests/Routing/RouteRegistrarTest.php b/tests/Routing/RouteRegistrarTest.php index 6596ff0355dd..4129b0de4f78 100644 --- a/tests/Routing/RouteRegistrarTest.php +++ b/tests/Routing/RouteRegistrarTest.php @@ -599,7 +599,7 @@ 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'); + $this->router->get('/api/{bar}/{foo}')->whereNumber(['bar', 'foo']); /** @var \Illuminate\Routing\Route $route */ foreach ($this->router->getRoutes() as $route) { @@ -612,7 +612,7 @@ public function testWhereAlphaRegistration() $wheres = ['foo' => '[a-zA-Z]+', 'bar' => '[a-zA-Z]+']; $this->router->get('/{foo}/{bar}')->whereAlpha(['foo', 'bar']); - $this->router->get('/api/{bar}/{foo}')->whereAlpha('bar', 'foo'); + $this->router->get('/api/{bar}/{foo}')->whereAlpha(['bar', 'foo']); /** @var \Illuminate\Routing\Route $route */ foreach ($this->router->getRoutes() as $route) {