|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BBSLab\NovaTranslation\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; |
|
7
|
|
|
use Illuminate\Support\Facades\DB; |
|
8
|
|
|
use Illuminate\Support\Str; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @property int $id |
|
12
|
|
|
* @property string $iso |
|
13
|
|
|
* @property string $label |
|
14
|
|
|
* @property int $fallback_id |
|
15
|
|
|
* @property \BBSLab\NovaTranslation\Models\Locale $fallback |
|
16
|
|
|
* @property bool $available_in_api |
|
17
|
|
|
* @property string $flag |
|
18
|
|
|
* @property \Illuminate\Support\Carbon $created_at |
|
19
|
|
|
* @property \Illuminate\Support\Carbon $updated_at |
|
20
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder availableInApi(array $args = []) |
|
21
|
|
|
*/ |
|
22
|
|
|
class Locale extends Model |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* {@inheritdoc} |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $table = 'locales'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* {@inheritdoc} |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $fillable = [ |
|
33
|
|
|
'iso', |
|
34
|
|
|
'label', |
|
35
|
|
|
'fallback_id', |
|
36
|
|
|
'available_in_api', |
|
37
|
|
|
]; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* {@inheritdoc} |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $casts = [ |
|
43
|
|
|
'fallback_id' => 'integer', |
|
44
|
|
|
'available_in_api' => 'boolean', |
|
45
|
|
|
]; |
|
46
|
|
|
|
|
47
|
|
|
protected $appends = ['flag']; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var \Callable|null |
|
51
|
|
|
*/ |
|
52
|
|
|
public static $flagResolver; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Scope a query to only include locales available in API. |
|
56
|
|
|
* |
|
57
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
|
58
|
|
|
* @param array $args |
|
59
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
|
60
|
|
|
*/ |
|
61
|
|
|
public function scopeAvailableInApi($query, array $args = []) |
|
62
|
|
|
{ |
|
63
|
|
|
$availableInApi = isset($args['available_in_api']) ? ! empty($args['available_in_api']) : true; |
|
64
|
|
|
|
|
65
|
|
|
return $query->where('available_in_api', '=', $availableInApi); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Locale fallback relationship. |
|
70
|
|
|
* |
|
71
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
|
72
|
|
|
*/ |
|
73
|
|
|
public function fallback(): BelongsTo |
|
74
|
|
|
{ |
|
75
|
|
|
return $this->belongsTo(static::class, 'fallback_id'); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param string $iso |
|
80
|
|
|
* @return \BBSLab\NovaTranslation\Models\Locale|null |
|
81
|
|
|
*/ |
|
82
|
|
|
public static function havingIso(string $iso) |
|
83
|
|
|
{ |
|
84
|
|
|
$iso = DB::connection()->getPdo()->quote(Str::lower($iso)); |
|
85
|
|
|
|
|
86
|
|
|
return static::query()->whereRaw('LOWER(`iso`) = '.$iso)->first(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Flag attribute accessor. |
|
91
|
|
|
* |
|
92
|
|
|
* @return string|null |
|
93
|
|
|
*/ |
|
94
|
|
|
public function getFlagAttribute(): ?string |
|
95
|
|
|
{ |
|
96
|
|
|
if (! isset($this->attributes['flag'])) { |
|
97
|
|
|
$this->attributes['flag'] = static::$flagResolver |
|
98
|
|
|
? call_user_func(static::$flagResolver, $this) |
|
99
|
|
|
: $this->resolveFlag(); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return $this->attributes['flag']; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Resolve flag using default behaviour. |
|
107
|
|
|
* |
|
108
|
|
|
* @return string|null |
|
109
|
|
|
*/ |
|
110
|
|
|
protected function resolveFlag(): ?string |
|
111
|
|
|
{ |
|
112
|
|
|
$chunks = explode('-', $this->iso); |
|
113
|
|
|
$country = strtoupper(end($chunks)); |
|
114
|
|
|
|
|
115
|
|
|
return config('nova-translation.flags.'.$country) ?? config('nova-translation.flags.default'); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|