Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be array and maybe add an example or/and description

*/
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