@@ -20,7 +20,7 @@ class BrowserLocale
20
20
*/
21
21
public function __construct ($ httpAcceptLanguages )
22
22
{
23
- $ this ->parseHttpAcceptLanguages ($ httpAcceptLanguages );
23
+ $ this ->locales = $ this -> parseHttpAcceptLanguages ($ httpAcceptLanguages );
24
24
}
25
25
26
26
/**
@@ -64,21 +64,24 @@ public function filter(Filter $filter)
64
64
*/
65
65
protected function parseHttpAcceptLanguages ($ httpAcceptLanguages )
66
66
{
67
- $ locales = $ this ->split ($ httpAcceptLanguages , ', ' );
67
+ $ acceptLanguages = $ this ->split ($ httpAcceptLanguages , ', ' );
68
+ $ locales = [];
68
69
69
- foreach ($ locales as $ httpAcceptLanguage ) {
70
- $ this ->makeLocale ($ httpAcceptLanguage );
70
+ foreach ($ acceptLanguages as $ httpAcceptLanguage ) {
71
+ $ locales [] = $ this ->makeLocale ($ httpAcceptLanguage );
71
72
}
72
73
73
- $ this ->sortLocales ();
74
+ $ sortedLocales = $ this ->sortLocales ($ locales );
75
+
76
+ return $ sortedLocales ;
74
77
}
75
78
76
79
/**
77
80
* Convert the given HTTP Accept Language to a Locale object.
78
81
*
79
82
* @param string $httpAcceptLanguage
80
83
*
81
- * @return void
84
+ * @return \CodeZero\BrowserLocale\Locale
82
85
*/
83
86
protected function makeLocale ($ httpAcceptLanguage )
84
87
{
@@ -87,11 +90,7 @@ protected function makeLocale($httpAcceptLanguage)
87
90
$ locale = $ parts [0 ];
88
91
$ weight = $ parts [1 ] ?? null ;
89
92
90
- if (empty ($ locale )) {
91
- return ;
92
- }
93
-
94
- $ this ->locales [] = new Locale (
93
+ return new Locale (
95
94
$ locale ,
96
95
$ this ->getLanguage ($ locale ),
97
96
$ this ->getCountry ($ locale ),
@@ -143,13 +142,13 @@ protected function getCountry($locale)
143
142
/**
144
143
* Parse the relative quality factor and return its value.
145
144
*
146
- * @param string $q
145
+ * @param string $qualityFactor
147
146
*
148
147
* @return float
149
148
*/
150
- protected function getWeight ($ q )
149
+ protected function getWeight ($ qualityFactor )
151
150
{
152
- $ parts = $ this ->split ($ q , '= ' );
151
+ $ parts = $ this ->split ($ qualityFactor , '= ' );
153
152
154
153
$ weight = $ parts [1 ] ?? 1.0 ;
155
154
@@ -159,16 +158,20 @@ protected function getWeight($q)
159
158
/**
160
159
* Sort the array of locales in descending order of preference.
161
160
*
162
- * @return void
161
+ * @param array $locales
162
+ *
163
+ * @return array
163
164
*/
164
- protected function sortLocales ()
165
+ protected function sortLocales ($ locales )
165
166
{
166
- usort ($ this -> locales , function ($ a , $ b ) {
167
- if ($ a ->weight === $ b ->weight ) {
167
+ usort ($ locales , function ($ localeA , $ localeB ) {
168
+ if ($ localeA ->weight === $ localeB ->weight ) {
168
169
return 0 ;
169
170
}
170
171
171
- return ($ a ->weight > $ b ->weight ) ? -1 : 1 ;
172
+ return ($ localeA ->weight > $ localeB ->weight ) ? -1 : 1 ;
172
173
});
174
+
175
+ return $ locales ;
173
176
}
174
177
}
0 commit comments