From 914bdfb29638987dcb16df61401107f4173e4506 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Sun, 13 Dec 2020 20:28:33 +0100 Subject: [PATCH] Conditional overrides --- README.md | 2 +- composer.json | 22 ++++++------ src/GlossTranslator.php | 61 ++++++++++++++++++++++++++++----- src/helpers.php | 10 +++--- tests/GlossTest.php | 74 ++++++++++++++++++++++++++++++++++++++++- 5 files changed, 143 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index e28f32c..add04a7 100644 --- a/README.md +++ b/README.md @@ -130,7 +130,7 @@ Gloss::extend('foo.pagination', fn ($value, $replace) => $replace($value, [ ':total' => ':total', ])); -Gloss::get('test.pagination', ['start' => 10, 'end' => 20, 'total' => 50]) +Gloss::get('foo.pagination', ['start' => 10, 'end' => 20, 'total' => 50]) // Showing 10 to 20 of 50 results ``` diff --git a/composer.json b/composer.json index 3e2ebf7..82b9872 100644 --- a/composer.json +++ b/composer.json @@ -16,22 +16,22 @@ } }, "require": { - "illuminate/translation": "^8.18||^6.0" + "illuminate/translation": "^8.18" }, "require-dev": { - "orchestra/testbench": "^6.4.0||^4.0.0", - "orchestra/testbench-core": "6.7.0||^4.0.0", + "orchestra/testbench": "^6.4.0", + "orchestra/testbench-core": "6.8.0", "phpunit/phpunit": "^9.5", "nunomaduro/larastan": "^0.6.11" }, -"extra": { - "laravel": { - "providers": [ - "Lean\\Gloss\\GlossServiceProviders" - ], - "aliases": { - "Gloss": "Lean\\Gloss\\Gloss" + "extra": { + "laravel": { + "providers": [ + "Lean\\Gloss\\GlossServiceProviders" + ], + "aliases": { + "Gloss": "Lean\\Gloss\\Gloss" + } } } } -} diff --git a/src/GlossTranslator.php b/src/GlossTranslator.php index c555cca..c7a2319 100644 --- a/src/GlossTranslator.php +++ b/src/GlossTranslator.php @@ -23,11 +23,21 @@ class GlossTranslator extends Translator * * @param string $shortKey * @param string $newKey + * @param array|null|callable $condition * @return void */ - public function key(string $shortKey, string $newKey) + public function key(string $shortKey, string $newKey, $condition = null) { - $this->keyOverrides[$shortKey] = $newKey; + if ($condition === null) { + $condition = fn () => true; + } elseif (!is_callable($condition)) { + $condition = fn ($data) => array_intersect_assoc($data, $condition) !== []; + } + + $this->keyOverrides[$shortKey][] = [ + 'condition' => $condition, + 'value' => $newKey, + ]; } /** @@ -35,23 +45,35 @@ class GlossTranslator extends Translator * * @param string $shortKey * @param string $value + * @param array|null|callable $condition * @return void */ - public function value(string $shortKey, string $value) + public function value(string $shortKey, string $value, $condition = null) { - $this->valueOverrides[$shortKey] = $value; + if ($condition === null) { + $condition = fn () => true; + } elseif (! is_callable($condition)) { + $condition = fn ($data) => array_intersect_assoc($data, $condition) !== []; + } + + $this->valueOverrides[$shortKey] = [ + 'condition' => $condition, + 'value' => $value, + ]; } /** * Register multiple value overrides. * * @param array $values + * @param array|null|callable $condition * @return void */ - public function values(array $values) + public function values(array $values, $condition = null) { foreach ($values as $key => $value) { - $this->valueOverrides[$key] = $value; + /** @var string $key */ + $this->value($key, $value, $condition); } } @@ -98,12 +120,33 @@ class GlossTranslator extends Translator protected function getWithoutExtensions($key, $replace = [], $locale = null, $fallback = true) { - return array_key_exists($key, $this->keyOverrides) - ? $this->get($this->keyOverrides[$key]) - : $this->valueOverrides[$key] + return $this->getKeyOverride($key, $replace) + ?? $this->getValueOverride($key, $replace) ?? parent::get($key, $replace, $locale, $fallback); } + protected function getKeyOverride(string $key, array $data) + { + if (isset($this->keyOverrides[$key])) { + foreach ($this->keyOverrides[$key] as $override) { + if ($override['condition']($data)) { + return $this->get($override['value']); + } + } + } + + return null; + } + + protected function getValueOverride(string $key, array $data) + { + if (isset($this->valueOverrides[$key]) && $this->valueOverrides[$key]['condition']($data)) { + return $this->valueOverrides[$key]['value']; + } + + return null; + } + public function choice($key, $number, array $replace = [], $locale = null) { if (array_key_exists($key, $this->extensions)) { diff --git a/src/helpers.php b/src/helpers.php index cceae5c..d6d0133 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -9,20 +9,22 @@ if (! function_exists('gloss')) { * Resolve a translation string or Gloss instance. * * @param string|array|null $key - * @param array $replace + * @param array|callable|null $replace * @param string|null $locale * @return void|string|null|\Lean\Gloss\GlossTranslator */ - function gloss($key = null, array $replace = [], string $locale = null) + function gloss($key = null, $replace = null, string $locale = null) { if (is_array($key)) { - Gloss::values($key); + [$overrides, $condition] = [$key, $replace]; + + Gloss::values($overrides, $condition); return; } if (is_string($key)) { - return Gloss::get($key, $replace, $locale); + return Gloss::get($key, (array) $replace, $locale); } return Gloss::getFacadeRoot(); diff --git a/tests/GlossTest.php b/tests/GlossTest.php index 40cb291..deb04b5 100644 --- a/tests/GlossTest.php +++ b/tests/GlossTest.php @@ -3,8 +3,8 @@ namespace Lean\Gloss\Tests; use Lean\Gloss\Gloss; -use Lean\Gloss\GlossServiceProvider; use Lean\Gloss\GlossTranslator; +use Illuminate\Support\Str; class GlossTest extends TestCase { @@ -219,6 +219,78 @@ class GlossTest extends TestCase $this->assertSame('Je tam 5 jablek', gloss()->choice('test.apples', 5)); } + /** @test */ + public function key_overrides_can_have_conditions() + { + $this->addMessages('en', 'test', [ + 'resource.create' => 'Create :Resource', + 'foo.create' => 'Foo/Create', + ]); + + Gloss::key('test.resource.create', 'test.foo.create', ['resource' => 'foo']); + + $this->assertSame('Foo/Create', Gloss::get('test.resource.create', ['resource' => 'foo'])); + $this->assertSame('Create Bar', Gloss::get('test.resource.create', ['resource' => 'bar'])); + } + + /** @test */ + public function value_overrides_can_have_conditions() + { + $this->addMessages('en', 'test', [ + 'resource.create' => 'Create :Resource', + ]); + + Gloss::value('test.resource.create', 'Bar/Create', ['resource' => 'bar']); + + $this->assertSame('Create Foo', Gloss::get('test.resource.create', ['resource' => 'foo'])); + $this->assertSame('Bar/Create', Gloss::get('test.resource.create', ['resource' => 'bar'])); + } + + /** @test */ + public function bulk_value_overrides_can_have_conditions() + { + $this->addMessages('en', 'test', [ + 'resource.create' => 'Create :Resource', + 'resource.edit' => 'Edit :Resource', + ]); + + gloss([ + 'test.resource.create' => 'Foo/Create', + 'test.resource.edit' => 'Foo/Edit', + ], ['resource' => 'foo']); + + $this->assertSame('Foo/Create', Gloss::get('test.resource.create', ['resource' => 'foo'])); + $this->assertSame('Foo/Edit', Gloss::get('test.resource.edit', ['resource' => 'foo'])); + $this->assertSame('Create Bar', Gloss::get('test.resource.create', ['resource' => 'bar'])); + $this->assertSame('Edit Bar', Gloss::get('test.resource.edit', ['resource' => 'bar'])); + } + + /** @test */ + public function custom_condition_for_key_and_value_overrides_can_be_used() + { + $this->addMessages('en', 'test', [ + 'resource.index' => 'Index of :Resource', + 'resource.create' => 'Create :Resource', + 'resource.show' => 'Detail of :Resource', + 'resource.edit' => 'Edit :Resource', + ]); + + gloss([ + 'test.resource.index' => 'custom', + 'test.resource.create' => 'custom', + ], fn ($data) => Str::startsWith($data['resource'], 'f')); + + $this->assertSame('custom', Gloss::get('test.resource.index', ['resource' => 'foo'])); + $this->assertSame('custom', Gloss::get('test.resource.create', ['resource' => 'foo'])); + $this->assertSame('Detail of Foo', Gloss::get('test.resource.show', ['resource' => 'foo'])); + $this->assertSame('Edit Foo', Gloss::get('test.resource.edit', ['resource' => 'foo'])); + + $this->assertSame('Index of Bar', Gloss::get('test.resource.index', ['resource' => 'bar'])); + $this->assertSame('Create Bar', Gloss::get('test.resource.create', ['resource' => 'bar'])); + $this->assertSame('Detail of Bar', Gloss::get('test.resource.show', ['resource' => 'bar'])); + $this->assertSame('Edit Bar', Gloss::get('test.resource.edit', ['resource' => 'bar'])); + } + protected function addMessage(string $key, string $value, string $locale = 'en', string $group = 'test', string $namespace = null): void { $this->addMessages($locale, $group, [$key => $value], $namespace);