1 | <?php |
||
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 | 10 | public function __construct($httpAcceptLanguages) |
|
22 | { |
||
23 | 10 | $this->parseHttpAcceptLanguages($httpAcceptLanguages); |
|
24 | 10 | } |
|
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 | 5 | public function filter(Filter $filter) |
|
54 | { |
||
55 | 5 | 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 | 10 | protected function parseHttpAcceptLanguages($httpAcceptLanguages) |
|
75 | |||
76 | /** |
||
77 | * Convert the given HTTP Accept Language to a Locale object. |
||
78 | * |
||
79 | * @param string $httpAcceptLanguage |
||
80 | * |
||
81 | * @return void |
||
82 | */ |
||
83 | 10 | protected function makeLocale($httpAcceptLanguage) |
|
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 | 10 | protected function split($string, $delimiter) |
|
114 | |||
115 | /** |
||
116 | * Get the 2-letter language code from the locale. |
||
117 | * |
||
118 | * @param string $locale |
||
119 | * |
||
120 | * @return string |
||
121 | */ |
||
122 | 8 | protected function getLanguage($locale) |
|
126 | |||
127 | /** |
||
128 | * Get the 2-letter country code from the locale. |
||
129 | * |
||
130 | * @param string $locale |
||
131 | * |
||
132 | * @return string |
||
133 | */ |
||
134 | 8 | protected function getCountry($locale) |
|
138 | |||
139 | /** |
||
140 | * Parse the relative quality factor and return its value. |
||
141 | * |
||
142 | * @param string $q |
||
143 | * |
||
144 | * @return float |
||
145 | */ |
||
146 | 8 | protected function getWeight($q) |
|
154 | |||
155 | /** |
||
156 | * Sort the array of locales in descending order of preference. |
||
157 | * |
||
158 | * @return void |
||
159 | */ |
||
160 | protected function sortLocales() |
||
170 | } |
||
171 |