1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PWWEB\Localisation\Repositories; |
4
|
|
|
|
5
|
|
|
use PWWEB\Core\Repositories\BaseRepository; |
6
|
|
|
use PWWEB\Localisation\Contracts\Language as LanguageContract; |
7
|
|
|
use PWWEB\Localisation\Exceptions\LanguageDoesNotExist; |
8
|
|
|
use PWWEB\Localisation\Models\Language; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* PWWEB\Localisation\Repositories\LanguageRepository LanguageRepository. |
12
|
|
|
* |
13
|
|
|
* The repository for Language. |
14
|
|
|
* Class LanguageRepository |
15
|
|
|
* |
16
|
|
|
* @author Frank Pillukeit <[email protected]> |
17
|
|
|
* @author Richard Browne <[email protected] |
18
|
|
|
* @copyright 2020 pw-websolutions.com |
19
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
20
|
|
|
*/ |
21
|
|
|
class LanguageRepository extends BaseRepository |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Fields that can be searched by. |
25
|
|
|
* |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $fieldSearchable = [ |
29
|
|
|
'name', |
30
|
|
|
'locale', |
31
|
|
|
'abbreviation', |
32
|
|
|
'installed', |
33
|
|
|
'active', |
34
|
|
|
'standard', |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Return searchable fields. |
39
|
|
|
* |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
|
|
public function getFieldsSearchable() |
43
|
|
|
{ |
44
|
|
|
return $this->fieldSearchable; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Configure the Model. |
49
|
|
|
* |
50
|
|
|
* @return string |
51
|
|
|
**/ |
52
|
|
|
public function model() |
53
|
|
|
{ |
54
|
|
|
return config('pwweb.localisation.models.language'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// |
58
|
|
|
// /** |
59
|
|
|
// * Get the current cached languages. |
60
|
|
|
// * |
61
|
|
|
// * @param array $params additional parameters for the database query |
62
|
|
|
// * |
63
|
|
|
// * @return \Illuminate\Database\Eloquent\Collection collection of languages |
64
|
|
|
// */ |
65
|
|
|
// protected static function get(array $params = []): Collection |
66
|
|
|
// { |
67
|
|
|
// returrn parent::get($params); |
68
|
|
|
// // Cached: return app(LocalisationRegistrar::class)->setLanguageClass(static::class)->getLanguages($params); |
69
|
|
|
// } |
70
|
|
|
// |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Retrieve all active languages. |
74
|
|
|
* |
75
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
76
|
|
|
*/ |
77
|
|
|
public function getAllActive() |
78
|
|
|
{ |
79
|
|
|
return $this->model->where('active', 1)->get(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Retrieve active language based on locale. |
84
|
|
|
* |
85
|
|
|
* @param string $locale The locale to check. |
86
|
|
|
* |
87
|
|
|
* @return \Illuminate\Database\Eloquent\Model|null |
88
|
|
|
*/ |
89
|
|
|
public function isLocaleActive(string $locale) |
90
|
|
|
{ |
91
|
|
|
return $this->model->where('active', 1)->where('locale', $locale)->first(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Retrieve active language based on lang. |
96
|
|
|
* |
97
|
|
|
* @param string $lang The language to check. |
98
|
|
|
* |
99
|
|
|
* @return \Illuminate\Database\Eloquent\Model|null |
100
|
|
|
*/ |
101
|
|
|
public function isLangActive(string $lang) |
102
|
|
|
{ |
103
|
|
|
return $this->model->where('active', 1)->where('abbreviation', $lang)->first(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// |
107
|
|
|
// /** |
108
|
|
|
// * Find a language by its name. |
109
|
|
|
// * |
110
|
|
|
// * @param string $name language name to be used to retrieve the language |
111
|
|
|
// * |
112
|
|
|
// * @throws \PWWEB\Localisation\Exceptions\LanguageDoesNotExist |
113
|
|
|
// * |
114
|
|
|
// * @return \PWWEB\Localisation\Contracts\Language |
115
|
|
|
// */ |
116
|
|
|
// public static function findByName(string $name): LanguageContract |
117
|
|
|
// { |
118
|
|
|
// $language = static::getLanguages(['name' => $name])->first(); |
119
|
|
|
// |
120
|
|
|
// if (null === $language) { |
121
|
|
|
// throw LanguageDoesNotExist::create($name); |
122
|
|
|
// } |
123
|
|
|
// |
124
|
|
|
// return $language; |
125
|
|
|
// } |
126
|
|
|
// |
127
|
|
|
// /** |
128
|
|
|
// * Find a language by its id. |
129
|
|
|
// * |
130
|
|
|
// * @param int $id ID to be used to retrieve the language |
131
|
|
|
// * |
132
|
|
|
// * @throws \PWWEB\Localisation\Exceptions\LanguageDoesNotExist |
133
|
|
|
// * |
134
|
|
|
// * @return \PWWEB\Localisation\Contracts\Language |
135
|
|
|
// */ |
136
|
|
|
// public static function findById(int $id): LanguageContract |
137
|
|
|
// { |
138
|
|
|
// $language = static::getLanguages(['id' => $id])->first(); |
139
|
|
|
// |
140
|
|
|
// if (null === $language) { |
141
|
|
|
// throw LanguageDoesNotExist::withId($id); |
142
|
|
|
// } |
143
|
|
|
// |
144
|
|
|
// return $language; |
145
|
|
|
// } |
146
|
|
|
// |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Find a language by its locale, e.g. en-gb. |
150
|
|
|
* |
151
|
|
|
* @param string $locale locale to be used to retrieve the language |
152
|
|
|
* |
153
|
|
|
* @throws \PWWEB\Localisation\Exceptions\LanguageDoesNotExist |
154
|
|
|
* |
155
|
|
|
* @return \Illuminate\Database\Eloquent\Model |
156
|
|
|
*/ |
157
|
|
|
public function findByLocale(string $locale) |
158
|
|
|
{ |
159
|
|
|
$language = $this->model->where(['locale' => $locale])->first(); |
160
|
|
|
|
161
|
|
|
if (null === $language) { |
162
|
|
|
throw LanguageDoesNotExist::create($locale); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return $language; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
// |
169
|
|
|
// /** |
170
|
|
|
// * Obtain the available locales. |
171
|
|
|
// * |
172
|
|
|
// * @param array $params Set of additional params for querying. |
173
|
|
|
// * |
174
|
|
|
// * @return array |
175
|
|
|
// */ |
176
|
|
|
// protected static function getLocales(array $params = []): array |
177
|
|
|
// { |
178
|
|
|
// $locales = []; |
179
|
|
|
// $languages = self::getLanguages($params); |
180
|
|
|
// |
181
|
|
|
// foreach ($languages as $language) { |
182
|
|
|
// $locales[] = $language->locale; |
183
|
|
|
// } |
184
|
|
|
// |
185
|
|
|
// return $locales; |
186
|
|
|
// } |
187
|
|
|
} |
188
|
|
|
|