Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| IntlDateFrmtrFactory | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
5 | |
100.00% |
1 / 1 |
| makeFrmtr | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
5 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @author: Doug Wilbourne (dougwilbourne@gmail.com) |
| 5 | */ |
| 6 | declare(strict_types=1); |
| 7 | |
| 8 | namespace pvc\intl; |
| 9 | |
| 10 | use DateTimeZone; |
| 11 | use IntlDateFormatter; |
| 12 | use pvc\intl\err\InvalidCalendarTypeException; |
| 13 | use pvc\intl\err\InvalidDateTimeTypeException; |
| 14 | |
| 15 | /** |
| 16 | * Class IntlDateFrmtrFactory |
| 17 | * a simple wrapper so that pvc exceptions are thrown for invalid arguments to the makeFrmtr method |
| 18 | */ |
| 19 | class IntlDateFrmtrFactory |
| 20 | { |
| 21 | /** |
| 22 | * @var int[] |
| 23 | */ |
| 24 | protected static array $validDateTimeTypes = [ |
| 25 | IntlDateFormatter::NONE, |
| 26 | IntlDateFormatter::FULL, |
| 27 | IntlDateFormatter::LONG, |
| 28 | IntlDateFormatter::MEDIUM, |
| 29 | IntlDateFormatter::SHORT, |
| 30 | ]; |
| 31 | |
| 32 | /** |
| 33 | * @var int[] |
| 34 | * there are other calendars that the international date formatter will use, but they must be specified in the |
| 35 | * locale. See the documentation for the IntlDateFormatter class. |
| 36 | */ |
| 37 | protected static array $validCalendarTypes = [ |
| 38 | IntlDateFormatter::TRADITIONAL, |
| 39 | IntlDateFormatter::GREGORIAN, |
| 40 | ]; |
| 41 | |
| 42 | /** |
| 43 | * makeFrmtr |
| 44 | * @param Locale $locale |
| 45 | * @param int $dateFormat |
| 46 | * @param int $timeFormat |
| 47 | * @param DateTimeZone $tz |
| 48 | * @param int $calendar |null |
| 49 | * @return IntlDateFormatter |
| 50 | * @throws InvalidCalendarTypeException |
| 51 | * @throws InvalidDateTimeTypeException |
| 52 | */ |
| 53 | public static function makeFrmtr( |
| 54 | Locale $locale, |
| 55 | int $dateFormat, |
| 56 | int $timeFormat, |
| 57 | DateTimeZone $tz, |
| 58 | int $calendar = null |
| 59 | ): IntlDateFormatter { |
| 60 | if (!in_array($dateFormat, self::$validDateTimeTypes)) { |
| 61 | throw new InvalidDateTimeTypeException(); |
| 62 | } |
| 63 | if (!in_array($timeFormat, self::$validDateTimeTypes)) { |
| 64 | throw new InvalidDateTimeTypeException(); |
| 65 | } |
| 66 | if (is_null($calendar)) { |
| 67 | $calendar = IntlDateFormatter::GREGORIAN; |
| 68 | } |
| 69 | if (!in_array($calendar, self::$validCalendarTypes)) { |
| 70 | throw new InvalidCalendarTypeException(); |
| 71 | } |
| 72 | return new IntlDateFormatter((string)$locale, $dateFormat, $timeFormat, $tz, $calendar); |
| 73 | } |
| 74 | } |