From 03a1a712be2653263a0b2cf13dc86b2d8f1b00bc Mon Sep 17 00:00:00 2001 From: Propaganistas Date: Sat, 2 Apr 2022 12:26:33 +0200 Subject: [PATCH 1/2] push --- .../CreatesRegularExpressionRouteConstraints.php | 12 ++++++++++++ tests/Routing/RouteRegistrarTest.php | 13 +++++++++++++ tests/Routing/RoutingRouteTest.php | 14 ++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/src/Illuminate/Routing/CreatesRegularExpressionRouteConstraints.php b/src/Illuminate/Routing/CreatesRegularExpressionRouteConstraints.php index 31b0f7166d04..a46cc4fbeefe 100644 --- a/src/Illuminate/Routing/CreatesRegularExpressionRouteConstraints.php +++ b/src/Illuminate/Routing/CreatesRegularExpressionRouteConstraints.php @@ -28,6 +28,18 @@ public function whereAlphaNumeric($parameters) return $this->assignExpressionToParameters($parameters, '[a-zA-Z0-9]+'); } + /** + * Specify that the given route parameters must be one of the given values. + * + * @param array|string $parameters + * @param array $values + * @return $this + */ + public function whereIn($parameters, array $values) + { + return $this->assignExpressionToParameters($parameters, implode('|', $values)); + } + /** * Specify that the given route parameters must be numeric. * diff --git a/tests/Routing/RouteRegistrarTest.php b/tests/Routing/RouteRegistrarTest.php index d4182d9c4ef3..d94d3011b985 100644 --- a/tests/Routing/RouteRegistrarTest.php +++ b/tests/Routing/RouteRegistrarTest.php @@ -851,6 +851,19 @@ public function testWhereAlphaNumericRegistration() } } + public function testWhereInRegistration() + { + $wheres = ['foo' => 'one|two', 'bar' => 'one|two']; + + $this->router->get('/{foo}/{bar}')->whereIn(['foo', 'bar'], ['one', 'two']); + $this->router->get('/api/{bar}/{foo}')->whereIn(['bar', 'foo'], ['one', 'two']); + + /** @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 () { diff --git a/tests/Routing/RoutingRouteTest.php b/tests/Routing/RoutingRouteTest.php index 57dc86d81200..ee95b34b32a4 100644 --- a/tests/Routing/RoutingRouteTest.php +++ b/tests/Routing/RoutingRouteTest.php @@ -800,6 +800,20 @@ public function testWherePatternsProperlyFilter() $route->where('bar', '[0-9]+'); $this->assertFalse($route->matches($request)); + $request = Request::create('foo/123', 'GET'); + $route = new Route('GET', 'foo/{bar}', ['where' => ['bar' => '123|456'], function () { + // + }]); + $route->where('bar', '123|456'); + $this->assertTrue($route->matches($request)); + + $request = Request::create('foo/123abc', 'GET'); + $route = new Route('GET', 'foo/{bar}', ['where' => ['bar' => '123|456'], function () { + // + }]); + $route->where('bar', '123|456'); + $this->assertFalse($route->matches($request)); + /* * Optional */ From 3e68e0cbae78e7d24a4609a887857b67a1aadddf Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Sat, 2 Apr 2022 10:31:55 -0500 Subject: [PATCH 2/2] Update CreatesRegularExpressionRouteConstraints.php --- ...eatesRegularExpressionRouteConstraints.php | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Illuminate/Routing/CreatesRegularExpressionRouteConstraints.php b/src/Illuminate/Routing/CreatesRegularExpressionRouteConstraints.php index a46cc4fbeefe..a7845e6cd41d 100644 --- a/src/Illuminate/Routing/CreatesRegularExpressionRouteConstraints.php +++ b/src/Illuminate/Routing/CreatesRegularExpressionRouteConstraints.php @@ -29,37 +29,37 @@ public function whereAlphaNumeric($parameters) } /** - * Specify that the given route parameters must be one of the given values. + * Specify that the given route parameters must be numeric. * * @param array|string $parameters - * @param array $values * @return $this */ - public function whereIn($parameters, array $values) + public function whereNumber($parameters) { - return $this->assignExpressionToParameters($parameters, implode('|', $values)); + return $this->assignExpressionToParameters($parameters, '[0-9]+'); } /** - * Specify that the given route parameters must be numeric. + * Specify that the given route parameters must be UUIDs. * * @param array|string $parameters * @return $this */ - public function whereNumber($parameters) + public function whereUuid($parameters) { - return $this->assignExpressionToParameters($parameters, '[0-9]+'); + return $this->assignExpressionToParameters($parameters, '[\da-fA-F]{8}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{12}'); } /** - * Specify that the given route parameters must be UUIDs. + * Specify that the given route parameters must be one of the given values. * * @param array|string $parameters + * @param array $values * @return $this */ - public function whereUuid($parameters) + public function whereIn($parameters, array $values) { - return $this->assignExpressionToParameters($parameters, '[\da-fA-F]{8}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{12}'); + return $this->assignExpressionToParameters($parameters, implode('|', $values)); } /**