Skip to content

Commit

Permalink
Merge pull request #11 from Laravel-Lang/1.x
Browse files Browse the repository at this point in the history
Added helper for accessing parameter names
  • Loading branch information
andrey-helldar authored Jun 14, 2024
2 parents e02eb7d + 5e54e0b commit 2e24618
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/Concerns/KeyNames.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
namespace LaravelLang\Routes\Concerns;

use LaravelLang\Config\Data\Shared\RouteNameData;
use LaravelLang\Config\Facades\Config;
use LaravelLang\Routes\Helpers\Name;

trait KeyNames
{
public function names(): RouteNameData
{
return Config::shared()->routes->names;
return Name::all();
}
}
36 changes: 36 additions & 0 deletions src/Helpers/Name.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace LaravelLang\Routes\Helpers;

use LaravelLang\Config\Data\Shared\RouteNameData;
use LaravelLang\Config\Facades\Config;

class Name
{
public static function all(): RouteNameData
{
return Config::shared()->routes->names;
}

public static function parameter(): string
{
return static::all()->parameter;
}

public static function header(): string
{
return static::all()->header;
}

public static function cookie(): string
{
return static::all()->cookie;
}

public static function session(): string
{
return static::all()->session;
}
}
3 changes: 1 addition & 2 deletions src/Middlewares/LocalizationByParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace LaravelLang\Routes\Middlewares;

use Illuminate\Http\Request;
use LaravelLang\Config\Facades\Config;

class LocalizationByParameter extends Middleware
{
Expand All @@ -20,6 +19,6 @@ protected function detect(Request $request): bool|float|int|string|null

protected function present(Request $request): bool
{
return in_array(Config::shared()->routes->names->parameter, $request->route()->parameterNames(), true);
return in_array($this->names()->parameter, $request->route()->parameterNames(), true);
}
}
3 changes: 1 addition & 2 deletions src/Middlewares/LocalizationByParameterWithRedirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Closure;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use LaravelLang\Config\Facades\Config;
use LaravelLang\Locales\Facades\Locales;
use LaravelLang\Routes\Concerns\KeyNames;

Expand Down Expand Up @@ -47,7 +46,7 @@ protected function hasParameter(Request $request): bool

protected function present(Request $request): bool
{
return in_array(Config::shared()->routes->names->parameter, $request->route()->parameterNames(), true);
return in_array($this->names()->parameter, $request->route()->parameterNames(), true);
}

protected function parameters(Request $request): array
Expand Down
2 changes: 1 addition & 1 deletion tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

uses(Tests\TestCase::class)
->beforeEach(fn () => Event::fake())
->in('Feature');
->in('Feature', 'Unit');
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protected function defineEnvironment($app): void

$config->set('app.locale', LocaleValue::LocaleMain);

$config->set(Name::Shared->value . '.aliases', [
$config->set(Name::Shared() . '.aliases', [
LocaleValue::LocaleAliasParent => LocaleValue::LocaleAlias,
]);
}
Expand Down
30 changes: 30 additions & 0 deletions tests/Unit/Helpers/NameTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

use LaravelLang\Config\Enums\Name as NameEnum;
use LaravelLang\Routes\Helpers\Name;

test('parameter', function () {
expect(Name::parameter())->toBe(
config(NameEnum::Shared() . '.routes.names.parameter')
);
});

test('header', function () {
expect(Name::header())->toBe(
config(NameEnum::Shared() . '.routes.names.header')
);
});

test('cookie', function () {
expect(Name::cookie())->toBe(
config(NameEnum::Shared() . '.routes.names.cookie')
);
});

test('session', function () {
expect(Name::session())->toBe(
config(NameEnum::Shared() . '.routes.names.session')
);
});

0 comments on commit 2e24618

Please sign in to comment.