@@ -20,7 +20,7 @@ class BrowserLocale
2020 */
2121 public function __construct ($ httpAcceptLanguages )
2222 {
23- $ this ->parseHttpAcceptLanguages ($ httpAcceptLanguages );
23+ $ this ->locales = $ this -> parseHttpAcceptLanguages ($ httpAcceptLanguages );
2424 }
2525
2626 /**
@@ -64,21 +64,24 @@ public function filter(Filter $filter)
6464 */
6565 protected function parseHttpAcceptLanguages ($ httpAcceptLanguages )
6666 {
67- $ locales = $ this ->split ($ httpAcceptLanguages , ', ' );
67+ $ acceptLanguages = $ this ->split ($ httpAcceptLanguages , ', ' );
68+ $ locales = [];
6869
69- foreach ($ locales as $ httpAcceptLanguage ) {
70- $ this ->makeLocale ($ httpAcceptLanguage );
70+ foreach ($ acceptLanguages as $ httpAcceptLanguage ) {
71+ $ locales [] = $ this ->makeLocale ($ httpAcceptLanguage );
7172 }
7273
73- $ this ->sortLocales ();
74+ $ sortedLocales = $ this ->sortLocales ($ locales );
75+
76+ return $ sortedLocales ;
7477 }
7578
7679 /**
7780 * Convert the given HTTP Accept Language to a Locale object.
7881 *
7982 * @param string $httpAcceptLanguage
8083 *
81- * @return void
84+ * @return \CodeZero\BrowserLocale\Locale
8285 */
8386 protected function makeLocale ($ httpAcceptLanguage )
8487 {
@@ -87,11 +90,7 @@ protected function makeLocale($httpAcceptLanguage)
8790 $ locale = $ parts [0 ];
8891 $ weight = $ parts [1 ] ?? null ;
8992
90- if (empty ($ locale )) {
91- return ;
92- }
93-
94- $ this ->locales [] = new Locale (
93+ return new Locale (
9594 $ locale ,
9695 $ this ->getLanguage ($ locale ),
9796 $ this ->getCountry ($ locale ),
@@ -143,13 +142,13 @@ protected function getCountry($locale)
143142 /**
144143 * Parse the relative quality factor and return its value.
145144 *
146- * @param string $q
145+ * @param string $qualityFactor
147146 *
148147 * @return float
149148 */
150- protected function getWeight ($ q )
149+ protected function getWeight ($ qualityFactor )
151150 {
152- $ parts = $ this ->split ($ q , '= ' );
151+ $ parts = $ this ->split ($ qualityFactor , '= ' );
153152
154153 $ weight = $ parts [1 ] ?? 1.0 ;
155154
@@ -159,16 +158,20 @@ protected function getWeight($q)
159158 /**
160159 * Sort the array of locales in descending order of preference.
161160 *
162- * @return void
161+ * @param array $locales
162+ *
163+ * @return array
163164 */
164- protected function sortLocales ()
165+ protected function sortLocales ($ locales )
165166 {
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 ) {
168169 return 0 ;
169170 }
170171
171- return ($ a ->weight > $ b ->weight ) ? -1 : 1 ;
172+ return ($ localeA ->weight > $ localeB ->weight ) ? -1 : 1 ;
172173 });
174+
175+ return $ locales ;
173176 }
174177}
0 commit comments