From c5f3e4c98cf408456b8c24c7abd3d75ca6b31bb9 Mon Sep 17 00:00:00 2001 From: Ostap Brehin Date: Thu, 18 Apr 2024 15:40:50 +0100 Subject: [PATCH 1/3] Add support for enums in `whereIn` route constraints --- .../CreatesRegularExpressionRouteConstraints.php | 5 ++++- tests/Routing/RouteRegistrarTest.php | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Routing/CreatesRegularExpressionRouteConstraints.php b/src/Illuminate/Routing/CreatesRegularExpressionRouteConstraints.php index e170451465c9..c6e0eb70cd01 100644 --- a/src/Illuminate/Routing/CreatesRegularExpressionRouteConstraints.php +++ b/src/Illuminate/Routing/CreatesRegularExpressionRouteConstraints.php @@ -70,7 +70,10 @@ public function whereUuid($parameters) */ public function whereIn($parameters, array $values) { - return $this->assignExpressionToParameters($parameters, implode('|', $values)); + return $this->assignExpressionToParameters($parameters, collect($values) + ->map(fn ($value) => $value instanceof \BackedEnum ? $value->value : $value) + ->implode('|') + ); } /** diff --git a/tests/Routing/RouteRegistrarTest.php b/tests/Routing/RouteRegistrarTest.php index 9171119f0741..cec7d2371131 100644 --- a/tests/Routing/RouteRegistrarTest.php +++ b/tests/Routing/RouteRegistrarTest.php @@ -12,6 +12,8 @@ use PHPUnit\Framework\TestCase; use Stringable; +include_once 'Enums.php'; + class RouteRegistrarTest extends TestCase { /** @@ -942,6 +944,19 @@ public function testWhereInRegistration() } } + public function testWhereInEnumRegistration() + { + $this->router->get('/posts/{category}')->whereIn('category', CategoryBackedEnum::cases()); + + $invalidRequest = Request::create('/posts/invalid-value', 'GET'); + $this->assertFalse($this->getRoute()->matches($invalidRequest)); + + foreach (CategoryBackedEnum::cases() as $case) { + $request = Request::create('/posts/'.$case->value, 'GET'); + $this->assertTrue($this->getRoute()->matches($request)); + } + } + public function testGroupWhereNumberRegistrationOnRouteRegistrar() { $wheres = ['foo' => '[0-9]+', 'bar' => '[0-9]+']; From 2a5872a9cc0d79feb0eedcd2ae46d03dff93d7e5 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 18 Apr 2024 09:57:28 -0500 Subject: [PATCH 2/3] Update CreatesRegularExpressionRouteConstraints.php --- .../Routing/CreatesRegularExpressionRouteConstraints.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Routing/CreatesRegularExpressionRouteConstraints.php b/src/Illuminate/Routing/CreatesRegularExpressionRouteConstraints.php index c6e0eb70cd01..3957f964851c 100644 --- a/src/Illuminate/Routing/CreatesRegularExpressionRouteConstraints.php +++ b/src/Illuminate/Routing/CreatesRegularExpressionRouteConstraints.php @@ -2,6 +2,7 @@ namespace Illuminate\Routing; +use BackedEnum; use Illuminate\Support\Arr; trait CreatesRegularExpressionRouteConstraints @@ -71,7 +72,7 @@ public function whereUuid($parameters) public function whereIn($parameters, array $values) { return $this->assignExpressionToParameters($parameters, collect($values) - ->map(fn ($value) => $value instanceof \BackedEnum ? $value->value : $value) + ->map(fn ($value) => $value instanceof BackedEnum ? $value->value : $value) ->implode('|') ); } From 00f1b6f2ea5f99aca1286134033977c628fa2013 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 18 Apr 2024 09:57:54 -0500 Subject: [PATCH 3/3] Update CreatesRegularExpressionRouteConstraints.php --- .../Routing/CreatesRegularExpressionRouteConstraints.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Routing/CreatesRegularExpressionRouteConstraints.php b/src/Illuminate/Routing/CreatesRegularExpressionRouteConstraints.php index 3957f964851c..39744e9178bb 100644 --- a/src/Illuminate/Routing/CreatesRegularExpressionRouteConstraints.php +++ b/src/Illuminate/Routing/CreatesRegularExpressionRouteConstraints.php @@ -71,9 +71,11 @@ public function whereUuid($parameters) */ public function whereIn($parameters, array $values) { - return $this->assignExpressionToParameters($parameters, collect($values) - ->map(fn ($value) => $value instanceof BackedEnum ? $value->value : $value) - ->implode('|') + return $this->assignExpressionToParameters( + $parameters, + collect($values) + ->map(fn ($value) => $value instanceof BackedEnum ? $value->value : $value) + ->implode('|') ); }