Skip to content
This repository has been archived by the owner on Apr 28, 2024. It is now read-only.

Commit

Permalink
Locale inherit config from parent.
Browse files Browse the repository at this point in the history
Ref #7
  • Loading branch information
lcharette committed Feb 18, 2020
1 parent ccea4cd commit 78fd865
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 20 deletions.
13 changes: 9 additions & 4 deletions src/Locale.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Locale implements LocaleInterface
protected $configFile = '';

/**
* @var array Locale config data, loaded from the locale YAML file
* @var string[] Locale config data, loaded from the locale YAML file
*/
protected $config;

Expand Down Expand Up @@ -60,6 +60,11 @@ protected function loadConfig(): void
{
$loader = new YamlFileLoader($this->configFile);
$this->config = $loader->load(false);

// Load nested locales config
foreach ($this->getDependentLocales() as $locale) {
$this->config = array_merge($locale->getConfig(), $this->config);
}
}

/**
Expand All @@ -72,7 +77,7 @@ public function getAuthors(): array
if (!isset($this->config['authors'])) {
return [];
} else {
return $this->config['authors'];
return (array) $this->config['authors'];
}
}

Expand All @@ -99,7 +104,7 @@ public function getIdentifier(): string
/**
* Return the raw configuration data.
*
* @return (array|string)[]
* @return string[]
*/
public function getConfig(): array
{
Expand Down Expand Up @@ -159,7 +164,7 @@ public function getName(): string
public function getPluralRule(): int
{
if (isset($this->config['plural_rule'])) {
return $this->config['plural_rule'];
return (int) $this->config['plural_rule'];
} else {
return 1;
}
Expand Down
63 changes: 51 additions & 12 deletions tests/LocaleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@

class LocaleTest extends TestCase
{
/** @var string */
protected $basePath;

/** @var ResourceLocator */
protected $locator;

public function setUp()
Expand All @@ -32,7 +34,7 @@ public function setUp()
// Add them one at a time to simulate how they are added in SprinkleManager
$this->locator->registerLocation('core');
$this->locator->registerLocation('account');
$this->locator->registerLocation('admin');
$this->locator->registerLocation('fr_CA');
}

public function testConstructor(): Locale
Expand Down Expand Up @@ -137,10 +139,6 @@ public function testGetDetails(Locale $locale): void
//getDependentLocalesIdentifier
$this->assertIsArray($locale->getDependentLocalesIdentifier());
$this->assertSame(['en_US'], $locale->getDependentLocalesIdentifier());

//getPluralRule
$this->assertIsInt($locale->getPluralRule());
$this->assertSame(2, $locale->getPluralRule());
}

/**
Expand Down Expand Up @@ -173,6 +171,41 @@ public function testGetPluralRuleWithNoRule(): void
$this->assertSame(1, $locale->getPluralRule());
}

/**
* @depends testGetDetails
* @depends testGetAuthors
*/
public function testGetDetailsWithInheritance(): void
{
$locale = new Locale('fr_CA');

//getName
$this->assertIsString($locale->getName());
$this->assertSame('French Canadian', $locale->getName());

//getRegionalName
$this->assertIsString($locale->getRegionalName());
$this->assertSame('Français Canadien', $locale->getRegionalName());

//getDependentLocalesIdentifier
$this->assertIsArray($locale->getDependentLocalesIdentifier());
$this->assertSame(['fr_FR'], $locale->getDependentLocalesIdentifier());

//getAuthors
$this->assertSame(['Foo Bar', 'Bar Foo'], $locale->getAuthors());
}

/**
* @depends testConstructorWithNotPath
* @depends testGetPluralRule
*/
public function testGetPluralRuleWithInheritance(): void
{
$locale = new Locale('fr_CA');
$this->assertIsInt($locale->getPluralRule());
$this->assertSame(2, $locale->getPluralRule());
}

/**
* @depends testConstructor
* @depends testGetDetails
Expand All @@ -184,7 +217,19 @@ public function testGetDependentLocales(Locale $locale): void
$this->assertInstanceOf(LocaleInterface::class, $result[0]);
}

public function testConstructorWithCustomFile()
/**
* @depends testGetDependentLocales
*/
public function testGetDependentLocalesWithNullParent(): void
{
$locale = new Locale('es_ES');

$result = $locale->getDependentLocales();
$this->assertIsArray($result);
$this->assertEmpty($result);
}

public function testConstructorWithCustomFile(): void
{
$locale = new Locale('de_DE', 'locale://de_DE/foo.yaml');
$this->assertInstanceOf(LocaleInterface::class, $locale);
Expand All @@ -199,10 +244,4 @@ public function testConstructorWithCustomFile()
$this->assertSame(1, $locale->getPluralRule());
$this->assertSame('de_DE', $locale->getRegionalName());
}

/*
TODO :
- fr_CA
- Null Parent
*/
}
4 changes: 0 additions & 4 deletions tests/data/sprinkles/fr_CA/locale/fr_CA/locale.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
name: French Canadian
regional: Français Canadien
authors:
- Foo Bar
- Bar Foo
plural_rule: 2
parents:
- fr_FR

0 comments on commit 78fd865

Please sign in to comment.