| 1 | <?php |
||
| 5 | class BrowserLocale |
||
| 6 | { |
||
| 7 | /** |
||
| 8 | * Array of all preferred locales that are |
||
| 9 | * configured in a visitor's browser. |
||
| 10 | * |
||
| 11 | * @var array |
||
| 12 | */ |
||
| 13 | protected $locales = []; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Supported filters for getLocales(). |
||
| 17 | * |
||
| 18 | * @var array |
||
| 19 | */ |
||
| 20 | protected $filters = ['full', 'language', 'country', 'weight']; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Create a new BrowserLocale instance. |
||
| 24 | * |
||
| 25 | * @param string $httpAcceptLanguages |
||
| 26 | */ |
||
| 27 | 8 | public function __construct($httpAcceptLanguages) |
|
| 28 | { |
||
| 29 | // $_SERVER["HTTP_ACCEPT_LANGUAGE"] will return a comma separated list of language codes. |
||
| 30 | // Each language code MAY have a "relative quality factor" attached ("nl;q=0.8") which |
||
| 31 | // determines the order of preference. For example: "nl-NL,nl;q=0.8,en-US;q=0.6,en;q=0.4" |
||
| 32 | 8 | $this->parseAcceptLanguages($httpAcceptLanguages); |
|
| 33 | 8 | } |
|
| 34 | |||
| 35 | /** |
||
| 36 | * Get the most preferred locale. |
||
| 37 | * |
||
| 38 | * @return Locale|null |
||
| 39 | */ |
||
| 40 | 3 | public function getLocale() |
|
| 44 | |||
| 45 | /** |
||
| 46 | * Get an array of Locale objects in descending order of preference. |
||
| 47 | * |
||
| 48 | * If a property filter is specified, a flattened array of locale information, |
||
| 49 | * containing only the requested property values will be returned instead. |
||
| 50 | * |
||
| 51 | * @param string $propertyFilter |
||
| 52 | * |
||
| 53 | * @return array |
||
| 54 | */ |
||
| 55 | 6 | public function getLocales($propertyFilter = null) |
|
| 63 | |||
| 64 | /** |
||
| 65 | * Parse all HTTP Accept Languages. |
||
| 66 | * |
||
| 67 | * @param string $httpAcceptLanguages |
||
| 68 | * |
||
| 69 | * @return void |
||
| 70 | */ |
||
| 71 | 8 | protected function parseAcceptLanguages($httpAcceptLanguages) |
|
| 72 | { |
||
| 73 | 8 | if (empty($httpAcceptLanguages)) { |
|
| 74 | 1 | return; |
|
| 75 | } |
||
| 76 | |||
| 77 | 7 | foreach ($this->splitAcceptLanguages($httpAcceptLanguages) as $httpAcceptLanguage) { |
|
| 78 | 7 | if ($locale = $this->parseAcceptLanguage($httpAcceptLanguage)) { |
|
|
1 ignored issue
–
show
|
|||
| 79 | 7 | $this->locales[] = $locale; |
|
| 80 | } |
||
| 81 | } |
||
| 82 | |||
| 83 | 7 | $this->sortLocales(); |
|
| 84 | 7 | } |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Extract and save information from a HTTP Accept Language. |
||
| 88 | * |
||
| 89 | * @param string $httpAcceptLanguage |
||
| 90 | * |
||
| 91 | * @return Locale|null |
||
| 92 | */ |
||
| 93 | 7 | protected function parseAcceptLanguage($httpAcceptLanguage) |
|
| 112 | |||
| 113 | /** |
||
| 114 | * Convert a comma separated list to an array. |
||
| 115 | * |
||
| 116 | * Example: ["en", "en-US;q=0.8"] |
||
| 117 | * |
||
| 118 | * @param string $httpAcceptLanguages |
||
| 119 | * |
||
| 120 | * @return array |
||
| 121 | */ |
||
| 122 | 7 | protected function splitAcceptLanguages($httpAcceptLanguages) |
|
| 126 | |||
| 127 | /** |
||
| 128 | * Split a language code and the relative quality factor by semicolon. |
||
| 129 | * |
||
| 130 | * Example: ["en"] or ["en-US"] or ["en-US", "q=0.8"] |
||
| 131 | * |
||
| 132 | * @param string $httpAcceptLanguage |
||
| 133 | * |
||
| 134 | * @return array |
||
| 135 | */ |
||
| 136 | 7 | protected function splitAcceptLanguage($httpAcceptLanguage) |
|
| 140 | |||
| 141 | /** |
||
| 142 | * Get the 2-letter language code from the locale. |
||
| 143 | * |
||
| 144 | * Example: "en" |
||
| 145 | * |
||
| 146 | * @param string $locale |
||
| 147 | * |
||
| 148 | * @return string |
||
| 149 | */ |
||
| 150 | 7 | protected function getLanguage($locale) |
|
| 154 | |||
| 155 | /** |
||
| 156 | * Get the 2-letter country code from the locale. |
||
| 157 | * |
||
| 158 | * Example: "US" |
||
| 159 | * |
||
| 160 | * @param string $locale |
||
| 161 | * |
||
| 162 | * @return string |
||
| 163 | */ |
||
| 164 | 7 | protected function getCountry($locale) |
|
| 172 | |||
| 173 | /** |
||
| 174 | * Parse the relative quality factor and return its value. |
||
| 175 | * |
||
| 176 | * Example: 1.0 or 0.8 |
||
| 177 | * |
||
| 178 | * @param string $q |
||
| 179 | * |
||
| 180 | * @return float |
||
| 181 | */ |
||
| 182 | 7 | protected function getWeight($q) |
|
| 193 | |||
| 194 | /** |
||
| 195 | * Sort the array of locales in descending order of preference. |
||
| 196 | * |
||
| 197 | * @return void |
||
| 198 | */ |
||
| 199 | protected function sortLocales() |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Get a flattened array of locale information, |
||
| 212 | * containing only the requested property values. |
||
| 213 | * |
||
| 214 | * @param string $property |
||
| 215 | * |
||
| 216 | * @return array |
||
| 217 | */ |
||
| 218 | 5 | protected function filterLocaleInfo($property) |
|
| 236 | } |
||
| 237 |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.