1 | <?php namespace Arcanedev\Localization\Utilities; |
||
25 | class Negotiator implements NegotiatorContract |
||
26 | { |
||
27 | /* ------------------------------------------------------------------------------------------------ |
||
28 | | Properties |
||
29 | | ------------------------------------------------------------------------------------------------ |
||
30 | */ |
||
31 | /** |
||
32 | * Default Locale. |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | private $defaultLocale; |
||
37 | |||
38 | /** |
||
39 | * The supported locales collection. |
||
40 | * |
||
41 | * @var \Arcanedev\Localization\Entities\LocaleCollection |
||
42 | */ |
||
43 | private $supportedLocales; |
||
44 | |||
45 | /** |
||
46 | * The HTTP request instance. |
||
47 | * |
||
48 | * @var \Illuminate\Http\Request |
||
49 | */ |
||
50 | private $request; |
||
51 | |||
52 | /* ------------------------------------------------------------------------------------------------ |
||
53 | | Main Functions |
||
54 | | ------------------------------------------------------------------------------------------------ |
||
55 | */ |
||
56 | /** |
||
57 | * Make Negotiator instance. |
||
58 | * |
||
59 | * @param string $defaultLocale |
||
60 | * @param \Arcanedev\Localization\Entities\LocaleCollection $supportedLanguages |
||
61 | */ |
||
62 | 189 | public function __construct($defaultLocale, LocaleCollection $supportedLanguages) |
|
67 | |||
68 | /* ------------------------------------------------------------------------------------------------ |
||
69 | | Main Functions |
||
70 | | ------------------------------------------------------------------------------------------------ |
||
71 | */ |
||
72 | /** |
||
73 | * Negotiate the request. |
||
74 | * |
||
75 | * @param \Illuminate\Http\Request $request |
||
76 | * |
||
77 | * @return string |
||
78 | */ |
||
79 | 189 | public function negotiate(Request $request) |
|
99 | |||
100 | /** |
||
101 | * Get locale from accepted languages header. |
||
102 | * |
||
103 | * @return null|string |
||
104 | */ |
||
105 | 189 | private function getFromAcceptedLanguagesHeader() |
|
120 | |||
121 | /** |
||
122 | * Get locale from http accepted languages server. |
||
123 | * |
||
124 | * @return null|string |
||
125 | */ |
||
126 | 12 | private function getFromHttpAcceptedLanguagesServer() |
|
144 | |||
145 | /** |
||
146 | * Get locale from remote host server. |
||
147 | * |
||
148 | * @return null|string |
||
149 | */ |
||
150 | 9 | private function getFromRemoteHostServer() |
|
160 | |||
161 | /* ------------------------------------------------------------------------------------------------ |
||
162 | | Check Functions |
||
163 | | ------------------------------------------------------------------------------------------------ |
||
164 | */ |
||
165 | /** |
||
166 | * Check if matches a supported locale. |
||
167 | * |
||
168 | * @param array $matches |
||
169 | * |
||
170 | * @return null|string |
||
171 | */ |
||
172 | 189 | private function inSupportedLocales(array $matches) |
|
188 | |||
189 | /** |
||
190 | * Check if the locale is supported. |
||
191 | * |
||
192 | * @param string $locale |
||
193 | * |
||
194 | * @return bool |
||
195 | */ |
||
196 | 189 | private function isSupported($locale) |
|
200 | |||
201 | /* ------------------------------------------------------------------------------------------------ |
||
202 | | Other Functions |
||
203 | | ------------------------------------------------------------------------------------------------ |
||
204 | */ |
||
205 | /** |
||
206 | * Return all the accepted languages from the browser |
||
207 | * |
||
208 | * @return array - Matches from the header field Accept-Languages |
||
209 | */ |
||
210 | 189 | private function getMatchesFromAcceptedLanguages() |
|
227 | |||
228 | /** |
||
229 | * Get the generic matches. |
||
230 | * |
||
231 | * @param array $acceptLanguages |
||
232 | * @param array $matches |
||
233 | * |
||
234 | * @return array |
||
235 | */ |
||
236 | 189 | private function retrieveGenericMatches($acceptLanguages, &$matches) |
|
269 | |||
270 | /** |
||
271 | * Get the quality factor. |
||
272 | * |
||
273 | * @param string $locale |
||
274 | * @param array $option |
||
275 | * |
||
276 | * @return float|null |
||
277 | */ |
||
278 | 189 | private function getQualityFactor($locale, $option) |
|
295 | } |
||
296 |