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

Added helper for accessing parameter names #11

Merged
merged 1 commit into from
Jun 14, 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
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')
);
});