Skip to content

Commit

Permalink
feat: add hasLocalized router macro
Browse files Browse the repository at this point in the history
  • Loading branch information
fyrts committed Feb 15, 2024
1 parent 6f4aac7 commit bbe9e45
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Macros/RouterMacros.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
}
}
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 bbe9e45

Please sign in to comment.