Skip to content

Commit 37eefdf

Browse files
authored
Change casts
1 parent 1ab90ac commit 37eefdf

File tree

1 file changed

+72
-15
lines changed

1 file changed

+72
-15
lines changed

src/Traits/Translatable.php

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

33
declare(strict_types=1);
44

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

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;
1014

11-
trait Translatable
15+
class Translation extends Model implements Contract
1216
{
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+
1339
/**
14-
* Get the translations for the model.
40+
* The table associated with the model.
41+
*
42+
* @var string
1543
*/
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
1755
{
18-
return $this->morphMany(Translation::getProxiedClass(), 'translatable');
56+
return Contract::class;
1957
}
2058

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

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();
3289
}
3390
}

0 commit comments

Comments
 (0)