|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Translation\Repositories; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Config\Repository as Config; |
|
|
|
|
|
|
6
|
|
|
use Illuminate\Foundation\Application; |
|
|
|
|
|
|
7
|
|
|
use Illuminate\Validation\Factory as Validator; |
|
8
|
|
|
use Translation\Models\Language; |
|
9
|
|
|
|
|
10
|
|
|
class LanguageRepository extends Repository |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* The model being queried. |
|
14
|
|
|
* |
|
15
|
|
|
* @var \Translation\Models\Language |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $model; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Validator |
|
21
|
|
|
* |
|
22
|
|
|
* @var \Illuminate\Validation\Validator |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $validator; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Validation errors. |
|
28
|
|
|
* |
|
29
|
|
|
* @var \Illuminate\Support\MessageBag |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $errors; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Default locale. |
|
35
|
|
|
* |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $defaultLocale; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Default available locales in case of filesystem source. |
|
42
|
|
|
* |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $defaultAvailableLocales; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Config repository. |
|
49
|
|
|
* |
|
50
|
|
|
* @var Config |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $config; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Constructor |
|
56
|
|
|
* |
|
57
|
|
|
* @param \Translation\Models\Language $model Bade model for queries. |
|
58
|
|
|
* @param \Illuminate\Validation\Validator $validator Validator factory |
|
59
|
|
|
* @return void |
|
60
|
|
|
*/ |
|
61
|
|
|
public function __construct(Language $model, Application $app) |
|
62
|
|
|
{ |
|
63
|
|
|
$this->model = $model; |
|
64
|
|
|
$this->validator = $app['validator']; |
|
65
|
|
|
$config = $app['config']; |
|
66
|
|
|
$this->defaultLocale = $config->get('app.locale'); |
|
67
|
|
|
$this->defaultAvailableLocales = $config->get('translator.available_locales', []); |
|
68
|
|
|
$this->config = $config; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Insert a new language entry into the database. |
|
73
|
|
|
* If the attributes are not valid, a null response is given and the errors can be retrieved through validationErrors() |
|
74
|
|
|
* |
|
75
|
|
|
* @param array $attributes Model attributes |
|
76
|
|
|
* @return boolean |
|
77
|
|
|
*/ |
|
78
|
|
|
public function create(array $attributes) |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->validate($attributes) ? Language::create($attributes) : null; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Insert a new language entry into the database. |
|
85
|
|
|
* If the attributes are not valid, a null response is given and the errors can be retrieved through validationErrors() |
|
86
|
|
|
* |
|
87
|
|
|
* @param array $attributes Model attributes |
|
88
|
|
|
* @return boolean |
|
89
|
|
|
*/ |
|
90
|
|
|
public function update(array $attributes) |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->validate($attributes) ? (boolean) Language::where('id', $attributes['id'])->update($attributes) : false; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Find a Language by its locale |
|
97
|
|
|
* |
|
98
|
|
|
* @return Language | null |
|
99
|
|
|
*/ |
|
100
|
|
|
public function findByLocale(string $locale) |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->model->where('locale', $locale)->first(); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Find a deleted Language by its locale |
|
107
|
|
|
* |
|
108
|
|
|
* @return Language | null |
|
109
|
|
|
*/ |
|
110
|
|
|
public function findTrashedByLocale($locale) |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->model->onlyTrashed()->where('locale', $locale)->first(); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Find all Languages except the one with the specified locale. |
|
117
|
|
|
* |
|
118
|
|
|
* @return Language | null |
|
119
|
|
|
*/ |
|
120
|
|
|
public function allExcept($locale) |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->model->where('locale', '!=', $locale)->get(); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Returns a list of all available locales. |
|
127
|
|
|
* |
|
128
|
|
|
* @return array |
|
129
|
|
|
*/ |
|
130
|
|
|
public function availableLocales() |
|
131
|
|
|
{ |
|
132
|
|
|
if ($this->config->has('translator.locales')) { |
|
133
|
|
|
return $this->config->get('translator.locales'); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
if ($this->config->get('translator.source') !== 'files') { |
|
137
|
|
|
if ($this->tableExists()) { |
|
138
|
|
|
$locales = $this->model->distinct()->get()->pluck('locale')->toArray(); |
|
139
|
|
|
$this->config->set('translator.locales', $locales); |
|
140
|
|
|
return $locales; |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
return $this->defaultAvailableLocales; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Checks if a language with the given locale exists. |
|
149
|
|
|
* |
|
150
|
|
|
* @return boolean |
|
151
|
|
|
*/ |
|
152
|
|
|
public function isValidLocale($locale) |
|
153
|
|
|
{ |
|
154
|
|
|
return $this->model->whereLocale($locale)->count() > 0; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Compute percentage translate of the given language. |
|
159
|
|
|
* |
|
160
|
|
|
* @param string $locale |
|
161
|
|
|
* @param string $referenceLocale |
|
162
|
|
|
* @return int |
|
163
|
|
|
*/ |
|
164
|
|
|
public function percentTranslated($locale) |
|
165
|
|
|
{ |
|
166
|
|
|
$lang = $this->findByLocale($locale); |
|
167
|
|
|
$referenceLang = $this->findByLocale($this->defaultLocale); |
|
168
|
|
|
|
|
169
|
|
|
$langEntries = $lang->translations()->count(); |
|
170
|
|
|
$referenceEntries = $referenceLang->translations()->count(); |
|
171
|
|
|
|
|
172
|
|
|
return $referenceEntries > 0 ? (int) round($langEntries * 100 / $referenceEntries) : 0; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Validate the given attributes |
|
177
|
|
|
* |
|
178
|
|
|
* @param array $attributes |
|
179
|
|
|
* @return boolean |
|
180
|
|
|
*/ |
|
181
|
|
|
public function validate(array $attributes) |
|
182
|
|
|
{ |
|
183
|
|
|
$id = array_get($attributes, 'id', 'NULL'); |
|
184
|
|
|
$table = $this->model->getTable(); |
|
185
|
|
|
$rules = [ |
|
186
|
|
|
'locale' => "required|unique:{$table},locale,{$id}", |
|
187
|
|
|
'name' => "required|unique:{$table},name,{$id}", |
|
188
|
|
|
]; |
|
189
|
|
|
$validator = $this->validator->make($attributes, $rules); |
|
190
|
|
|
if ($validator->fails()) { |
|
191
|
|
|
$this->errors = $validator->errors(); |
|
192
|
|
|
return false; |
|
193
|
|
|
} |
|
194
|
|
|
return true; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Returns the validations errors of the last action executed. |
|
199
|
|
|
* |
|
200
|
|
|
* @return \Illuminate\Support\MessageBag |
|
201
|
|
|
*/ |
|
202
|
|
|
public function validationErrors() |
|
203
|
|
|
{ |
|
204
|
|
|
return $this->errors; |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths