Skip to content

Commit

Permalink
Introduce Filter classes
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanvermeyen committed Mar 31, 2018
1 parent f0cb914 commit 36246aa
Show file tree
Hide file tree
Showing 18 changed files with 497 additions and 83 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

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

## 3.0.0 (2018-04-01)

- Use `Filter` classes to filter specific Locale info with a new `filter()` method
- `getLocales()` no longer accepts an argument to filter results

## 2.0.0 (2018-03-30)

- Rename main class & remove interface for simplicity
Expand Down
23 changes: 17 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,28 +80,39 @@ foreach ($locales as $locale) {
}
```

## Get Flattened Array
## Filter Locale Info

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`)
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`)

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

$languages = $browser->getLocales('language');
$filter = \CodeZero\BrowserLocale\Filters\CombinedFilter;
$locales = $browser->filter($filter);
//=> 'en-US,nl-NL;q=0.8,nl;q=0.6'
//=> Result: ['en-US', 'en', 'nl-BE', 'nl']

$filter = \CodeZero\BrowserLocale\Filters\LanguageFilter;
$languages = $browser->filter($filter);
//=> 'en-US,en;q=0.8,nl-NL;q=0.6'
//=> Result: ['en', 'nl']

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

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

You can create your own filters by implementing the `\CodeZero\BrowserLocale\Filters\Filter` interface.

## Testing

```
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
},
"require-dev": {
"illuminate/support": "^5.5",
"mockery/mockery": "^1.0",
"phpunit/phpunit": "^6.0"
},
"scripts": {
Expand Down
115 changes: 114 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 15 additions & 36 deletions src/BrowserLocale.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace CodeZero\BrowserLocale;

use CodeZero\BrowserLocale\Filters\Filter;

class BrowserLocale
{
/**
Expand All @@ -11,13 +13,6 @@ class BrowserLocale
*/
protected $locales = [];

/**
* Supported filters for getLocales().
*
* @var array
*/
protected $filters = ['locale', 'language', 'country', 'weight'];

/**
* Create a new BrowserLocale instance.
*
Expand All @@ -40,19 +35,24 @@ public function getLocale()

/**
* Get an array of Locale objects in descending order of preference.
* Specify a Locale property to get a flattened array of values of that property.
*
* @param string $property
*
* @return array
*/
public function getLocales($property = null)
public function getLocales()
{
if ( ! in_array($property, $this->filters)) {
return $this->locales;
}
return $this->locales;
}

return $this->filterLocaleInfo($property);
/**
* Filter the locales using the given Filter.
*
* @param \CodeZero\BrowserLocale\Filters\Filter $filter
*
* @return array
*/
public function filter(Filter $filter)
{
return $filter->filter($this->locales);
}

/**
Expand Down Expand Up @@ -167,25 +167,4 @@ protected function sortLocales()
return ($a->weight > $b->weight) ? -1 : 1;
});
}

/**
* Get a flattened array of locale information,
* containing only the requested property values.
*
* @param string $property
*
* @return array
*/
protected function filterLocaleInfo($property)
{
$filtered = [];

foreach ($this->locales as $locale) {
if ($locale->$property && ! in_array($locale->$property, $filtered)) {
$filtered[] = $locale->$property;
}
}

return $filtered;
}
}
62 changes: 62 additions & 0 deletions src/Filters/CombinedFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace CodeZero\BrowserLocale\Filters;

use CodeZero\BrowserLocale\Locale;

class CombinedFilter implements Filter
{
/**
* The filtered results.
*
* @var array
*/
protected $filtered;

/**
* Filter the locales.
*
* @param array $locales
*
* @return array
*/
public function filter(array $locales)
{
$this->filtered = [];

foreach ($locales as $locale) {
$this->filterLocale($locale);
}

return $this->filtered;
}

/**
* Filter the given Locale.
*
* @param \CodeZero\BrowserLocale\Locale $locale
*
* @return void
*/
protected function filterLocale(Locale $locale)
{
$language = substr($locale->locale, 0, 2);

$this->addLocale($locale->locale);
$this->addLocale($language);
}

/**
* Add a locale to the results array.
*
* @param string $locale
*
* @return void
*/
protected function addLocale($locale)
{
if ( ! in_array($locale, $this->filtered)) {
$this->filtered[] = $locale;
}
}
}
18 changes: 18 additions & 0 deletions src/Filters/CountryFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace CodeZero\BrowserLocale\Filters;

class CountryFilter extends PropertyFilter implements Filter
{
/**
* Filter the locales.
*
* @param array $locales
*
* @return array
*/
public function filter(array $locales)
{
return $this->filterByProperty($locales, 'country');
}
}
15 changes: 15 additions & 0 deletions src/Filters/Filter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace CodeZero\BrowserLocale\Filters;

interface Filter
{
/**
* Filter the locales.
*
* @param array $locales
*
* @return array
*/
public function filter(array $locales);
}
Loading

0 comments on commit 36246aa

Please sign in to comment.