Skip to content

Commit

Permalink
LanguageTag: added isCountry() & getCountry()
Browse files Browse the repository at this point in the history
  • Loading branch information
janpecha committed Feb 15, 2024
1 parent 5437a9a commit 0d49b5f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/LanguageTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,27 @@ public function getLang()
}


/**
* @param string $country
* @return bool
*/
public function isCountry($country)
{
return substr_compare($this->tag, strtoupper($country), 3, 2, FALSE) === 0;
}


/**
* @return non-empty-string
*/
public function getCountry()
{
$country = substr($this->tag, 3, 2);
assert($country !== '');
return $country;
}


/**
* @return non-empty-string
*/
Expand Down
8 changes: 8 additions & 0 deletions tests/Translator/LanguageTag.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,21 @@ require __DIR__ . '/../bootstrap.php';
test('valid', function () {
$tag = new LanguageTag('cs-CZ');
Assert::same('cs-CZ', $tag->toString());
Assert::same('cs', $tag->getLang());
Assert::true($tag->isLang('cs'));
Assert::false($tag->isLang('en'));
Assert::same('CZ', $tag->getCountry());
Assert::true($tag->isCountry('CZ'));
Assert::false($tag->isCountry('US'));

$tag = new LanguageTag('en-US');
Assert::same('en-US', $tag->toString());
Assert::same('en', $tag->getLang());
Assert::false($tag->isLang('cs'));
Assert::true($tag->isLang('en'));
Assert::same('US', $tag->getCountry());
Assert::false($tag->isCountry('cz'));
Assert::true($tag->isCountry('us'));
});


Expand Down

0 comments on commit 0d49b5f

Please sign in to comment.