1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CodeZero\BrowserLocale; |
4
|
|
|
|
5
|
|
|
use CodeZero\BrowserLocale\Filters\Filter; |
6
|
|
|
|
7
|
|
|
class BrowserLocale |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Array of \CodeZero\BrowserLocale\Locale instances. |
11
|
|
|
* |
12
|
|
|
* @var array |
13
|
|
|
*/ |
14
|
|
|
protected $locales = []; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Create a new BrowserLocale instance. |
18
|
|
|
* |
19
|
|
|
* @param string $httpAcceptLanguages |
20
|
|
|
*/ |
21
|
6 |
|
public function __construct($httpAcceptLanguages) |
22
|
|
|
{ |
23
|
6 |
|
$this->parseHttpAcceptLanguages($httpAcceptLanguages); |
24
|
6 |
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Get the most preferred locale. |
28
|
|
|
* |
29
|
|
|
* @return \CodeZero\BrowserLocale\Locale|null |
30
|
|
|
*/ |
31
|
4 |
|
public function getLocale() |
32
|
|
|
{ |
33
|
4 |
|
return $this->locales[0] ?? null; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Get an array of Locale objects in descending order of preference. |
38
|
|
|
* |
39
|
|
|
* @return array |
40
|
|
|
*/ |
41
|
3 |
|
public function getLocales() |
42
|
|
|
{ |
43
|
3 |
|
return $this->locales; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Filter the locales using the given Filter. |
48
|
|
|
* |
49
|
|
|
* @param \CodeZero\BrowserLocale\Filters\Filter $filter |
50
|
|
|
* |
51
|
|
|
* @return array |
52
|
|
|
*/ |
53
|
1 |
|
public function filter(Filter $filter) |
54
|
|
|
{ |
55
|
1 |
|
return $filter->filter($this->locales); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Parse all HTTP Accept Languages. |
60
|
|
|
* |
61
|
|
|
* @param string $httpAcceptLanguages |
62
|
|
|
* |
63
|
|
|
* @return void |
64
|
|
|
*/ |
65
|
6 |
|
protected function parseHttpAcceptLanguages($httpAcceptLanguages) |
66
|
|
|
{ |
67
|
6 |
|
$locales = $this->split($httpAcceptLanguages, ','); |
68
|
|
|
|
69
|
6 |
|
foreach ($locales as $httpAcceptLanguage) { |
70
|
6 |
|
$this->makeLocale($httpAcceptLanguage); |
71
|
|
|
} |
72
|
|
|
|
73
|
6 |
|
$this->sortLocales(); |
74
|
6 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Convert the given HTTP Accept Language to a Locale object. |
78
|
|
|
* |
79
|
|
|
* @param string $httpAcceptLanguage |
80
|
|
|
* |
81
|
|
|
* @return void |
82
|
|
|
*/ |
83
|
6 |
|
protected function makeLocale($httpAcceptLanguage) |
84
|
|
|
{ |
85
|
6 |
|
$parts = $this->split($httpAcceptLanguage, ';'); |
86
|
|
|
|
87
|
6 |
|
$locale = $parts[0]; |
88
|
6 |
|
$weight = $parts[1] ?? null; |
89
|
|
|
|
90
|
6 |
|
if (empty($locale)) { |
91
|
3 |
|
return; |
92
|
|
|
} |
93
|
|
|
|
94
|
3 |
|
$this->locales[] = new Locale( |
95
|
3 |
|
$locale, |
96
|
3 |
|
$this->getLanguage($locale), |
97
|
3 |
|
$this->getCountry($locale), |
98
|
3 |
|
$this->getWeight($weight) |
99
|
|
|
); |
100
|
3 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Split the given string by the delimiter. |
104
|
|
|
* |
105
|
|
|
* @param string $string |
106
|
|
|
* @param string $delimiter |
107
|
|
|
* |
108
|
|
|
* @return array |
109
|
|
|
*/ |
110
|
6 |
|
protected function split($string, $delimiter) |
111
|
|
|
{ |
112
|
6 |
|
return explode($delimiter, trim($string)) ?: []; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get the 2-letter language code from the locale. |
117
|
|
|
* |
118
|
|
|
* @param string $locale |
119
|
|
|
* |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
3 |
|
protected function getLanguage($locale) |
123
|
|
|
{ |
124
|
3 |
|
return substr($locale, 0, 2) ?: ''; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Get the 2-letter country code from the locale. |
129
|
|
|
* |
130
|
|
|
* @param string $locale |
131
|
|
|
* |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
3 |
|
protected function getCountry($locale) |
135
|
|
|
{ |
136
|
3 |
|
return substr($locale, 3, 2) ?: ''; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Parse the relative quality factor and return its value. |
141
|
|
|
* |
142
|
|
|
* @param string $q |
143
|
|
|
* |
144
|
|
|
* @return float |
145
|
|
|
*/ |
146
|
3 |
|
protected function getWeight($q) |
147
|
|
|
{ |
148
|
3 |
|
$parts = $this->split($q, '='); |
149
|
|
|
|
150
|
3 |
|
$weight = $parts[1] ?? 1.0; |
151
|
|
|
|
152
|
3 |
|
return (float) $weight; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Sort the array of locales in descending order of preference. |
157
|
|
|
* |
158
|
|
|
* @return void |
159
|
|
|
*/ |
160
|
6 |
|
protected function sortLocales() |
161
|
|
|
{ |
162
|
6 |
|
usort($this->locales, function ($a, $b) { |
163
|
3 |
|
if ($a->weight === $b->weight) { |
164
|
1 |
|
return 0; |
165
|
|
|
} |
166
|
|
|
|
167
|
3 |
|
return ($a->weight > $b->weight) ? -1 : 1; |
168
|
6 |
|
}); |
169
|
6 |
|
} |
170
|
|
|
} |
171
|
|
|
|