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 | | Constructor |
||
54 | | ----------------------------------------------------------------- |
||
55 | */ |
||
56 | /** |
||
57 | * Make Negotiator instance. |
||
58 | * |
||
59 | * @param string $defaultLocale |
||
60 | * @param \Arcanedev\Localization\Entities\LocaleCollection $supportedLanguages |
||
61 | */ |
||
62 | 192 | public function __construct($defaultLocale, LocaleCollection $supportedLanguages) |
|
67 | |||
68 | /* ----------------------------------------------------------------- |
||
69 | | Main Methods |
||
70 | | ----------------------------------------------------------------- |
||
71 | */ |
||
72 | /** |
||
73 | * Make Negotiator instance. |
||
74 | * |
||
75 | * @param string $defaultLocale |
||
76 | * @param \Arcanedev\Localization\Entities\LocaleCollection $supportedLanguages |
||
77 | * |
||
78 | * @return self |
||
79 | */ |
||
80 | 192 | public static function make($defaultLocale, LocaleCollection $supportedLanguages) |
|
84 | |||
85 | /** |
||
86 | * Negotiate the request. |
||
87 | * |
||
88 | * @param \Illuminate\Http\Request $request |
||
89 | * |
||
90 | * @return string |
||
91 | */ |
||
92 | 192 | public function negotiate(Request $request) |
|
109 | |||
110 | /** |
||
111 | * Get locale from accepted languages header. |
||
112 | * |
||
113 | * @return null|string |
||
114 | */ |
||
115 | 192 | private function getFromAcceptedLanguagesHeader() |
|
130 | |||
131 | /** |
||
132 | * Get locale from http accepted languages server. |
||
133 | * |
||
134 | * @return null|string |
||
135 | */ |
||
136 | 12 | private function getFromHttpAcceptedLanguagesServer() |
|
151 | |||
152 | /** |
||
153 | * Get locale from remote host server. |
||
154 | * |
||
155 | * @return null|string |
||
156 | */ |
||
157 | 9 | private function getFromRemoteHostServer() |
|
167 | |||
168 | /* ------------------------------------------------------------------------------------------------ |
||
169 | | Check Methods |
||
170 | | ------------------------------------------------------------------------------------------------ |
||
171 | */ |
||
172 | /** |
||
173 | * Check if matches a supported locale. |
||
174 | * |
||
175 | * @param array $matches |
||
176 | * |
||
177 | * @return null|string |
||
178 | */ |
||
179 | 192 | private function inSupportedLocales(array $matches) |
|
193 | |||
194 | /** |
||
195 | * Check if the locale is supported. |
||
196 | * |
||
197 | * @param string $locale |
||
198 | * |
||
199 | * @return bool |
||
200 | */ |
||
201 | 192 | private function isSupported($locale) |
|
205 | |||
206 | /* ------------------------------------------------------------------------------------------------ |
||
207 | | Other Functions |
||
208 | | ------------------------------------------------------------------------------------------------ |
||
209 | */ |
||
210 | /** |
||
211 | * Return all the accepted languages from the browser |
||
212 | * |
||
213 | * @return array - Matches from the header field Accept-Languages |
||
214 | */ |
||
215 | 192 | private function getMatchesFromAcceptedLanguages() |
|
232 | |||
233 | /** |
||
234 | * Get the generic matches. |
||
235 | * |
||
236 | * @param array $acceptLanguages |
||
237 | * @param array $matches |
||
238 | * |
||
239 | * @return array |
||
240 | */ |
||
241 | 192 | private function retrieveGenericMatches($acceptLanguages, &$matches) |
|
274 | |||
275 | /** |
||
276 | * Get the quality factor. |
||
277 | * |
||
278 | * @param string $locale |
||
279 | * @param array $option |
||
280 | * |
||
281 | * @return float|null |
||
282 | */ |
||
283 | 192 | private function getQualityFactor($locale, $option) |
|
297 | } |
||
298 |