1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arcanedev\Agent\Detectors; |
6
|
|
|
|
7
|
|
|
use Arcanedev\Agent\Contracts\Detector; |
8
|
|
|
use Illuminate\Http\Request; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class LanguageDetector |
12
|
|
|
* |
13
|
|
|
* @package Arcanedev\Agent\Detectors |
14
|
|
|
* @author ARCANEDEV <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class LanguageDetector implements Detector |
17
|
|
|
{ |
18
|
|
|
/* ----------------------------------------------------------------- |
19
|
|
|
| Properties |
20
|
|
|
| ----------------------------------------------------------------- |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $languages; |
27
|
|
|
|
28
|
|
|
/* ----------------------------------------------------------------- |
29
|
|
|
| Getters & Setters |
30
|
|
|
| ----------------------------------------------------------------- |
31
|
|
|
*/ |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Get the languages. |
35
|
|
|
* |
36
|
|
|
* @return array |
37
|
|
|
*/ |
38
|
18 |
|
public function languages(): array |
39
|
|
|
{ |
40
|
18 |
|
return $this->languages; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get the languages keys. |
45
|
|
|
* |
46
|
|
|
* @return array |
47
|
|
|
*/ |
48
|
18 |
|
public function keys(): array |
49
|
|
|
{ |
50
|
18 |
|
return array_keys($this->languages()); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/* ----------------------------------------------------------------- |
54
|
|
|
| Main Methods |
55
|
|
|
| ----------------------------------------------------------------- |
56
|
|
|
*/ |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Handle the given request. |
60
|
|
|
* |
61
|
|
|
* @param \Illuminate\Http\Request $request |
62
|
|
|
* |
63
|
|
|
* @return $this |
64
|
|
|
*/ |
65
|
372 |
|
public function handle(Request $request): Detector |
66
|
|
|
{ |
67
|
372 |
|
$this->languages = []; |
68
|
372 |
|
$acceptLanguage = $request->server('HTTP_ACCEPT_LANGUAGE'); |
69
|
|
|
|
70
|
372 |
|
if ( ! empty($acceptLanguage)) { |
71
|
372 |
|
$this->parse($acceptLanguage); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
372 |
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/* ----------------------------------------------------------------- |
78
|
|
|
| Other Methods |
79
|
|
|
| ----------------------------------------------------------------- |
80
|
|
|
*/ |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Parse the accept language. |
84
|
|
|
* |
85
|
|
|
* @param string $acceptLanguage |
86
|
|
|
*/ |
87
|
372 |
|
protected function parse(string $acceptLanguage): void |
88
|
|
|
{ |
89
|
|
|
// Parse accept language string. |
90
|
372 |
|
foreach (explode(',', $acceptLanguage) as $piece) { |
91
|
372 |
|
$parts = explode(';', $piece); |
92
|
372 |
|
$language = strtolower($parts[0]); |
93
|
372 |
|
$priority = empty($parts[1]) ? 1. : floatval(str_replace('q=', '', $parts[1])); |
94
|
372 |
|
$this->languages[$language] = $priority; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
// Sort languages by priority. |
98
|
372 |
|
arsort($this->languages); |
99
|
372 |
|
} |
100
|
|
|
} |
101
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.