From 4fdc5f48751d28aed2afdede72422e51e689c00e Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Sat, 1 Jun 2024 17:24:21 +0300 Subject: [PATCH] Works with data objects --- composer.json | 13 ++++ config/public.php | 24 +++---- phpunit.xml | 17 +++++ src/Concerns/HasKey.php | 19 ++++++ src/Concerns/HasValues.php | 28 +++++++++ src/Data/HiddenData.php | 5 +- src/Data/NonPushableData.php | 23 +++++++ src/Data/PushableData.php | 23 ++++--- src/Data/SharedData.php | 5 +- src/Data/SmartPunctuationData.php | 2 +- src/Facades/Config.php | 37 +++++++---- tests/Pest.php | 3 + tests/TestCase.php | 11 ++++ .../Unit/Facades/Config/HiddenWithEnvTest.php | 49 +++++++++++++++ .../Unit/Facades/Config/SharedWithEnvTest.php | 63 +++++++++++++++++++ 15 files changed, 285 insertions(+), 37 deletions(-) create mode 100644 phpunit.xml create mode 100644 src/Concerns/HasKey.php create mode 100644 src/Concerns/HasValues.php create mode 100644 src/Data/NonPushableData.php create mode 100644 tests/Pest.php create mode 100644 tests/TestCase.php create mode 100644 tests/Unit/Facades/Config/HiddenWithEnvTest.php create mode 100644 tests/Unit/Facades/Config/SharedWithEnvTest.php diff --git a/composer.json b/composer.json index 7b0bdb3..8a8c7e7 100644 --- a/composer.json +++ b/composer.json @@ -36,6 +36,10 @@ "illuminate/support": "^10.0 || ^11.0", "laravel-lang/locale-list": "^1.4" }, + "require-dev": { + "orchestra/testbench": "^9.1", + "pestphp/pest": "^2.34" + }, "minimum-stability": "stable", "prefer-stable": true, "autoload": { @@ -43,11 +47,17 @@ "LaravelLang\\Config\\": "src/" } }, + "autoload-dev": { + "psr-4": { + "Tests\\": "tests/" + } + }, "config": { "allow-plugins": { "dragon-code/codestyler": true, "ergebnis/composer-normalize": true, "friendsofphp/php-cs-fixer": true, + "pestphp/pest-plugin": true, "symfony/thanks": true }, "preferred-install": "dist", @@ -59,5 +69,8 @@ "LaravelLang\\Config\\ServiceProvider" ] } + }, + "scripts": { + "test": "vendor/bin/pest" } } diff --git a/config/public.php b/config/public.php index 0f50bee..d805652 100644 --- a/config/public.php +++ b/config/public.php @@ -29,6 +29,18 @@ 'align' => (bool)env('LOCALIZATION_ALIGN', true), + /* + * The language codes chosen for the files in this repository may not + * match the preferences for your project. + * + * Specify here mappings of localizations with your project. + */ + + 'aliases' => [ + // \LaravelLang\LocaleList\Locale::German->value => 'de-DE', + // \LaravelLang\LocaleList\Locale::GermanSwitzerland->value => 'de-CH', + ], + /* * This option determines the mechanism for converting translation * keys into a typographic version. @@ -80,16 +92,4 @@ ], ], ], - - /* - * The language codes chosen for the files in this repository may not - * match the preferences for your project. - * - * Specify here mappings of localizations with your project. - */ - - 'aliases' => [ - // \LaravelLang\LocaleList\Locale::German->value => 'de-DE', - // \LaravelLang\LocaleList\Locale::GermanSwitzerland->value => 'de-CH', - ], ]; diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..b285193 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,17 @@ + + + + + ./tests + + + + + ./src + + + diff --git a/src/Concerns/HasKey.php b/src/Concerns/HasKey.php new file mode 100644 index 0000000..d28826d --- /dev/null +++ b/src/Concerns/HasKey.php @@ -0,0 +1,19 @@ +value ?? $key->name; + } + + return $key; + } +} diff --git a/src/Concerns/HasValues.php b/src/Concerns/HasValues.php new file mode 100644 index 0000000..35ea6ab --- /dev/null +++ b/src/Concerns/HasValues.php @@ -0,0 +1,28 @@ +default)) { + $default = $this->default . '.' . $this->resolveKey($key); + } + + return $this->value($this->key . '.' . $this->resolveKey($key), $default ?? null); + } + + protected function value(string $key, ?string $default = null): mixed + { + return $default + ? config()->get($key, config()->get($default)) + : config()->get($key); + } +} diff --git a/src/Data/HiddenData.php b/src/Data/HiddenData.php index 2a4d8a4..daaacc6 100644 --- a/src/Data/HiddenData.php +++ b/src/Data/HiddenData.php @@ -9,6 +9,7 @@ class HiddenData public function __construct( public PushableData $plugins, public PushableData $packages, - public readonly array $map, - ) {} + public NonPushableData $map, + ) { + } } diff --git a/src/Data/NonPushableData.php b/src/Data/NonPushableData.php new file mode 100644 index 0000000..68e1f82 --- /dev/null +++ b/src/Data/NonPushableData.php @@ -0,0 +1,23 @@ +key, config($this->default, [])); + } +} diff --git a/src/Data/PushableData.php b/src/Data/PushableData.php index 412b033..98a6cee 100644 --- a/src/Data/PushableData.php +++ b/src/Data/PushableData.php @@ -4,28 +4,35 @@ namespace LaravelLang\Config\Data; +use BackedEnum; +use LaravelLang\Config\Concerns\HasValues; + class PushableData { + use HasValues; + public function __construct( - public array $items - ) {} + protected readonly string $key, + protected readonly ?string $default = null + ) { + } - public function get(): array + public function all(): array { - return $this->items; + return config($this->key, config($this->default)); } public function push(mixed $value): static { - $this->items[] = $value; + config()->push($this->key, $value); return $this; } - public function set(int|string $key, mixed $value): static + public function set(int | string | BackedEnum $key, mixed $value): mixed { - $this->items[$key] = $value; + config()->set($key = $this->key . '.' . $this->resolveKey($key), $value); - return $this; + return $this->get($key); } } diff --git a/src/Data/SharedData.php b/src/Data/SharedData.php index eb636d1..d1891c6 100644 --- a/src/Data/SharedData.php +++ b/src/Data/SharedData.php @@ -9,7 +9,8 @@ class SharedData public function __construct( public bool $inline, public bool $align, - public array $aliases, + public NonPushableData $aliases, public SmartPunctuationData $smartPunctuation, - ) {} + ) { + } } diff --git a/src/Data/SmartPunctuationData.php b/src/Data/SmartPunctuationData.php index 950ccae..54ab25e 100644 --- a/src/Data/SmartPunctuationData.php +++ b/src/Data/SmartPunctuationData.php @@ -9,6 +9,6 @@ class SmartPunctuationData public function __construct( public bool $enabled, public array $common, - public array $locales, + public NonPushableData $locales, ) {} } diff --git a/src/Facades/Config.php b/src/Facades/Config.php index 567c40c..6beec47 100644 --- a/src/Facades/Config.php +++ b/src/Facades/Config.php @@ -6,6 +6,7 @@ use Illuminate\Config\Repository; use LaravelLang\Config\Data\HiddenData; +use LaravelLang\Config\Data\NonPushableData; use LaravelLang\Config\Data\PushableData; use LaravelLang\Config\Data\SharedData; use LaravelLang\Config\Data\SmartPunctuationData; @@ -18,7 +19,7 @@ public static function shared(): SharedData return new SharedData( inline: static::value(Name::Shared, 'inline'), align: static::value(Name::Shared, 'align'), - aliases: static::value(Name::Shared, 'aliases'), + aliases: static::value(Name::Shared, 'aliases', object: PushableData::class), smartPunctuation: static::smartPunctuation() ); } @@ -26,30 +27,42 @@ public static function shared(): SharedData public static function hidden(): HiddenData { return new HiddenData( - plugins: static::value(Name::Hidden, 'plugins', PushableData::class), - packages: static::value(Name::Hidden, 'packages', PushableData::class), - map: static::value(Name::Hidden, 'map'), + plugins: static::value(Name::Hidden, 'plugins', object: PushableData::class), + packages: static::value(Name::Hidden, 'packages', object: PushableData::class), + map: static::value(Name::Hidden, 'map', object: NonPushableData::class), ); } protected static function smartPunctuation(): SmartPunctuationData { return new SmartPunctuationData( - enabled: static::value(Name::Shared, 'enable'), - common: static::value(Name::Shared, 'common'), - locales: static::value(Name::Shared, 'locales'), + enabled: static::value(Name::Shared, 'smart_punctuation.enabled'), + common: static::value(Name::Shared, 'smart_punctuation.common'), + + locales: static::value( + Name::Shared, + 'smart_punctuation.locales', + 'smart_punctuation.common', + PushableData::class + ), ); } - protected static function value(Name $name, string $key, ?string $object = null): mixed + protected static function value(Name $name, string $key, ?string $default = null, ?string $object = null): mixed { - $value = static::repository($name->value . '.' . $key); + if (is_null($object)) { + return static::repository($name->value . '.' . $key, $default); + } - return is_null($object) ? $value : new $object($value); + return new $object($name->value . '.' . $key, $name->value, '.' . $default); } - protected static function repository(string $key): mixed + protected static function repository(string $key, ?string $default = null): mixed { - return app(Repository::class)->get($key); + if (!is_null($default)) { + $default = static::repository($default); + } + + return app(Repository::class)->get($key, $default); } } diff --git a/tests/Pest.php b/tests/Pest.php new file mode 100644 index 0000000..174d7fd --- /dev/null +++ b/tests/Pest.php @@ -0,0 +1,3 @@ + ['foo', 'bar']]); + + expect(Config::hidden()->plugins->all()) + ->toBeArray() + ->toBe(['foo', 'bar']) + ->toBe(config('localization-private.plugins')); + + Config::hidden()->plugins->push('baz'); + + expect(Config::hidden()->plugins->all()) + ->toBeArray() + ->toBe(['foo', 'bar', 'baz']) + ->toBe(config('localization-private.plugins')); +}); + +test('packages', function () { + config(['localization-private.packages' => ['foo', 'bar']]); + + expect(Config::hidden()->packages->all()) + ->toBeArray() + ->toBe(['foo', 'bar']) + ->toBe(config('localization-private.packages')); + + Config::hidden()->packages->push('baz'); + + expect(Config::hidden()->packages->all()) + ->toBeArray() + ->toBe(['foo', 'bar', 'baz']) + ->toBe(config('localization-private.packages')); +}); + +test('map', function () { + expect(Config::hidden()->map->all()) + ->toBeArray() + ->toBe(config('localization-private.map')); +}); diff --git a/tests/Unit/Facades/Config/SharedWithEnvTest.php b/tests/Unit/Facades/Config/SharedWithEnvTest.php new file mode 100644 index 0000000..b2c17b1 --- /dev/null +++ b/tests/Unit/Facades/Config/SharedWithEnvTest.php @@ -0,0 +1,63 @@ +inline) + ->toBeBool() + ->toBeTrue() + ->toBe('localization.inline'); +}); + +test('align', function () { + expect(Config::shared()->align) + ->toBeBool() + ->toBeFalse() + ->toBe('localization.align'); +}); + +test('aliases', function () { + expect(Config::shared()->aliases) + ->toBeArray() + ->toBe('localization.aliases'); +}); + +test('smart punctuation: enabled', function () { + expect(Config::shared()->smartPunctuation->enabled) + ->toBeBool() + ->toBeTrue() + ->toBe('localization.smart_punctuation.enabled'); +}); + +test('smart punctuation: common', function () { + expect(Config::shared()->smartPunctuation->common) + ->toBeArray() + ->toBe('localization.smart_punctuation.common'); +}); + +test('smart punctuation: locales', function () { + expect(Config::shared()->smartPunctuation->locales->all()) + ->toBeArray() + ->toBe('localization.smart_punctuation.locales'); +}); + +test('smart punctuation: get locale', function () { + expect(Config::shared()->smartPunctuation->locales->get(Locale::French)) + ->toBeArray() + ->toBe('localization.smart_punctuation.locales.' . Locale::French->value); +}); + +test('smart punctuation: get default locale', function () { + expect(Config::shared()->smartPunctuation->locales->get(Locale::Zulu)) + ->toBeArray() + ->toBe('localization.smart_punctuation.common'); +});