Skip to content

Commit

Permalink
Add "whereIn" route parameter constraint method (#41794)
Browse files Browse the repository at this point in the history
* push

* Update CreatesRegularExpressionRouteConstraints.php

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
Propaganistas and taylorotwell authored Apr 2, 2022
1 parent 88e3617 commit 0cc725b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
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 @@ -801,6 +801,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

0 comments on commit 0cc725b

Please sign in to comment.