Skip to content

Commit 36246aa

Browse files
committed
Introduce Filter classes
1 parent f0cb914 commit 36246aa

18 files changed

+497
-83
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
All notable changes to `BrowserLocale` will be documented in this file.
44

5+
## 3.0.0 (2018-04-01)
6+
7+
- Use `Filter` classes to filter specific Locale info with a new `filter()` method
8+
- `getLocales()` no longer accepts an argument to filter results
9+
510
## 2.0.0 (2018-03-30)
611

712
- Rename main class & remove interface for simplicity

README.md

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,28 +80,39 @@ foreach ($locales as $locale) {
8080
}
8181
```
8282

83-
## Get Flattened Array
83+
## Filter Locale Info
8484

85-
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`)
85+
You can get a flattened array with only specific Locale information. 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`)
8686

8787
``` php
88-
$locales = $browser->getLocales('locale');
88+
$filter = \CodeZero\BrowserLocale\Filters\LocaleFilter;
89+
$locales = $browser->filter($filter);
8990
//=> 'en-US,en;q=0.8,nl-NL;q=0.6'
9091
//=> Result: ['en-US', 'en', 'nl-BE']
9192

92-
$languages = $browser->getLocales('language');
93+
$filter = \CodeZero\BrowserLocale\Filters\CombinedFilter;
94+
$locales = $browser->filter($filter);
95+
//=> 'en-US,nl-NL;q=0.8,nl;q=0.6'
96+
//=> Result: ['en-US', 'en', 'nl-BE', 'nl']
97+
98+
$filter = \CodeZero\BrowserLocale\Filters\LanguageFilter;
99+
$languages = $browser->filter($filter);
93100
//=> 'en-US,en;q=0.8,nl-NL;q=0.6'
94101
//=> Result: ['en', 'nl']
95102

96-
$countries = $browser->getLocales('country');
103+
$filter = \CodeZero\BrowserLocale\Filters\CountryFilter;
104+
$countries = $browser->filter($filter);
97105
//=> 'en-US,en;q=0.8,nl-NL;q=0.6,nl;q=0.4'
98106
//=> Result: ['US', 'BE']
99107

100-
$weights = $browser->getLocales('weight');
108+
$filter = \CodeZero\BrowserLocale\Filters\WeightFilter;
109+
$weights = $browser->filter($filter);
101110
//=> 'en-US,en;q=0.8,nl-NL;q=0.6,nl;q=0.4'
102111
//=> Result: [1.0, 0.8, 0.6, 0.4]
103112
```
104113

114+
You can create your own filters by implementing the `\CodeZero\BrowserLocale\Filters\Filter` interface.
115+
105116
## Testing
106117

107118
```

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
},
2323
"require-dev": {
2424
"illuminate/support": "^5.5",
25+
"mockery/mockery": "^1.0",
2526
"phpunit/phpunit": "^6.0"
2627
},
2728
"scripts": {

composer.lock

Lines changed: 114 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/BrowserLocale.php

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace CodeZero\BrowserLocale;
44

5+
use CodeZero\BrowserLocale\Filters\Filter;
6+
57
class BrowserLocale
68
{
79
/**
@@ -11,13 +13,6 @@ class BrowserLocale
1113
*/
1214
protected $locales = [];
1315

14-
/**
15-
* Supported filters for getLocales().
16-
*
17-
* @var array
18-
*/
19-
protected $filters = ['locale', 'language', 'country', 'weight'];
20-
2116
/**
2217
* Create a new BrowserLocale instance.
2318
*
@@ -40,19 +35,24 @@ public function getLocale()
4035

4136
/**
4237
* 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.
44-
*
45-
* @param string $property
4638
*
4739
* @return array
4840
*/
49-
public function getLocales($property = null)
41+
public function getLocales()
5042
{
51-
if ( ! in_array($property, $this->filters)) {
52-
return $this->locales;
53-
}
43+
return $this->locales;
44+
}
5445

55-
return $this->filterLocaleInfo($property);
46+
/**
47+
* Filter the locales using the given Filter.
48+
*
49+
* @param \CodeZero\BrowserLocale\Filters\Filter $filter
50+
*
51+
* @return array
52+
*/
53+
public function filter(Filter $filter)
54+
{
55+
return $filter->filter($this->locales);
5656
}
5757

5858
/**
@@ -167,25 +167,4 @@ protected function sortLocales()
167167
return ($a->weight > $b->weight) ? -1 : 1;
168168
});
169169
}
170-
171-
/**
172-
* Get a flattened array of locale information,
173-
* containing only the requested property values.
174-
*
175-
* @param string $property
176-
*
177-
* @return array
178-
*/
179-
protected function filterLocaleInfo($property)
180-
{
181-
$filtered = [];
182-
183-
foreach ($this->locales as $locale) {
184-
if ($locale->$property && ! in_array($locale->$property, $filtered)) {
185-
$filtered[] = $locale->$property;
186-
}
187-
}
188-
189-
return $filtered;
190-
}
191170
}

src/Filters/CombinedFilter.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace CodeZero\BrowserLocale\Filters;
4+
5+
use CodeZero\BrowserLocale\Locale;
6+
7+
class CombinedFilter implements Filter
8+
{
9+
/**
10+
* The filtered results.
11+
*
12+
* @var array
13+
*/
14+
protected $filtered;
15+
16+
/**
17+
* Filter the locales.
18+
*
19+
* @param array $locales
20+
*
21+
* @return array
22+
*/
23+
public function filter(array $locales)
24+
{
25+
$this->filtered = [];
26+
27+
foreach ($locales as $locale) {
28+
$this->filterLocale($locale);
29+
}
30+
31+
return $this->filtered;
32+
}
33+
34+
/**
35+
* Filter the given Locale.
36+
*
37+
* @param \CodeZero\BrowserLocale\Locale $locale
38+
*
39+
* @return void
40+
*/
41+
protected function filterLocale(Locale $locale)
42+
{
43+
$language = substr($locale->locale, 0, 2);
44+
45+
$this->addLocale($locale->locale);
46+
$this->addLocale($language);
47+
}
48+
49+
/**
50+
* Add a locale to the results array.
51+
*
52+
* @param string $locale
53+
*
54+
* @return void
55+
*/
56+
protected function addLocale($locale)
57+
{
58+
if ( ! in_array($locale, $this->filtered)) {
59+
$this->filtered[] = $locale;
60+
}
61+
}
62+
}

src/Filters/CountryFilter.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace CodeZero\BrowserLocale\Filters;
4+
5+
class CountryFilter extends PropertyFilter implements Filter
6+
{
7+
/**
8+
* Filter the locales.
9+
*
10+
* @param array $locales
11+
*
12+
* @return array
13+
*/
14+
public function filter(array $locales)
15+
{
16+
return $this->filterByProperty($locales, 'country');
17+
}
18+
}

src/Filters/Filter.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace CodeZero\BrowserLocale\Filters;
4+
5+
interface Filter
6+
{
7+
/**
8+
* Filter the locales.
9+
*
10+
* @param array $locales
11+
*
12+
* @return array
13+
*/
14+
public function filter(array $locales);
15+
}

0 commit comments

Comments
 (0)