From bbe9e45b78d8d7a59f79bd23011b29bd4f55feb7 Mon Sep 17 00:00:00 2001 From: Sam Feyaerts Date: Thu, 15 Feb 2024 17:11:32 +0100 Subject: [PATCH 1/2] feat: add `hasLocalized` router macro --- src/Macros/RouterMacros.php | 17 +++++++++++++++++ tests/RouteTest.php | 11 +++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/Macros/RouterMacros.php b/src/Macros/RouterMacros.php index 96f0a47..81aa69b 100644 --- a/src/Macros/RouterMacros.php +++ b/src/Macros/RouterMacros.php @@ -29,4 +29,21 @@ 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); + }; + } } diff --git a/tests/RouteTest.php b/tests/RouteTest.php index 318f73c..7b44ede 100644 --- a/tests/RouteTest.php +++ b/tests/RouteTest.php @@ -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(); From 8df9edd83fddfd67289a409bbd0fa8ff5cd35522 Mon Sep 17 00:00:00 2001 From: Sam Feyaerts Date: Thu, 15 Feb 2024 17:23:04 +0100 Subject: [PATCH 2/2] style: fix code style in `RouterMacros` --- src/Macros/RouterMacros.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Macros/RouterMacros.php b/src/Macros/RouterMacros.php index 81aa69b..a09796a 100644 --- a/src/Macros/RouterMacros.php +++ b/src/Macros/RouterMacros.php @@ -40,9 +40,10 @@ public function hasLocalized(): Closure { return function ($name) { $names = array_map( - static fn ($pattern) => locale() . ".{$pattern}", + static fn ($pattern) => locale().".{$pattern}", is_array($name) ? $name : func_get_args(), ); + return $this->has($names); }; }