diff --git a/src/Locale.php b/src/Locale.php index 8cafabf..b2c4bd1 100644 --- a/src/Locale.php +++ b/src/Locale.php @@ -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; @@ -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); + } } /** @@ -72,7 +77,7 @@ public function getAuthors(): array if (!isset($this->config['authors'])) { return []; } else { - return $this->config['authors']; + return (array) $this->config['authors']; } } @@ -99,7 +104,7 @@ public function getIdentifier(): string /** * Return the raw configuration data. * - * @return (array|string)[] + * @return string[] */ public function getConfig(): array { @@ -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; } diff --git a/tests/LocaleTest.php b/tests/LocaleTest.php index 627419f..b4854b3 100644 --- a/tests/LocaleTest.php +++ b/tests/LocaleTest.php @@ -18,8 +18,10 @@ class LocaleTest extends TestCase { + /** @var string */ protected $basePath; + /** @var ResourceLocator */ protected $locator; public function setUp() @@ -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 @@ -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()); } /** @@ -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 @@ -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); @@ -199,10 +244,4 @@ public function testConstructorWithCustomFile() $this->assertSame(1, $locale->getPluralRule()); $this->assertSame('de_DE', $locale->getRegionalName()); } - - /* - TODO : - - fr_CA - - Null Parent - */ } diff --git a/tests/data/sprinkles/fr_CA/locale/fr_CA/locale.yaml b/tests/data/sprinkles/fr_CA/locale/fr_CA/locale.yaml index 1aace6a..f1f901b 100644 --- a/tests/data/sprinkles/fr_CA/locale/fr_CA/locale.yaml +++ b/tests/data/sprinkles/fr_CA/locale/fr_CA/locale.yaml @@ -1,8 +1,4 @@ name: French Canadian regional: Français Canadien -authors: - - Foo Bar - - Bar Foo -plural_rule: 2 parents: - fr_FR