Skip to content

Commit c3e1365

Browse files
committed
Refactor for readability and simplicity
1 parent 4b43181 commit c3e1365

File tree

7 files changed

+88
-102
lines changed

7 files changed

+88
-102
lines changed

.scrutinizer.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
filter:
22
excluded_paths:
3-
- "config/"
4-
- "resources/"
5-
- "routes/"
63
- "tests/"
74
checks:
85
php:

CHANGELOG.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ All notable changes to `BrowserLocale` will be documented in this file.
77
- Rename main class & remove interface for simplicity
88
- Make constructor argument required
99
- Make private methods protected
10-
- Rename `$locale` property to `$full` in `Locale` class
1110
- Switch to PHPUnit for testing
1211
- Add ServiceProvider for Laravel
1312

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@
1111

1212
Every browser has a setting for preferred website locales.
1313

14-
This can be read by PHP, usually with the `$_SERVER["HTTP_ACCEPT_LANGUAGE"]` variable. Its value is a string that looks like this: `nl-NL,nl;q=0.8,en-US;q=0.6,en;q=0.4`.
14+
This can be read by PHP, usually with the `$_SERVER["HTTP_ACCEPT_LANGUAGE"]` variable.
15+
16+
> `$_SERVER["HTTP_ACCEPT_LANGUAGE"]` will return a comma separated list of language codes. Each language code MAY have a "relative quality factor" attached ("nl;q=0.8") which determines the order of preference. For example: `nl-NL,nl;q=0.8,en-US;q=0.6,en;q=0.4`. If no relative quality factor is present, the value is by default `1.0`.
17+
18+
**BrowserLocale** parses this string and lets you access the preferred locales quickly and easily.
1519

16-
`BrowserLocale` parses this string and lets you access the preferred locales quickly and easily.
1720

1821
## Requirements
1922

@@ -51,7 +54,7 @@ This will return an instance of `\CodeZero\BrowserLocale\Locale` or `null` if no
5154

5255
``` php
5356
if ($locale !== null) {
54-
$full = $locale->full; // Example: "en-US"
57+
$full = $locale->locale; // Example: "en-US"
5558
$language = $locale->language; // Example: "en"
5659
$country = $locale->country; // Example: "US"
5760
$weight = $locale->weight; // Example: 1.0
@@ -70,7 +73,7 @@ If no locales exist, an empty array will be returned.
7073

7174
``` php
7275
foreach ($locales as $locale) {
73-
$full = $locale->full; // Example: "en-US"
76+
$full = $locale->locale; // Example: "en-US"
7477
$language = $locale->language; // Example: "en"
7578
$country = $locale->country; // Example: "US"
7679
$weight = $locale->weight; // Example: 1.0
@@ -82,7 +85,7 @@ foreach ($locales as $locale) {
8285
You can get a flattened array, containing only specific locale info. These arrays will always be sorted by weight in descending order. There will be no duplicate values! (e.g. `en` and `en-US` are both the language `en`)
8386

8487
``` php
85-
$locales = $browser->getLocales('full');
88+
$locales = $browser->getLocales('locale');
8689
//=> Result: ['en-US', 'en', 'nl-BE', 'nl']
8790

8891
$languages = $browser->getLocales('language');

phpunit.xml.dist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
<filter>
1717
<whitelist processUncoveredFilesFromWhitelist="true">
1818
<directory suffix=".php">./src</directory>
19+
<exclude>
20+
<file>./src/Laravel/BrowserLocaleServiceProvider.php</file>
21+
</exclude>
1922
</whitelist>
2023
</filter>
2124
</phpunit>

src/BrowserLocale.php

Lines changed: 36 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
class BrowserLocale
66
{
77
/**
8-
* Array of all preferred locales that are
9-
* configured in a visitor's browser.
8+
* Array of \CodeZero\BrowserLocale\Locale instances.
109
*
1110
* @var array
1211
*/
@@ -17,7 +16,7 @@ class BrowserLocale
1716
*
1817
* @var array
1918
*/
20-
protected $filters = ['full', 'language', 'country', 'weight'];
19+
protected $filters = ['locale', 'language', 'country', 'weight'];
2120

2221
/**
2322
* Create a new BrowserLocale instance.
@@ -26,10 +25,7 @@ class BrowserLocale
2625
*/
2726
public function __construct($httpAcceptLanguages)
2827
{
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-
$this->parseAcceptLanguages($httpAcceptLanguages);
28+
$this->parseHttpAcceptLanguages($httpAcceptLanguages);
3329
}
3430

3531
/**
@@ -44,21 +40,19 @@ public function getLocale()
4440

4541
/**
4642
* Get an array of Locale objects in descending order of preference.
43+
* Specify a Locale property to get a flattened array of values of that property.
4744
*
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
45+
* @param string $property
5246
*
5347
* @return array
5448
*/
55-
public function getLocales($propertyFilter = null)
49+
public function getLocales($property = null)
5650
{
57-
if ($propertyFilter === null) {
51+
if ( ! in_array($property, $this->filters)) {
5852
return $this->locales;
5953
}
6054

61-
return $this->filterLocaleInfo($propertyFilter);
55+
return $this->filterLocaleInfo($property);
6256
}
6357

6458
/**
@@ -68,127 +62,94 @@ public function getLocales($propertyFilter = null)
6862
*
6963
* @return void
7064
*/
71-
protected function parseAcceptLanguages($httpAcceptLanguages)
65+
protected function parseHttpAcceptLanguages($httpAcceptLanguages)
7266
{
73-
if (empty($httpAcceptLanguages)) {
74-
return;
75-
}
67+
$locales = $this->split($httpAcceptLanguages, ',');
7668

77-
foreach ($this->splitAcceptLanguages($httpAcceptLanguages) as $httpAcceptLanguage) {
78-
if ($locale = $this->parseAcceptLanguage($httpAcceptLanguage)) {
79-
$this->locales[] = $locale;
80-
}
69+
foreach ($locales as $httpAcceptLanguage) {
70+
$this->makeLocale($httpAcceptLanguage);
8171
}
8272

8373
$this->sortLocales();
8474
}
8575

8676
/**
87-
* Extract and save information from a HTTP Accept Language.
77+
* Convert the given HTTP Accept Language to a Locale object.
8878
*
8979
* @param string $httpAcceptLanguage
9080
*
91-
* @return \CodeZero\BrowserLocale\Locale|null
81+
* @return void
9282
*/
93-
protected function parseAcceptLanguage($httpAcceptLanguage)
83+
protected function makeLocale($httpAcceptLanguage)
9484
{
95-
$parts = $this->splitAcceptLanguage($httpAcceptLanguage);
85+
$parts = $this->split($httpAcceptLanguage, ';');
9686

97-
$locale = $parts[0] ?? null;
87+
$locale = $parts[0];
9888
$weight = $parts[1] ?? null;
9989

100-
if ($locale === null) {
101-
return null;
90+
if (empty($locale)) {
91+
return;
10292
}
10393

104-
$localeInstance = new Locale();
105-
$localeInstance->full = $locale;
106-
$localeInstance->language = $this->getLanguage($locale);
107-
$localeInstance->country = $this->getCountry($locale);
108-
$localeInstance->weight = $this->getWeight($weight);
109-
110-
return $localeInstance;
111-
}
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-
protected function splitAcceptLanguages($httpAcceptLanguages)
123-
{
124-
return explode(',', $httpAcceptLanguages) ?: [];
94+
$this->locales[] = new Locale(
95+
$locale,
96+
$this->getLanguage($locale),
97+
$this->getCountry($locale),
98+
$this->getWeight($weight)
99+
);
125100
}
126101

127102
/**
128-
* Split a language code and the relative quality factor by semicolon.
103+
* Split the given string by the delimiter.
129104
*
130-
* Example: ["en"] or ["en-US"] or ["en-US", "q=0.8"]
131-
*
132-
* @param string $httpAcceptLanguage
105+
* @param string $string
106+
* @param string $delimiter
133107
*
134108
* @return array
135109
*/
136-
protected function splitAcceptLanguage($httpAcceptLanguage)
110+
protected function split($string, $delimiter)
137111
{
138-
return explode(';', trim($httpAcceptLanguage)) ?: [];
112+
return explode($delimiter, trim($string)) ?: [];
139113
}
140114

141115
/**
142116
* Get the 2-letter language code from the locale.
143117
*
144-
* Example: "en"
145-
*
146118
* @param string $locale
147119
*
148120
* @return string
149121
*/
150122
protected function getLanguage($locale)
151123
{
152-
return substr($locale, 0, 2);
124+
return substr($locale, 0, 2) ?: '';
153125
}
154126

155127
/**
156128
* Get the 2-letter country code from the locale.
157129
*
158-
* Example: "US"
159-
*
160130
* @param string $locale
161131
*
162132
* @return string
163133
*/
164134
protected function getCountry($locale)
165135
{
166-
if (($divider = strpos($locale, '-')) === false){
167-
return '';
168-
}
169-
170-
return substr($locale, $divider + 1, 2);
136+
return substr($locale, 3, 2) ?: '';
171137
}
172138

173139
/**
174140
* Parse the relative quality factor and return its value.
175141
*
176-
* Example: 1.0 or 0.8
177-
*
178142
* @param string $q
179143
*
180144
* @return float
181145
*/
182146
protected function getWeight($q)
183147
{
184-
$weight = 1.0;
185-
$parts = explode('=', $q);
148+
$parts = $this->split($q, '=');
186149

187-
if (isset($parts[1])) {
188-
$weight = ((float) $parts[1]);
189-
}
150+
$weight = $parts[1] ?? 1.0;
190151

191-
return $weight;
152+
return (float) $weight;
192153
}
193154

194155
/**
@@ -217,15 +178,9 @@ protected function sortLocales()
217178
*/
218179
protected function filterLocaleInfo($property)
219180
{
220-
$locales = $this->locales;
221-
222-
if ( ! in_array($property, $this->filters)) {
223-
return $locales;
224-
}
225-
226181
$filtered = [];
227182

228-
foreach ($locales as $locale) {
183+
foreach ($this->locales as $locale) {
229184
if ($locale->$property && ! in_array($locale->$property, $filtered)) {
230185
$filtered[] = $locale->$property;
231186
}

src/Locale.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
class Locale
66
{
77
/**
8-
* Full locale with language and country code.
8+
* Full locale with language and optional country code.
99
*
1010
* @var string
1111
*/
12-
public $full;
12+
public $locale;
1313

1414
/**
1515
* Language code of the locale.
@@ -32,4 +32,20 @@ class Locale
3232
* @var float
3333
*/
3434
public $weight;
35+
36+
/**
37+
* Create a new Locale instance.
38+
*
39+
* @param string $locale
40+
* @param string $language
41+
* @param string $country
42+
* @param float $weight
43+
*/
44+
public function __construct($locale, $language, $country, $weight)
45+
{
46+
$this->locale = $locale;
47+
$this->language = $language;
48+
$this->country = $country;
49+
$this->weight = $weight;
50+
}
3551
}

0 commit comments

Comments
 (0)