From 40af6708874abb016a428aac1d5b49876de08cf1 Mon Sep 17 00:00:00 2001 From: Erik van Velzen Date: Wed, 30 May 2018 22:24:16 +0200 Subject: [PATCH] Add helper function to get the supported locales excluding mappings --- src/SlmLocale/Locale/Detector.php | 19 +++++++++++++-- tests/SlmLocaleTest/Locale/DetectorTest.php | 26 +++++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/SlmLocale/Locale/Detector.php b/src/SlmLocale/Locale/Detector.php index 45c9227..d8ac2dc 100644 --- a/src/SlmLocale/Locale/Detector.php +++ b/src/SlmLocale/Locale/Detector.php @@ -67,14 +67,14 @@ class Detector implements EventManagerAwareInterface /** * Optional list of supported locales * - * @var array + * @var string[] */ protected $supported; /** * Optional list of locale mappings * - * @var array + * @var array & string[string] */ protected $mappings; @@ -128,6 +128,21 @@ public function hasSupported() return is_array($this->supported) && count($this->supported); } + /** + * get the supported locales excluding those which are mapped to another locale + * + * @return string[] + */ + public function getMainSupportedLocales() + { + $supported = $this->getSupported() ?: []; + $mappings = $this->getMappings() ?: []; + + $mappedFrom = array_keys($mappings); + + return array_values(array_diff($supported, $mappedFrom)); + } + public function getMappings() { return $this->mappings; diff --git a/tests/SlmLocaleTest/Locale/DetectorTest.php b/tests/SlmLocaleTest/Locale/DetectorTest.php index 289c053..b1aefe2 100644 --- a/tests/SlmLocaleTest/Locale/DetectorTest.php +++ b/tests/SlmLocaleTest/Locale/DetectorTest.php @@ -199,6 +199,32 @@ public function testEmptyMappingsListIndicatesNoMappingsList() $this->assertFalse($detector->hasMappings()); } + public function testMainSupportedLocalesExcludesMappings() + { + $detector = new Detector(); + $detector->setSupported([ + 'nl', + 'nl_NL', + ]); + + $detector->setMappings([ + 'nl' => 'nl_NL', + ]); + + $this->assertEquals(['nl_NL'], $detector->getMainSupportedLocales()); + } + + public function testMainSupportedLocalesWithoutMappings() + { + $detector = new Detector(); + $detector->setSupported([ + 'nl', + 'nl_NL', + ]); + + $this->assertEquals(['nl', 'nl_NL'], $detector->getMainSupportedLocales()); + } + public function testStrategyAttachesToEventManager() { $detector = new Detector();