1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* PWWEB\Localisation\Models\Language Model. |
5
|
|
|
* |
6
|
|
|
* Standard Language Model. |
7
|
|
|
* |
8
|
|
|
* @author Frank Pillukeit <[email protected]> |
9
|
|
|
* @copyright 2020 pw-websolutions.com |
10
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace PWWEB\Localisation\Models; |
14
|
|
|
|
15
|
|
|
use Illuminate\Database\Eloquent\Collection; |
16
|
|
|
use Illuminate\Database\Eloquent\Model; |
17
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
18
|
|
|
use PWWEB\Localisation\Contracts\Language as LanguageContract; |
19
|
|
|
use PWWEB\Localisation\Exceptions\LanguageDoesNotExist; |
20
|
|
|
use PWWEB\Localisation\LocalisationRegistrar; |
21
|
|
|
|
22
|
|
|
class Language extends Model implements LanguageContract |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Constructor. |
26
|
|
|
* |
27
|
|
|
* @param array $attributes additional attributes for model initialisation |
28
|
|
|
* |
29
|
|
|
* @return void |
30
|
|
|
*/ |
31
|
|
|
public function __construct(array $attributes = []) |
32
|
|
|
{ |
33
|
|
|
parent::__construct($attributes); |
34
|
|
|
|
35
|
|
|
$this->setTable(config('pwweb.localisation.table_names.languages')); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* A language can be applied to countries. |
40
|
|
|
* |
41
|
|
|
* @return BelongsToMany countries the language belongs to |
42
|
|
|
*/ |
43
|
|
|
public function countries(): BelongsToMany |
44
|
|
|
{ |
45
|
|
|
return $this->belongsToMany( |
46
|
|
|
config('pwweb.localisation.models.countries'), |
47
|
|
|
config('pwweb.localisation.table_names.country_has_language'), |
48
|
|
|
'language_id', |
49
|
|
|
'country_id' |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Find a language by its name. |
55
|
|
|
* |
56
|
|
|
* @param string $name language name to be used to retrieve the language |
57
|
|
|
* |
58
|
|
|
* @throws \PWWEB\Localisation\Exceptions\LanguageDoesNotExist |
59
|
|
|
* |
60
|
|
|
* @return \PWWEB\Localisation\Contracts\Language |
61
|
|
|
*/ |
62
|
|
|
public static function findByName(string $name): LanguageContract |
63
|
|
|
{ |
64
|
|
|
$language = static::getLanguages(['name' => $name])->first(); |
65
|
|
|
|
66
|
|
|
if (null === $language) { |
67
|
|
|
throw LanguageDoesNotExist::create($name); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $language; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Find a language by its id. |
75
|
|
|
* |
76
|
|
|
* @param int $id ID to be used to retrieve the language |
77
|
|
|
* |
78
|
|
|
* @throws \PWWEB\Localisation\Exceptions\LanguageDoesNotExist |
79
|
|
|
* |
80
|
|
|
* @return \PWWEB\Localisation\Contracts\Language |
81
|
|
|
*/ |
82
|
|
|
public static function findById(int $id): LanguageContract |
83
|
|
|
{ |
84
|
|
|
$language = static::getLanguages(['id' => $id])->first(); |
85
|
|
|
|
86
|
|
|
if (null === $language) { |
87
|
|
|
throw LanguageDoesNotExist::withId($id); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $language; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Find a language by its locale, e.g. en-gb. |
95
|
|
|
* |
96
|
|
|
* @param string $locale locale to be used to retrieve the language |
97
|
|
|
* |
98
|
|
|
* @throws \PWWEB\Localisation\Exceptions\LanguageDoesNotExist |
99
|
|
|
* |
100
|
|
|
* @return \PWWEB\Localisation\Contracts\Language |
101
|
|
|
*/ |
102
|
|
|
public static function findByLocale(string $locale): LanguageContract |
103
|
|
|
{ |
104
|
|
|
$language = static::getLanguages(['locale' => $locale])->first(); |
105
|
|
|
|
106
|
|
|
if (null === $language) { |
107
|
|
|
throw LanguageDoesNotExist::create($locale); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $language; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get the current cached languages. |
115
|
|
|
* |
116
|
|
|
* @param array $params additional parameters for the database query |
117
|
|
|
* |
118
|
|
|
* @return Collection collection of languages |
119
|
|
|
*/ |
120
|
|
|
protected static function getLanguages(array $params = []): Collection |
121
|
|
|
{ |
122
|
|
|
return app(LocalisationRegistrar::class) |
123
|
|
|
->setLanguageClass(static::class) |
124
|
|
|
->getLanguages($params); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Obtain the available locales. |
129
|
|
|
* |
130
|
|
|
* @param array $params Set of additional params for querying. |
131
|
|
|
* |
132
|
|
|
* @return array |
133
|
|
|
*/ |
134
|
|
|
protected static function getLocales(array $params = []): array |
135
|
|
|
{ |
136
|
|
|
$locales = []; |
137
|
|
|
$languages = self::getLanguages($params); |
138
|
|
|
|
139
|
|
|
foreach ($languages as $language) { |
140
|
|
|
$locales[] = $language->locale; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $locales; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|