Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
7 / 7 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| Locale | |
100.00% |
7 / 7 |
|
100.00% |
4 / 4 |
5 | |
100.00% |
1 / 1 |
| exists | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setLocaleString | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| getLocaleString | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __toString | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @author: Doug Wilbourne (dougwilbourne@gmail.com) |
| 5 | */ |
| 6 | |
| 7 | declare(strict_types=1); |
| 8 | |
| 9 | namespace pvc\intl; |
| 10 | |
| 11 | use pvc\interfaces\intl\LocaleInterface; |
| 12 | use pvc\intl\err\InvalidLocaleException; |
| 13 | use Symfony\Component\Intl\Locales; |
| 14 | |
| 15 | /** |
| 16 | * Class Locale. |
| 17 | */ |
| 18 | class Locale implements LocaleInterface |
| 19 | { |
| 20 | /** |
| 21 | * @var string |
| 22 | */ |
| 23 | protected string $localeString; |
| 24 | |
| 25 | /** |
| 26 | * exists |
| 27 | * @param string $locale |
| 28 | * @return bool |
| 29 | */ |
| 30 | public static function exists(string $locale): bool |
| 31 | { |
| 32 | return Locales::exists(\Locale::canonicalize($locale)); |
| 33 | } |
| 34 | |
| 35 | public function setLocaleString(string $localeString): void |
| 36 | { |
| 37 | /** |
| 38 | * canonicalize the string first, e.g. convert hyphens to underscores, ensure the country code is lower case |
| 39 | * and the language code is upper case (and fix any other segments of the string as well). |
| 40 | */ |
| 41 | $localeString = \Locale::canonicalize($localeString); |
| 42 | if (!self::exists($localeString)) { |
| 43 | throw new InvalidLocaleException($localeString); |
| 44 | } |
| 45 | $this->localeString = $localeString; |
| 46 | } |
| 47 | |
| 48 | public function getLocaleString(): string |
| 49 | { |
| 50 | return $this->localeString ?? \Locale::getDefault(); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * __toString |
| 55 | * @return string |
| 56 | */ |
| 57 | public function __toString(): string |
| 58 | { |
| 59 | return $this->getLocaleString(); |
| 60 | } |
| 61 | } |