Skip to content

Commit

Permalink
Added support for constraining in a locale
Browse files Browse the repository at this point in the history
  • Loading branch information
chinleung committed Aug 17, 2020
1 parent 2aedb09 commit 8f2c951
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 8 deletions.
26 changes: 22 additions & 4 deletions src/MultilingualRegistrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ protected function registerRoute(string $key, $handle, string $locale, array $op
{
$route = $this->generateRoute($key, $handle, $locale, $options);

foreach (Arr::get($options, 'constraints', []) as $name => $expression) {
$route->where($name, $expression);
}
$this->applyConstraints($route, $locale, $options);

if ($prefix = $this->generatePrefixForLocale($key, $locale)) {
$route->setUri("{$prefix}/{$route->uri}");
Expand Down Expand Up @@ -110,7 +108,7 @@ protected function generateRoute(string $key, $handle, string $locale, array $op
$handle ?: '\Illuminate\Routing\ViewController'
);

if (is_null($handle)) {
if ($handle === null) {
return $route
->defaults('view', Arr::get($options, 'view', $key))
->defaults('data', Arr::get($options, 'data', []));
Expand Down Expand Up @@ -251,4 +249,24 @@ protected function shouldNotPrefixDefaultHome(string $locale): bool
return $locale == config('laravel-multilingual-routes.default')
&& ! config('laravel-multilingual-routes.prefix_default_home');
}

/**
* Apply the constraints of a route.
*
* @param \Illuminate\Routing\Route $route
* @param string $locale
* @param array $options
* @return void
*/
protected function applyConstraints(Route $route, string $locale, $options): void
{
$constraints = array_merge(
Arr::get($options, 'constraints', []),
Arr::get($options, "constraints-{$locale}", [])
);

foreach ($constraints as $name => $expression) {
$route->where($name, $expression);
}
}
}
11 changes: 7 additions & 4 deletions src/MultilingualRoutePendingRegistration.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,15 +184,18 @@ public function only($locales): self
*
* @param array|string $name
* @param string|null $expression
* @param string|null $locale
* @return $this
*/
public function where($name, $expression = null): self
public function where($name, $expression = null, string $locale = null): self
{
if (! is_array(Arr::get($this->options, 'constraints'))) {
Arr::set($this->options, 'constraints', []);
$key = rtrim("constraints-{$locale}", '-');

if (! is_array(Arr::get($this->options, $key))) {
Arr::set($this->options, $key, []);
}

Arr::set($this->options, "constraints.$name", $expression);
Arr::set($this->options, "{$key}.{$name}", $expression);

return $this;
}
Expand Down
25 changes: 25 additions & 0 deletions tests/RouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,31 @@ public function a_route_param_can_have_constraints(): void
}
}

/** @test **/
public function a_route_param_can_have_constraints_by_locale(): void
{
$this->registerTranslations([
'en' => [
'routes.search' => 'search/{filter}/{filter2}',
],
'fr' => [
'routes.search' => 'recherche/{filter}/{filter2}',
],
]);

Route::multilingual('search')->where('filter', '.*')
->where('filter2', 'fr', 'fr')
->where('filter2', 'en', 'en')
->name('search.results');

foreach (locales() as $locale) {
$route = Route::getRoutes()->getByName("{$locale}.search.results");

$this->assertEquals('.*', Arr::get($route->wheres, 'filter'));
$this->assertEquals($locale, Arr::get($route->wheres, 'filter2'));
}
}

/** @test **/
public function a_route_without_translation_will_be_registered_with_its_key(): void
{
Expand Down

0 comments on commit 8f2c951

Please sign in to comment.