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

Add hasLocalized router macro #77

Merged
merged 2 commits into from
Feb 26, 2024
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
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
Loading