1 | <?php |
||||
2 | |||||
3 | namespace Orkhanahmadov\LaravelAcceptLanguageMiddleware; |
||||
4 | |||||
5 | use Closure; |
||||
6 | use Illuminate\Http\Request; |
||||
7 | use Illuminate\Support\Collection; |
||||
8 | |||||
9 | class Middleware |
||||
10 | { |
||||
11 | /** |
||||
12 | * Handle an incoming request. |
||||
13 | * |
||||
14 | * @param Request $request |
||||
15 | * @param \Closure $next |
||||
16 | * @return mixed |
||||
17 | */ |
||||
18 | public function handle($request, Closure $next) |
||||
19 | { |
||||
20 | if ($locale = $this->parseHttpLocale($request)) { |
||||
21 | app()->setLocale($locale); |
||||
0 ignored issues
–
show
introduced
by
![]() |
|||||
22 | } |
||||
23 | |||||
24 | return $next($request); |
||||
25 | } |
||||
26 | |||||
27 | private function parseHttpLocale(Request $request): string |
||||
28 | { |
||||
29 | $list = explode(',', $request->server('HTTP_ACCEPT_LANGUAGE', '')); |
||||
0 ignored issues
–
show
It seems like
$request->server('HTTP_ACCEPT_LANGUAGE', '') can also be of type array and null ; however, parameter $string of explode() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
30 | |||||
31 | $locales = Collection::make($list) |
||||
0 ignored issues
–
show
$list of type string[] is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $items of Illuminate\Support\Collection::make() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
32 | ->map(function ($locale) { |
||||
33 | $parts = explode(';', $locale); |
||||
34 | |||||
35 | $mapping['locale'] = trim($parts[0]); |
||||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||||
36 | |||||
37 | if (isset($parts[1])) { |
||||
38 | $factorParts = explode('=', $parts[1]); |
||||
39 | |||||
40 | $mapping['factor'] = $factorParts[1]; |
||||
41 | } else { |
||||
42 | $mapping['factor'] = 1; |
||||
43 | } |
||||
44 | |||||
45 | return $mapping; |
||||
46 | }) |
||||
47 | ->sortByDesc(function ($locale) { |
||||
48 | return $locale['factor']; |
||||
49 | }); |
||||
50 | |||||
51 | return $locales->first()['locale']; |
||||
52 | } |
||||
53 | } |
||||
54 |