Skip to content

Commit

Permalink
Revert "Keep null value"
Browse files Browse the repository at this point in the history
This reverts commit abecffc.
  • Loading branch information
mabdullahsari authored and freekmurze committed Dec 6, 2023
1 parent 867223c commit d6dc7c9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 36 deletions.
26 changes: 12 additions & 14 deletions src/HasTranslations.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function getTranslation(string $key, string $locale, bool $useFallbackLoc

$translations = $this->getTranslations($key);

$translation = $translations[$normalizedLocale] ?? null;
$translation = $translations[$normalizedLocale] ?? '';

$translatableConfig = app(Translatable::class);

Expand Down Expand Up @@ -101,20 +101,20 @@ public function getTranslationWithoutFallback(string $key, string $locale): mixe
return $this->getTranslation($key, $locale, false);
}

public function getTranslations(string $key = null, array $allowedLocales = null, bool $keepNullValues = true): array
public function getTranslations(string $key = null, array $allowedLocales = null): array
{
if ($key !== null) {
$this->guardAgainstNonTranslatableAttribute($key);

return array_filter(
json_decode($this->getAttributes()[$key] ?? '' ?: '{}', true) ?: [],
fn ($value, $locale) => $this->filterTranslations($value, $locale, $allowedLocales, $keepNullValues),
fn ($value, $locale) => $this->filterTranslations($value, $locale, $allowedLocales),
ARRAY_FILTER_USE_BOTH,
);
}

return array_reduce($this->getTranslatableAttributes(), function ($result, $item) use ($allowedLocales, $keepNullValues) {
$result[$item] = $this->getTranslations($item, $allowedLocales, $keepNullValues);
return array_reduce($this->getTranslatableAttributes(), function ($result, $item) use ($allowedLocales) {
$result[$item] = $this->getTranslations($item, $allowedLocales);

return $result;
});
Expand Down Expand Up @@ -204,7 +204,7 @@ public function forgetAllTranslations(string $locale): self

public function getTranslatedLocales(string $key): array
{
return array_keys($this->getTranslations($key, null, false));
return array_keys($this->getTranslations($key));
}

public function isTranslatableAttribute(string $key): bool
Expand Down Expand Up @@ -268,16 +268,14 @@ protected function normalizeLocale(string $key, string $locale, bool $useFallbac
return $locale;
}

protected function filterTranslations(mixed $value = null, string $locale = null, array $allowedLocales = null, bool $keepNullValues = true): bool
protected function filterTranslations(mixed $value = null, string $locale = null, array $allowedLocales = null): bool
{
if (! $keepNullValues) {
if ($value === null) {
return false;
}
if ($value === null) {
return false;
}

if ($value === '') {
return false;
}
if ($value === '') {
return false;
}

if ($allowedLocales === null) {
Expand Down
27 changes: 5 additions & 22 deletions tests/TranslatableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
$this->testModel->setTranslation('name', 'en', 'testValue_en');
$this->testModel->save();

expect($this->testModel->getTranslation('name', 'fr', false))->toBe(null);
expect($this->testModel->getTranslation('name', 'fr', false))->toBe('');
});

it('will return fallback locale translation when getting an unknown locale and fallback is true', function () {
Expand Down Expand Up @@ -126,7 +126,7 @@
$this->testModel->setTranslation('name', 'en', 'testValue_en');
$this->testModel->save();

expect($this->testModel->getTranslationWithoutFallback('name', 'fr'))->toBe(null);
expect($this->testModel->getTranslationWithoutFallback('name', 'fr'))->toBe('');
});

it('will return an empty string when getting an unknown locale and fallback is empty', function () {
Expand All @@ -139,7 +139,7 @@
$this->testModel->setTranslation('name', 'en', 'testValue_en');
$this->testModel->save();

expect($this->testModel->getTranslation('name', 'fr'))->toBe(null);
expect($this->testModel->getTranslation('name', 'fr'))->toBe('');
});

it('can save a translated attribute', function () {
Expand All @@ -149,13 +149,6 @@
expect($this->testModel->name)->toBe('testValue_en');
});

it('can save null value in a translated attribute', function () {
$this->testModel->setTranslation('name', 'en', null);
$this->testModel->save();

expect($this->testModel->name)->toBe(null);
});

it('can set translated values when creating a model', function () {
$model = TestModel::create([
'name' => ['en' => 'testValue_en'],
Expand Down Expand Up @@ -455,23 +448,13 @@ public function setNameAttribute($value)
it('can check if an attribute has translation', function () {
$this->testModel->setTranslation('name', 'en', 'testValue_en');
$this->testModel->setTranslation('name', 'nl', null);
$this->testModel->setTranslation('name', 'de', null);
$this->testModel->save();

expect($this->testModel->hasTranslation('name', 'en'))->toBeTrue();

expect($this->testModel->hasTranslation('name', 'pt'))->toBeFalse();
});

it('will return the same number of translations with the same values as saved', function () {
$this->testModel->setTranslation('name', 'en', 'testValue_en');
$this->testModel->setTranslation('name', 'nl', null);
$this->testModel->setTranslation('name', 'de', '');
$this->testModel->save();

expect($this->testModel->getTranslations('name'))->toEqual(['en' => 'testValue_en', 'nl' => null, 'de' => '']);
});

it('can correctly set a field when a mutator is defined', function () {
$testModel = (new class () extends TestModel {
public function setNameAttribute($value)
Expand Down Expand Up @@ -716,7 +699,7 @@ public function setAttributesExternally(array $attributes)
$this->testModel->save();

$this->testModel->setLocale('it');
expect($this->testModel->getTranslation('name', 'it', false))->toBe(null);
expect($this->testModel->getTranslation('name', 'it', false))->toBe('');
});

it('will return default fallback locale translation when getting an unknown locale with fallback any', function () {
Expand Down Expand Up @@ -777,7 +760,7 @@ public function setAttributesExternally(array $attributes)

$model->setLocale('fr');

expect($model->name)->toBe(null);
expect($model->name)->toBe('');
});

it('can set fallback locale on model', function () {
Expand Down

0 comments on commit d6dc7c9

Please sign in to comment.