|
2 | 2 |
|
3 | 3 | declare(strict_types=1); |
4 | 4 |
|
5 | | -namespace Cone\Root\Traits; |
| 5 | +namespace Cone\Root\Models; |
6 | 6 |
|
7 | | -use Cone\Root\Models\Translation; |
8 | | -use Illuminate\Database\Eloquent\Relations\MorphMany; |
9 | | -use Illuminate\Support\Facades\App; |
| 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; |
10 | 14 |
|
11 | | -trait Translatable |
| 15 | +class Translation extends Model implements Contract |
12 | 16 | { |
| 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 | + |
13 | 39 | /** |
14 | | - * Get the translations for the model. |
| 40 | + * The table associated with the model. |
| 41 | + * |
| 42 | + * @var string |
15 | 43 | */ |
16 | | - public function translations(): MorphMany |
| 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 |
17 | 55 | { |
18 | | - return $this->morphMany(Translation::getProxiedClass(), 'translatable'); |
| 56 | + return Contract::class; |
19 | 57 | } |
20 | 58 |
|
21 | 59 | /** |
22 | | - * Translate the value of the given key. |
| 60 | + * Create a new factory instance for the model. |
23 | 61 | */ |
24 | | - public function translate(string $key, ?string $locale = null): mixed |
| 62 | + protected static function newFactory(): TranslationFactory |
25 | 63 | { |
26 | | - $locale ??= App::getLocale(); |
| 64 | + return TranslationFactory::new(); |
| 65 | + } |
27 | 66 |
|
28 | | - return match ($locale) { |
29 | | - (Translation::proxy())::getTranslatableLocale() => $this->getAttribute($key), |
30 | | - default => $this->translations->firstWhere('locale', $locale)?->values[$key] ?? $this->getAttribute($key), |
31 | | - }; |
| 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(); |
32 | 89 | } |
33 | 90 | } |
0 commit comments