From f85dbb9240a7f45b7e354bc46e691b36660e5701 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Fri, 28 Jun 2024 23:44:17 +0300 Subject: [PATCH] Added sorting option for `Translator` package --- config/public.php | 3 +++ src/Data/Shared/Translators/TranslatorData.php | 1 + src/Services/Config.php | 14 +++++++++----- tests/Unit/SharedWithEnvTest.php | 6 ++++++ tests/Unit/SharedWithoutEnvTest.php | 3 +++ 5 files changed, 22 insertions(+), 5 deletions(-) diff --git a/config/public.php b/config/public.php index 3274c4e..b37eef1 100644 --- a/config/public.php +++ b/config/public.php @@ -186,12 +186,14 @@ 'translator' => '\LaravelLang\Translator\Integrations\Google', 'enabled' => (bool) env('TRANSLATION_GOOGLE_ENABLED', true), + 'order' => (int) env('TRANSLATION_GOOGLE_ORDER', 1), ], 'deepl' => [ 'translator' => '\LaravelLang\Translator\Integrations\Deepl', 'enabled' => (bool) env('TRANSLATION_DEEPL_ENABLED', false), + 'order' => (int) env('TRANSLATION_DEEPL_ORDER', 2), 'credentials' => [ 'key' => env('TRANSLATION_DEEPL_KEY'), @@ -202,6 +204,7 @@ 'translator' => '\LaravelLang\Translator\Integrations\Yandex', 'enabled' => (bool) env('TRANSLATION_YANDEX_ENABLED', false), + 'order' => (int) env('TRANSLATION_YANDEX_ORDER', 3), 'credentials' => [ 'key' => env('TRANSLATION_YANDEX_KEY'), diff --git a/src/Data/Shared/Translators/TranslatorData.php b/src/Data/Shared/Translators/TranslatorData.php index 153eaeb..080a3bd 100644 --- a/src/Data/Shared/Translators/TranslatorData.php +++ b/src/Data/Shared/Translators/TranslatorData.php @@ -10,5 +10,6 @@ public function __construct( public bool $enabled, public string $translator, public array $credentials = [], + public int $order = 0, ) {} } diff --git a/src/Services/Config.php b/src/Services/Config.php index 91cd7e4..38be6e4 100644 --- a/src/Services/Config.php +++ b/src/Services/Config.php @@ -5,6 +5,7 @@ namespace LaravelLang\Config\Services; use Illuminate\Config\Repository; +use Illuminate\Support\Collection; use LaravelLang\Config\Constants\RouteName; use LaravelLang\Config\Data\Common\NonPushableData; use LaravelLang\Config\Data\Common\PushableData; @@ -126,11 +127,14 @@ protected function translatorOptions(): TranslatorOptionsData protected function getTranslators(): array { - return array_map(fn (array $item) => new TranslatorData( - enabled : $item['enabled'] ?? true, - translator : $item['translator'], - credentials: $item['credentials'] ?? [] - ), $this->value(Name::Shared, 'translators.channels')); + return (new Collection($this->value(Name::Shared, 'translators.channels')))->map( + fn (array $item) => new TranslatorData( + enabled : $item['enabled'] ?? true, + translator : $item['translator'], + credentials: $item['credentials'] ?? [], + order : $item['order'] ?? 0 + ) + )->sortBy(fn (TranslatorData $item) => $item->order)->all(); } protected function value( diff --git a/tests/Unit/SharedWithEnvTest.php b/tests/Unit/SharedWithEnvTest.php index 36b5679..c39b635 100644 --- a/tests/Unit/SharedWithEnvTest.php +++ b/tests/Unit/SharedWithEnvTest.php @@ -13,11 +13,14 @@ putenv('LOCALIZATION_SMART_ENABLED=true'); config()->set(Name::Shared() . '.translators.channels.google.enabled', false); + config()->set(Name::Shared() . '.translators.channels.google.order', 5); config()->set(Name::Shared() . '.translators.channels.deepl.enabled', true); + config()->set(Name::Shared() . '.translators.channels.deepl.order', 6); config()->set(Name::Shared() . '.translators.channels.deepl.credentials.key', 'qwerty123'); config()->set(Name::Shared() . '.translators.channels.yandex.enabled', true); + config()->set(Name::Shared() . '.translators.channels.yandex.order', 7); config()->set(Name::Shared() . '.translators.channels.yandex.credentials.key', 'qwerty456'); config()->set(Name::Shared() . '.translators.channels.yandex.credentials.folder', '123'); }); @@ -126,12 +129,14 @@ expect(Config::shared()->translators->channels->all['google']) ->toBeInstanceOf(TranslatorData::class) ->enabled->toBeFalse() + ->order->toBe(5) ->translator->toBe('\LaravelLang\Translator\Integrations\Google') ->credentials->toBeEmpty(); expect(Config::shared()->translators->channels->all['deepl']) ->toBeInstanceOf(TranslatorData::class) ->enabled->toBeTrue() + ->order->toBe(6) ->translator->toBe('\LaravelLang\Translator\Integrations\Deepl') ->credentials->toBe([ 'key' => 'qwerty123', @@ -140,6 +145,7 @@ expect(Config::shared()->translators->channels->all['yandex']) ->toBeInstanceOf(TranslatorData::class) ->enabled->toBeTrue() + ->order->toBe(7) ->translator->toBe('\LaravelLang\Translator\Integrations\Yandex') ->credentials->toBe([ 'key' => 'qwerty456', diff --git a/tests/Unit/SharedWithoutEnvTest.php b/tests/Unit/SharedWithoutEnvTest.php index 9284129..18a4f3f 100644 --- a/tests/Unit/SharedWithoutEnvTest.php +++ b/tests/Unit/SharedWithoutEnvTest.php @@ -117,12 +117,14 @@ expect(Config::shared()->translators->channels->all['google']) ->toBeInstanceOf(TranslatorData::class) ->enabled->toBeTrue() + ->order->toBe(1) ->translator->toBe('\LaravelLang\Translator\Integrations\Google') ->credentials->toBeEmpty(); expect(Config::shared()->translators->channels->all['deepl']) ->toBeInstanceOf(TranslatorData::class) ->enabled->toBeFalse() + ->order->toBe(2) ->translator->toBe('\LaravelLang\Translator\Integrations\Deepl') ->credentials->toBe([ 'key' => null, @@ -131,6 +133,7 @@ expect(Config::shared()->translators->channels->all['yandex']) ->toBeInstanceOf(TranslatorData::class) ->enabled->toBeFalse() + ->order->toBe(3) ->translator->toBe('\LaravelLang\Translator\Integrations\Yandex') ->credentials->toBe([ 'key' => null,