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
|
|
|
* @author ARCANEDEV <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class LanguageDetector implements Detector |
16
|
|
|
{ |
17
|
|
|
/* ----------------------------------------------------------------- |
18
|
|
|
| Properties |
19
|
|
|
| ----------------------------------------------------------------- |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $languages; |
26
|
|
|
|
27
|
|
|
/* ----------------------------------------------------------------- |
28
|
|
|
| Getters & Setters |
29
|
|
|
| ----------------------------------------------------------------- |
30
|
|
|
*/ |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Get the languages. |
34
|
|
|
* |
35
|
|
|
* @return array |
36
|
|
|
*/ |
37
|
18 |
|
public function languages(): array |
38
|
|
|
{ |
39
|
18 |
|
return $this->languages; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Get the languages keys. |
44
|
|
|
* |
45
|
|
|
* @return array |
46
|
|
|
*/ |
47
|
18 |
|
public function keys(): array |
48
|
|
|
{ |
49
|
18 |
|
return array_keys($this->languages()); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/* ----------------------------------------------------------------- |
53
|
|
|
| Main Methods |
54
|
|
|
| ----------------------------------------------------------------- |
55
|
|
|
*/ |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Handle the given request. |
59
|
|
|
* |
60
|
|
|
* @param \Illuminate\Http\Request $request |
61
|
|
|
* |
62
|
|
|
* @return $this |
63
|
|
|
*/ |
64
|
372 |
|
public function handle(Request $request): Detector |
65
|
|
|
{ |
66
|
372 |
|
return $this->fromAcceptLanguage( |
67
|
372 |
|
$request->server('HTTP_ACCEPT_LANGUAGE') |
|
|
|
|
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Handle from the given accept language. |
73
|
|
|
* |
74
|
|
|
* @param string $acceptLanguage |
75
|
|
|
* |
76
|
|
|
* @return $this |
77
|
|
|
*/ |
78
|
372 |
|
public function fromAcceptLanguage(string $acceptLanguage): Detector |
79
|
|
|
{ |
80
|
372 |
|
$this->languages = []; |
81
|
|
|
|
82
|
372 |
|
if ( ! empty($acceptLanguage)) { |
83
|
372 |
|
$this->parse($acceptLanguage); |
84
|
|
|
} |
85
|
|
|
|
86
|
372 |
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/* ----------------------------------------------------------------- |
90
|
|
|
| Other Methods |
91
|
|
|
| ----------------------------------------------------------------- |
92
|
|
|
*/ |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Parse the accept language. |
96
|
|
|
* |
97
|
|
|
* @param string $acceptLanguage |
98
|
|
|
*/ |
99
|
372 |
|
protected function parse(string $acceptLanguage): void |
100
|
|
|
{ |
101
|
|
|
// Parse accept language string. |
102
|
372 |
|
foreach (explode(',', $acceptLanguage) as $piece) { |
103
|
372 |
|
$parts = explode(';', $piece); |
104
|
372 |
|
$language = strtolower($parts[0]); |
105
|
372 |
|
$priority = empty($parts[1]) ? 1. : floatval(str_replace('q=', '', $parts[1])); |
106
|
372 |
|
$this->languages[$language] = $priority; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
// Sort languages by priority. |
110
|
372 |
|
arsort($this->languages); |
111
|
372 |
|
} |
112
|
|
|
} |
113
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.