Skip to content

Commit

Permalink
Adding methods to access default locale
Browse files Browse the repository at this point in the history
  • Loading branch information
simonrjones committed Nov 18, 2021
1 parent 07f5e0b commit 43248fa
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
29 changes: 29 additions & 0 deletions src/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,24 @@ public function addDefaultLocale(string $locale, array $data = [], string $direc
$this->defaultLocale = $locale;
}

/**
* Whether a default locale is set
* @return bool
*/
public function hasDefaultLocale(): bool
{
return (null !== $this->defaultLocale);
}

/**
* Return default locale, or null if not set
* @return string|null
*/
public function getDefaultLocale(): ?string
{
return $this->defaultLocale;
}

/**
* Add RTF locale for this site
* @param string $locale
Expand Down Expand Up @@ -138,6 +156,17 @@ public function getLocaleData(?string $name = null)
return null;
}

/**
* Whether a locale as the named data set
* @param string $name
* @return bool
* @throws InvalidLocaleException
*/
public function hasLocaleData(string $name): bool
{
return (null !== $this->getLocaleData($name));
}

/**
* Return data values for all locales, optionally excluding data for the current locale
*
Expand Down
19 changes: 16 additions & 3 deletions tests/SiteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,25 @@ public function testTextDirection()
public function testGetLocaleData()
{
$site = new Site();
$site->addLocale('fr', ['siteId' => 1]);
$site->addLocale('ja', ['siteId' => 2]);
$site->addLocale('fr', ['siteId' => 1, 'test' => 'one']);
$site->addLocale('ja', ['siteId' => 2, 'test' => 'two']);
$site->addLocaleRtl('ar', ['siteId' => 3]);

$site->setLocale('fr');
$this->assertSame(1, $site->getLocaleData('siteId'));
$this->assertSame(1, $site->siteId);
$this->assertNull($site->getLocaleData('test'));
$this->assertTrue($site->hasLocaleData('test'));
$this->assertSame('one', $site->getLocaleData('test'));

$site->setLocale('ja');
$this->assertSame(2, $site->siteId);
$this->assertTrue($site->hasLocaleData('test'));
$this->assertSame('two', $site->getLocaleData('test'));

$site->setLocale('ar');
$this->assertSame(3, $site->siteId);
$this->assertFalse($site->hasLocaleData('test'));
$this->assertNull($site->getLocaleData('test'));
}

public function testGetData()
Expand Down Expand Up @@ -145,14 +150,22 @@ public function testDefaultLocale()
{
$site = new Site();
$site->addLocale('en');
$this->assertFalse($site->hasDefaultLocale());

$site->addDefaultLocale('fr');
$this->assertTrue($site->hasDefaultLocale());
$this->assertSame('fr', $site->getDefaultLocale());

$this->assertSame('fr', $site->getLocale());

// Still fr since once getLocale run sets to default locale
$site->addLocale('ja');
$site->addDefaultLocale('de');
$this->assertTrue($site->hasDefaultLocale());
$this->assertSame('fr', $site->getLocale());

// But getDefaultLocale returns whatever is currently set
$this->assertSame('de', $site->getDefaultLocale());
}

public function testGetTextDirection()
Expand Down

0 comments on commit 43248fa

Please sign in to comment.