Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Add "whereIn" route parameter constraint method #41794

Merged
merged 2 commits into from
Apr 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ 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}');
}

/**
* 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));
}

/**
* Apply the given regular expression to the given parameters.
*
Expand Down
13 changes: 13 additions & 0 deletions tests/Routing/RouteRegistrarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
14 changes: 14 additions & 0 deletions tests/Routing/RoutingRouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down