Skip to content

Commit

Permalink
Merge pull request #77 from fyrts/feature/has-localized
Browse files Browse the repository at this point in the history
Add `hasLocalized` router macro
  • Loading branch information
chinleung authored Feb 26, 2024
2 parents 58e21bc + 8df9edd commit a769cd0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/Macros/RouterMacros.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,22 @@ public function multilingual(): Closure
);
};
}

/**
* Check if a route with the given name exists for the current locale.
*
* @param string|array $name
* @return \Closure
*/
public function hasLocalized(): Closure
{
return function ($name) {
$names = array_map(
static fn ($pattern) => locale().".{$pattern}",
is_array($name) ? $name : func_get_args(),
);

return $this->has($names);
};
}
}
11 changes: 11 additions & 0 deletions tests/RouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,17 @@ public function a_route_can_be_registered_with_a_middleware(): void
}
}

/** @test **/
public function a_named_route_can_be_checked_if_it_exists(): void
{
$this
->registerTestRoute()
->name('foo');

$this->assertTrue(Route::hasLocalized('foo'));
$this->assertFalse(Route::hasLocalized('bar'));
}

protected function registerTestRoute(): MultilingualRoutePendingRegistration
{
$this->registerTestTranslations();
Expand Down

0 comments on commit a769cd0

Please sign in to comment.