Skip to content

Commit f43ef16

Browse files
committed
fixes
1 parent b6982b6 commit f43ef16

File tree

4 files changed

+32
-88
lines changed

4 files changed

+32
-88
lines changed

src/Models/Translation.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Cone\Root\Database\Factories\TranslationFactory;
88
use Cone\Root\Interfaces\Models\Translation as Contract;
99
use Cone\Root\Traits\InteractsWithProxy;
10+
use Illuminate\Database\Eloquent\Casts\AsArrayObject;
1011
use Illuminate\Database\Eloquent\Factories\HasFactory;
1112
use Illuminate\Database\Eloquent\Model;
1213
use Illuminate\Database\Eloquent\Relations\MorphTo;
@@ -22,7 +23,7 @@ class Translation extends Model implements Contract
2223
* @var array<string, string>
2324
*/
2425
protected $casts = [
25-
'values' => 'json',
26+
'values' => AsArrayObject::class,
2627
];
2728

2829
/**

src/Resources/Resource.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -356,20 +356,20 @@ public function resolveTranslationsField(Request $request): ?Translations
356356
}
357357

358358
return Translations::make()
359-
->withFields(function () use ($request): array {
360-
return $this->resolveFields($request)
361-
->translatable()
362-
->map(static function (Field $field): Field {
363-
return (clone $field)
364-
->translatable(false)
365-
->value(static function (Request $request, Translation $model) use ($field): mixed {
366-
return $model->values[$field->getModelAttribute()] ?? null;
367-
})
368-
->hydrate(static function (Request $request, Translation $model, mixed $value) use ($field): void {
369-
$model->values[$field->getModelAttribute()] = $value;
370-
});
371-
})->all();
372-
});
359+
->withFields(function () use ($request): array {
360+
return $this->resolveFields($request)
361+
->translatable()
362+
->map(static function (Field $field): Field {
363+
return (clone $field)
364+
->translatable(false)
365+
->value(static function (Request $request, Translation $model) use ($field): mixed {
366+
return $model->values[$field->getModelAttribute()] ?? null;
367+
})
368+
->hydrate(static function (Request $request, Translation $model, mixed $value) use ($field): void {
369+
$model->values[$field->getModelAttribute()] = $value;
370+
});
371+
})->all();
372+
});
373373
}
374374

375375
/**

src/Traits/Translatable.php

Lines changed: 15 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -2,89 +2,32 @@
22

33
declare(strict_types=1);
44

5-
namespace Cone\Root\Models;
5+
namespace Cone\Root\Traits;
66

7-
use Cone\Root\Database\Factories\TranslationFactory;
8-
use Cone\Root\Interfaces\Models\Translation as Contract;
9-
use Cone\Root\Traits\InteractsWithProxy;
10-
use Illuminate\Database\Eloquent\Casts\AsArrayObject;
11-
use Illuminate\Database\Eloquent\Factories\HasFactory;
12-
use Illuminate\Database\Eloquent\Model;
13-
use Illuminate\Database\Eloquent\Relations\MorphTo;
7+
use Cone\Root\Models\Translation;
8+
use Illuminate\Database\Eloquent\Relations\MorphMany;
9+
use Illuminate\Support\Facades\App;
1410

15-
class Translation extends Model implements Contract
11+
trait Translatable
1612
{
17-
use HasFactory;
18-
use InteractsWithProxy;
19-
20-
/**
21-
* The attributes that should be cast to native types.
22-
*
23-
* @var array<string, string>
24-
*/
25-
protected $casts = [
26-
'values' => AsArrayObject::class,
27-
];
28-
29-
/**
30-
* The attributes that are mass assignable.
31-
*
32-
* @var list<string>
33-
*/
34-
protected $fillable = [
35-
'locale',
36-
'values',
37-
];
38-
3913
/**
40-
* The table associated with the model.
41-
*
42-
* @var string
14+
* Get the translations for the model.
4315
*/
44-
protected $table = 'root_translations';
45-
46-
/**
47-
* The translatable model's locale.
48-
*/
49-
protected static string $translatableLocale = 'en';
50-
51-
/**
52-
* Get the proxied interface.
53-
*/
54-
public static function getProxiedInterface(): string
16+
public function translations(): MorphMany
5517
{
56-
return Contract::class;
18+
return $this->morphMany(Translation::getProxiedClass(), 'translatable');
5719
}
5820

5921
/**
60-
* Create a new factory instance for the model.
22+
* Translate the value of the given key.
6123
*/
62-
protected static function newFactory(): TranslationFactory
24+
public function translate(string $key, ?string $locale = null): mixed
6325
{
64-
return TranslationFactory::new();
65-
}
26+
$locale ??= App::getLocale();
6627

67-
/**
68-
* Set the translatable model's locale.
69-
*/
70-
public static function setTranslatableLocale(string $locale): void
71-
{
72-
static::$translatableLocale = $locale;
73-
}
74-
75-
/**
76-
* Get the translatable model's locale.
77-
*/
78-
public static function getTranslatableLocale(): string
79-
{
80-
return static::$translatableLocale;
81-
}
82-
83-
/**
84-
* Get the translatable model for the translation.
85-
*/
86-
public function translatable(): MorphTo
87-
{
88-
return $this->morphTo();
28+
return match ($locale) {
29+
(Translation::proxy())::getTranslatableLocale() => $this->getAttribute($key),
30+
default => $this->translations->firstWhere('locale', $locale)?->values[$key] ?? $this->getAttribute($key),
31+
};
8932
}
9033
}

tests/Models/TranslationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function test_a_translatable_model_attributes_can_be_translated(): void
5555
$this->user->translate('name', Translation::getTranslatableLocale())
5656
);
5757

58-
$this->assertNull(
58+
$this->assertSame(
5959
$this->user->name,
6060
$this->user->translate('name', 'fake')
6161
);

0 commit comments

Comments
 (0)