From 36246aa6d07a20f20dca8725cf39cd3dbec80570 Mon Sep 17 00:00:00 2001 From: Ivan Vermeyen Date: Sun, 1 Apr 2018 00:56:05 +0200 Subject: [PATCH] Introduce Filter classes --- CHANGELOG.md | 5 ++ README.md | 23 ++++-- composer.json | 1 + composer.lock | 115 ++++++++++++++++++++++++++- src/BrowserLocale.php | 51 ++++-------- src/Filters/CombinedFilter.php | 62 +++++++++++++++ src/Filters/CountryFilter.php | 18 +++++ src/Filters/Filter.php | 15 ++++ src/Filters/LanguageFilter.php | 18 +++++ src/Filters/LocaleFilter.php | 18 +++++ src/Filters/PropertyFilter.php | 27 +++++++ src/Filters/WeightFilter.php | 18 +++++ tests/BrowserLocaleTest.php | 59 +++++--------- tests/Filters/CombinedFilterTest.php | 30 +++++++ tests/Filters/CountryFilterTest.php | 30 +++++++ tests/Filters/LanguageFilterTest.php | 30 +++++++ tests/Filters/LocaleFilterTest.php | 30 +++++++ tests/Filters/WeightFilterTest.php | 30 +++++++ 18 files changed, 497 insertions(+), 83 deletions(-) create mode 100644 src/Filters/CombinedFilter.php create mode 100644 src/Filters/CountryFilter.php create mode 100644 src/Filters/Filter.php create mode 100644 src/Filters/LanguageFilter.php create mode 100644 src/Filters/LocaleFilter.php create mode 100644 src/Filters/PropertyFilter.php create mode 100644 src/Filters/WeightFilter.php create mode 100644 tests/Filters/CombinedFilterTest.php create mode 100644 tests/Filters/CountryFilterTest.php create mode 100644 tests/Filters/LanguageFilterTest.php create mode 100644 tests/Filters/LocaleFilterTest.php create mode 100644 tests/Filters/WeightFilterTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index e926cd5..04a3115 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 59700e8..b8b7a4c 100644 --- a/README.md +++ b/README.md @@ -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 ``` diff --git a/composer.json b/composer.json index be63720..3d8f9bc 100644 --- a/composer.json +++ b/composer.json @@ -22,6 +22,7 @@ }, "require-dev": { "illuminate/support": "^5.5", + "mockery/mockery": "^1.0", "phpunit/phpunit": "^6.0" }, "scripts": { diff --git a/composer.lock b/composer.lock index 415711d..87d7a44 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "d01ea0070ad492e7cbb0bb15d49183e8", + "content-hash": "664c893063b3f35974d5705e12f29f0a", "packages": [], "packages-dev": [ { @@ -128,6 +128,54 @@ ], "time": "2017-07-22T11:58:36+00:00" }, + { + "name": "hamcrest/hamcrest-php", + "version": "v2.0.0", + "source": { + "type": "git", + "url": "https://github.com/hamcrest/hamcrest-php.git", + "reference": "776503d3a8e85d4f9a1148614f95b7a608b046ad" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/hamcrest/hamcrest-php/zipball/776503d3a8e85d4f9a1148614f95b7a608b046ad", + "reference": "776503d3a8e85d4f9a1148614f95b7a608b046ad", + "shasum": "" + }, + "require": { + "php": "^5.3|^7.0" + }, + "replace": { + "cordoval/hamcrest-php": "*", + "davedevelopment/hamcrest-php": "*", + "kodova/hamcrest-php": "*" + }, + "require-dev": { + "phpunit/php-file-iterator": "1.3.3", + "phpunit/phpunit": "~4.0", + "satooshi/php-coveralls": "^1.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.0-dev" + } + }, + "autoload": { + "classmap": [ + "hamcrest" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD" + ], + "description": "This is the PHP port of Hamcrest Matchers", + "keywords": [ + "test" + ], + "time": "2016-01-20T08:20:44+00:00" + }, { "name": "illuminate/contracts", "version": "v5.6.15", @@ -229,6 +277,71 @@ "homepage": "https://laravel.com", "time": "2018-03-29T15:55:16+00:00" }, + { + "name": "mockery/mockery", + "version": "1.0", + "source": { + "type": "git", + "url": "https://github.com/mockery/mockery.git", + "reference": "1bac8c362b12f522fdd1f1fa3556284c91affa38" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/mockery/mockery/zipball/1bac8c362b12f522fdd1f1fa3556284c91affa38", + "reference": "1bac8c362b12f522fdd1f1fa3556284c91affa38", + "shasum": "" + }, + "require": { + "hamcrest/hamcrest-php": "~2.0", + "lib-pcre": ">=7.0", + "php": ">=5.6.0" + }, + "require-dev": { + "phpunit/phpunit": "~5.7|~6.1" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-0": { + "Mockery": "library/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Pádraic Brady", + "email": "padraic.brady@gmail.com", + "homepage": "http://blog.astrumfutura.com" + }, + { + "name": "Dave Marshall", + "email": "dave.marshall@atstsolutions.co.uk", + "homepage": "http://davedevelopment.co.uk" + } + ], + "description": "Mockery is a simple yet flexible PHP mock object framework for use in unit testing with PHPUnit, PHPSpec or any other testing framework. Its core goal is to offer a test double framework with a succinct API capable of clearly defining all possible object operations and interactions using a human readable Domain Specific Language (DSL). Designed as a drop in alternative to PHPUnit's phpunit-mock-objects library, Mockery is easy to integrate with PHPUnit and can operate alongside phpunit-mock-objects without the World ending.", + "homepage": "http://github.com/mockery/mockery", + "keywords": [ + "BDD", + "TDD", + "library", + "mock", + "mock objects", + "mockery", + "stub", + "test", + "test double", + "testing" + ], + "time": "2017-10-06T16:20:43+00:00" + }, { "name": "myclabs/deep-copy", "version": "1.7.0", diff --git a/src/BrowserLocale.php b/src/BrowserLocale.php index 5ac4113..29fbd21 100644 --- a/src/BrowserLocale.php +++ b/src/BrowserLocale.php @@ -2,6 +2,8 @@ namespace CodeZero\BrowserLocale; +use CodeZero\BrowserLocale\Filters\Filter; + class BrowserLocale { /** @@ -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. * @@ -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); } /** @@ -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; - } } diff --git a/src/Filters/CombinedFilter.php b/src/Filters/CombinedFilter.php new file mode 100644 index 0000000..36df58b --- /dev/null +++ b/src/Filters/CombinedFilter.php @@ -0,0 +1,62 @@ +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; + } + } +} diff --git a/src/Filters/CountryFilter.php b/src/Filters/CountryFilter.php new file mode 100644 index 0000000..467b173 --- /dev/null +++ b/src/Filters/CountryFilter.php @@ -0,0 +1,18 @@ +filterByProperty($locales, 'country'); + } +} diff --git a/src/Filters/Filter.php b/src/Filters/Filter.php new file mode 100644 index 0000000..fc2a143 --- /dev/null +++ b/src/Filters/Filter.php @@ -0,0 +1,15 @@ +filterByProperty($locales, 'language'); + } +} diff --git a/src/Filters/LocaleFilter.php b/src/Filters/LocaleFilter.php new file mode 100644 index 0000000..65d960f --- /dev/null +++ b/src/Filters/LocaleFilter.php @@ -0,0 +1,18 @@ +filterByProperty($locales, 'locale'); + } +} diff --git a/src/Filters/PropertyFilter.php b/src/Filters/PropertyFilter.php new file mode 100644 index 0000000..e2257b5 --- /dev/null +++ b/src/Filters/PropertyFilter.php @@ -0,0 +1,27 @@ +$property) && ! in_array($locale->$property, $filtered)) { + $filtered[] = $locale->$property; + } + } + + return $filtered; + } +} diff --git a/src/Filters/WeightFilter.php b/src/Filters/WeightFilter.php new file mode 100644 index 0000000..9d05e98 --- /dev/null +++ b/src/Filters/WeightFilter.php @@ -0,0 +1,18 @@ +filterByProperty($locales, 'weight'); + } +} diff --git a/tests/BrowserLocaleTest.php b/tests/BrowserLocaleTest.php index 0945531..7c25bcd 100644 --- a/tests/BrowserLocaleTest.php +++ b/tests/BrowserLocaleTest.php @@ -3,6 +3,8 @@ namespace CodeZero\BrowserLocale\Tests; use CodeZero\BrowserLocale\BrowserLocale; +use CodeZero\BrowserLocale\Filters\Filter; +use Mockery; use PHPUnit\Framework\TestCase; class BrowserLocaleTest extends TestCase @@ -58,46 +60,6 @@ public function it_returns_an_array_of_all_preferred_locales() $this->assertEquals(0.4, $locales[3]->weight); } - /** @test */ - public function it_returns_a_simple_array_of_locale_locales() - { - $browser = new BrowserLocale('en-US,en;q=0.8,nl-NL;q=0.6'); - - $locales = $browser->getLocales('locale'); - - $this->assertEquals(['en-US', 'en', 'nl-NL'], $locales); - } - - /** @test */ - public function it_returns_a_simple_array_of_language_codes() - { - $browser = new BrowserLocale('en-US,en;q=0.8,nl-NL;q=0.6'); - - $locales = $browser->getLocales('language'); - - $this->assertEquals(['en', 'nl'], $locales); - } - - /** @test */ - public function it_returns_a_simple_array_of_country_codes() - { - $browser = new BrowserLocale('en-US,en;q=0.8,nl-NL;q=0.6,nl;q=0.4'); - - $locales = $browser->getLocales('country'); - - $this->assertEquals(['US', 'NL'], $locales); - } - - /** @test */ - public function it_returns_a_simple_array_of_weight_values() - { - $browser = new BrowserLocale('en-US,en;q=0.8,nl-NL;q=0.6,nl;q=0.4'); - - $locales = $browser->getLocales('weight'); - - $this->assertEquals([1.0, 0.8, 0.6, 0.4], $locales); - } - /** @test */ public function it_returns_null_or_an_empty_array_if_no_locale_exists() { @@ -123,4 +85,21 @@ public function it_returns_null_or_an_empty_array_if_null_is_passed_in() $this->assertEquals([], $browser->getLocales('country')); $this->assertEquals([], $browser->getLocales('weight')); } + + /** @test */ + public function it_applies_a_filters_to_the_locales() + { + $browser = new BrowserLocale(''); + + $filter = Mockery::mock(Filter::class) + ->expects() + ->filter([]) + ->once() + ->andReturns(['result']) + ->getMock(); + + $locales = $browser->filter($filter); + + $this->assertEquals(['result'], $locales); + } } diff --git a/tests/Filters/CombinedFilterTest.php b/tests/Filters/CombinedFilterTest.php new file mode 100644 index 0000000..ec34f0d --- /dev/null +++ b/tests/Filters/CombinedFilterTest.php @@ -0,0 +1,30 @@ +filter(new CombinedFilter); + + $this->assertEquals(['en-US', 'en', 'nl-NL', 'nl'], $locales); + } + + /** @test */ + public function it_returns_an_empty_array_if_no_locales_exist() + { + $browser = new BrowserLocale(''); + + $locales = $browser->filter(new CombinedFilter); + + $this->assertEquals([], $locales); + } +} diff --git a/tests/Filters/CountryFilterTest.php b/tests/Filters/CountryFilterTest.php new file mode 100644 index 0000000..c861714 --- /dev/null +++ b/tests/Filters/CountryFilterTest.php @@ -0,0 +1,30 @@ +filter(new CountryFilter); + + $this->assertEquals(['US', 'NL'], $locales); + } + + /** @test */ + public function it_returns_an_empty_array_if_no_locales_exist() + { + $browser = new BrowserLocale(''); + + $locales = $browser->filter(new CountryFilter); + + $this->assertEquals([], $locales); + } +} diff --git a/tests/Filters/LanguageFilterTest.php b/tests/Filters/LanguageFilterTest.php new file mode 100644 index 0000000..132c1a6 --- /dev/null +++ b/tests/Filters/LanguageFilterTest.php @@ -0,0 +1,30 @@ +filter(new LanguageFilter); + + $this->assertEquals(['en', 'nl'], $locales); + } + + /** @test */ + public function it_returns_an_empty_array_if_no_locales_exist() + { + $browser = new BrowserLocale(''); + + $locales = $browser->filter(new LanguageFilter); + + $this->assertEquals([], $locales); + } +} diff --git a/tests/Filters/LocaleFilterTest.php b/tests/Filters/LocaleFilterTest.php new file mode 100644 index 0000000..a96d960 --- /dev/null +++ b/tests/Filters/LocaleFilterTest.php @@ -0,0 +1,30 @@ +filter(new LocaleFilter); + + $this->assertEquals(['en-US', 'en', 'nl-NL'], $locales); + } + + /** @test */ + public function it_returns_an_empty_array_if_no_locales_exist() + { + $browser = new BrowserLocale(''); + + $locales = $browser->filter(new LocaleFilter); + + $this->assertEquals([], $locales); + } +} diff --git a/tests/Filters/WeightFilterTest.php b/tests/Filters/WeightFilterTest.php new file mode 100644 index 0000000..9b691c5 --- /dev/null +++ b/tests/Filters/WeightFilterTest.php @@ -0,0 +1,30 @@ +filter(new WeightFilter); + + $this->assertEquals([1.0, 0.8, 0.6, 0.4], $locales); + } + + /** @test */ + public function it_returns_an_empty_array_if_no_locales_exist() + { + $browser = new BrowserLocale(''); + + $locales = $browser->filter(new WeightFilter); + + $this->assertEquals([], $locales); + } +}