Skip to content

Commit

Permalink
Merge pull request #559 from tighten/jbk/filter-negation
Browse files Browse the repository at this point in the history
Add support for negating route filter patterns with `!`
  • Loading branch information
bakerkretzmar committed Sep 23, 2022
2 parents 9c5082e + 583e703 commit 99b7beb
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 5 deletions.
34 changes: 29 additions & 5 deletions src/Ziggy.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ private function group($group)
$filters = [];

foreach ($group as $groupName) {
$filters = array_merge($filters, config("ziggy.groups.{$groupName}"));
$filters = array_merge($filters, Arr::wrap(config("ziggy.groups.{$groupName}")));
}

return $this->filter($filters, true)->routes;
return $this->filter($filters)->routes;
}

if (config()->has("ziggy.groups.{$group}")) {
return $this->filter(config("ziggy.groups.{$group}"), true)->routes;
return $this->filter(config("ziggy.groups.{$group}"))->routes;
}

return $this->routes;
Expand All @@ -86,10 +86,34 @@ private function group($group)
*/
public function filter($filters = [], $include = true): self
{
$this->routes = $this->routes->filter(function ($route, $name) use ($filters, $include) {
return Str::is(Arr::wrap($filters), $name) ? $include : ! $include;
$filters = Arr::wrap($filters);

$reject = collect($filters)->every(function (string $pattern) {
return Str::startsWith($pattern, '!');
});

$this->routes = $reject
? $this->routes->reject(function ($route, $name) use ($filters) {
foreach ($filters as $pattern) {
if (Str::is(substr($pattern, 1), $name)) {
return true;
}
}
})
: $this->routes->filter(function ($route, $name) use ($filters, $include) {
if ($include === false) {
return ! Str::is($filters, $name);
}

foreach ($filters as $pattern) {
if (Str::startsWith($pattern, '!') && Str::is(substr($pattern, 1), $name)) {
return false;
}
}

return Str::is($filters, $name);
});

return $this;
}

Expand Down
86 changes: 86 additions & 0 deletions tests/Unit/ZiggyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,92 @@ public function can_set_included_routes_using_groups_config()
$this->assertSame($expected, $routes);
}

/** @test */
public function can_include_routes_from_multiple_groups()
{
config(['ziggy.groups' => ['home' => ['home'], 'posts' => ['posts.*']]]);
$routes = (new Ziggy(['home', 'posts']))->toArray()['routes'];

$expected = [
'home' => [
'uri' => 'home',
'methods' => ['GET', 'HEAD'],
],
'posts.index' => [
'uri' => 'posts',
'methods' => ['GET', 'HEAD'],
],
'posts.show' => [
'uri' => 'posts/{post}',
'methods' => ['GET', 'HEAD'],
],
'posts.store' => [
'uri' => 'posts',
'methods' => ['POST'],
],
];

$this->assertSame($expected, $routes);
}

/** @test */
public function can_set_excluded_routes_in_groups_using_negative_patterns()
{
config(['ziggy.groups' => ['authors' => ['!home', '!posts.*', '!postComments.*']]]);
$routes = (new Ziggy('authors'))->toArray()['routes'];

$expected = [
'admin.users.index' => [
'uri' => 'admin/users',
'methods' => ['GET', 'HEAD'],
],
];

$this->assertSame($expected, $routes);
}

/** @test */
public function can_combine_filters_in_groups_with_positive_and_negative_patterns()
{
config(['ziggy.groups' => ['authors' => ['posts.*', '!posts.index']]]);
$routes = (new Ziggy('authors'))->toArray()['routes'];

$expected = [
'posts.show' => [
'uri' => 'posts/{post}',
'methods' => ['GET', 'HEAD'],
],
'posts.store' => [
'uri' => 'posts',
'methods' => ['POST'],
],
];

$this->assertSame($expected, $routes);
}

/** @test */
public function can_filter_routes_from_multiple_groups_using_negative_patterns()
{
config(['ziggy.groups' => ['home' => '!posts.*', 'posts' => '!home']]);
$routes = (new Ziggy(['home', 'posts']))->toArray()['routes'];

$expected = [
'postComments.index' => [
'uri' => 'posts/{post}/comments',
'methods' => ['GET', 'HEAD'],
],
'admin.users.index' => [
'uri' => 'admin/users',
'methods' => ['GET', 'HEAD'],
],
];

$this->addPostCommentsRouteWithBindings($expected);

$this->assertSame($expected, $routes);
}

/** @test */
public function can_ignore_passed_group_not_set_in_config()
{
Expand Down

0 comments on commit 99b7beb

Please sign in to comment.