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