Skip to content

Commit 87e3452

Browse files
author
Erik van Velzen
committed
Add helper function to get the supported locales excluding mappings
1 parent 2a8a0a6 commit 87e3452

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

src/SlmLocale/Locale/Detector.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,14 @@ class Detector implements EventManagerAwareInterface
6767
/**
6868
* Optional list of supported locales
6969
*
70-
* @var array
70+
* @var string[]
7171
*/
7272
protected $supported;
7373

7474
/**
7575
* Optional list of locale mappings
7676
*
77-
* @var array
77+
* @var array & string[string]
7878
*/
7979
protected $mappings;
8080

@@ -128,6 +128,21 @@ public function hasSupported()
128128
return is_array($this->supported) && count($this->supported);
129129
}
130130

131+
/**
132+
* get the supported locales excluding those which are mapped to another locale
133+
*
134+
* @return string[]
135+
*/
136+
public function getMainSupportedLocales()
137+
{
138+
$supported = $this->getSupported() ?: [];
139+
$mappings = $this->getMappings() ?: [];
140+
141+
$mappedFrom = array_keys($mappings);
142+
143+
return array_values(array_diff($supported, $mappedFrom));
144+
}
145+
131146
public function getMappings()
132147
{
133148
return $this->mappings;

tests/SlmLocaleTest/Locale/DetectorTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,32 @@ public function testEmptyMappingsListIndicatesNoMappingsList()
199199
$this->assertFalse($detector->hasMappings());
200200
}
201201

202+
public function testMainSupportedLocalesExcludesMappings()
203+
{
204+
$detector = new Detector();
205+
$detector->setSupported([
206+
'nl',
207+
'nl_NL',
208+
]);
209+
210+
$detector->setMappings([
211+
'nl' => 'nl_NL',
212+
]);
213+
214+
$this->assertEquals(['nl_NL'], $detector->getMainSupportedLocales());
215+
}
216+
217+
public function testMainSupportedLocalesWithoutMappings()
218+
{
219+
$detector = new Detector();
220+
$detector->setSupported([
221+
'nl',
222+
'nl_NL',
223+
]);
224+
225+
$this->assertEquals(['nl', 'nl_NL'], $detector->getMainSupportedLocales());
226+
}
227+
202228
public function testStrategyAttachesToEventManager()
203229
{
204230
$detector = new Detector();

0 commit comments

Comments
 (0)