Skip to content

Commit

Permalink
Add helper function to get the supported locales excluding mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik van Velzen committed Jun 12, 2018
1 parent 2a8a0a6 commit 40af670
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/SlmLocale/Locale/Detector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
26 changes: 26 additions & 0 deletions tests/SlmLocaleTest/Locale/DetectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 40af670

Please sign in to comment.