|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Brick\DateTime\Formatter; |
| 6 | + |
| 7 | +use Brick\DateTime\LocalDate; |
| 8 | +use Brick\DateTime\LocalDateTime; |
| 9 | +use Brick\DateTime\LocalTime; |
| 10 | +use Brick\DateTime\ZonedDateTime; |
| 11 | +use DateTimeZone; |
| 12 | +use IntlDateFormatter; |
| 13 | +use IntlDatePatternGenerator; |
| 14 | + |
| 15 | +use function array_keys; |
| 16 | +use function extension_loaded; |
| 17 | +use function get_class; |
| 18 | +use function in_array; |
| 19 | +use function sprintf; |
| 20 | +use function str_split; |
| 21 | + |
| 22 | +use const PHP_VERSION_ID; |
| 23 | + |
| 24 | +/** |
| 25 | + * Formats the value using the Intl extension. |
| 26 | + */ |
| 27 | +final class IntlFormatter implements DateTimeFormatter |
| 28 | +{ |
| 29 | + public const FULL = IntlDateFormatter::FULL; |
| 30 | + public const LONG = IntlDateFormatter::LONG; |
| 31 | + public const MEDIUM = IntlDateFormatter::MEDIUM; |
| 32 | + public const SHORT = IntlDateFormatter::SHORT; |
| 33 | + |
| 34 | + private string $locale; |
| 35 | + |
| 36 | + private int $dateFormat; |
| 37 | + |
| 38 | + private int $timeFormat; |
| 39 | + |
| 40 | + private string $pattern; |
| 41 | + |
| 42 | + private function __construct(string $locale, int $dateFormat, int $timeFormat, string $pattern) |
| 43 | + { |
| 44 | + $this->locale = $locale; |
| 45 | + $this->dateFormat = $dateFormat; |
| 46 | + $this->timeFormat = $timeFormat; |
| 47 | + $this->pattern = $pattern; |
| 48 | + |
| 49 | + if (! extension_loaded('intl')) { |
| 50 | + throw new DateTimeFormatException('IntlFormatter requires ext-intl to be installed and enabled.'); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Returns a formatter of given type for a date value. |
| 56 | + */ |
| 57 | + public static function ofDate(string $locale, int $format): self |
| 58 | + { |
| 59 | + return new self($locale, $format, IntlDateFormatter::NONE, ''); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Returns a formatter of given type for a time value. |
| 64 | + */ |
| 65 | + public static function ofTime(string $locale, int $format): self |
| 66 | + { |
| 67 | + return new self($locale, IntlDateFormatter::NONE, $format, ''); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Returns a formatter of given type for a date-time value. |
| 72 | + */ |
| 73 | + public static function ofDateTime(string $locale, int $dateFormat, int $timeFormat): self |
| 74 | + { |
| 75 | + return new self($locale, $dateFormat, $timeFormat, ''); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Returns a formatter with given ICU SimpleFormat pattern. |
| 80 | + */ |
| 81 | + public static function ofPattern(string $locale, string $pattern): self |
| 82 | + { |
| 83 | + return new self($locale, IntlDateFormatter::NONE, IntlDateFormatter::NONE, $pattern); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Returns a formatter with a pattern that best matches given skeleton. |
| 88 | + */ |
| 89 | + public static function ofSkeleton(string $locale, string $skeleton): self |
| 90 | + { |
| 91 | + if (PHP_VERSION_ID < 80100) { |
| 92 | + throw new DateTimeFormatException('IntlFormatter::ofSkeleton() is only available in PHP 8.1 and above.'); |
| 93 | + } |
| 94 | + |
| 95 | + $generator = new IntlDatePatternGenerator($locale); |
| 96 | + $pattern = $generator->getBestPattern($skeleton); |
| 97 | + |
| 98 | + if ($pattern === false) { |
| 99 | + throw new DateTimeFormatException('Failed to resolve the best formatting pattern for given locale and skeleton.'); |
| 100 | + } |
| 101 | + |
| 102 | + return self::ofPattern($locale, $pattern); |
| 103 | + } |
| 104 | + |
| 105 | + public function format(DateTimeFormatContext $context): string |
| 106 | + { |
| 107 | + $value = $context->getValue(); |
| 108 | + |
| 109 | + if ($this->dateFormat !== IntlDateFormatter::NONE && $value instanceof LocalTime) { |
| 110 | + throw new DateTimeFormatException('IntlFormatter with a date part cannot be used to format Brick\DateTime\LocalTime.'); |
| 111 | + } |
| 112 | + |
| 113 | + if ($this->timeFormat !== IntlDateFormatter::NONE && $value instanceof LocalDate) { |
| 114 | + throw new DateTimeFormatException('IntlFormatter with a time part cannot be used to format Brick\DateTime\LocalDate.'); |
| 115 | + } |
| 116 | + |
| 117 | + if (($this->timeFormat === self::FULL || $this->timeFormat === self::LONG) && ! ($value instanceof ZonedDateTime)) { |
| 118 | + throw new DateTimeFormatException(sprintf('IntlFormatter with a long or full time part cannot be used to format %s.', get_class($value))); |
| 119 | + } |
| 120 | + |
| 121 | + if ($this->pattern !== '') { |
| 122 | + self::checkPattern($this->pattern, $value); |
| 123 | + } |
| 124 | + |
| 125 | + $timeZone = $value instanceof ZonedDateTime ? $value->getTimeZone()->toNativeDateTimeZone() : new DateTimeZone('UTC'); |
| 126 | + $formatter = new IntlDateFormatter($this->locale, $this->dateFormat, $this->timeFormat, $timeZone, null, $this->pattern); |
| 127 | + |
| 128 | + return $formatter->format($value->toNativeDateTimeImmutable()); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * @param LocalDate|LocalDateTime|LocalTime|ZonedDateTime $value |
| 133 | + */ |
| 134 | + private static function checkPattern(string $pattern, $value): void |
| 135 | + { |
| 136 | + $supportedTypesMap = [ |
| 137 | + LocalDate::class => true, |
| 138 | + LocalDateTime::class => true, |
| 139 | + LocalTime::class => true, |
| 140 | + ZonedDateTime::class => true, |
| 141 | + ]; |
| 142 | + |
| 143 | + $inString = false; |
| 144 | + foreach (str_split($pattern) as $character) { |
| 145 | + if ($character === '\'') { |
| 146 | + if ($inString) { |
| 147 | + $inString = false; |
| 148 | + |
| 149 | + continue; |
| 150 | + } |
| 151 | + |
| 152 | + $inString = true; |
| 153 | + |
| 154 | + continue; |
| 155 | + } |
| 156 | + |
| 157 | + if ($inString) { |
| 158 | + continue; |
| 159 | + } |
| 160 | + |
| 161 | + if (in_array($character, ['G', 'y', 'Y', 'u', 'U', 'r', 'Q', 'q', 'M', 'L', 'w', 'W', 'd', 'D', 'F', 'g', 'E', 'e', 'c'], true)) { |
| 162 | + $supportedTypesMap[LocalTime::class] = false; |
| 163 | + } |
| 164 | + |
| 165 | + if (in_array($character, ['a', 'h', 'H', 'k', 'K', 'm', 's', 'S', 'A'], true)) { |
| 166 | + $supportedTypesMap[LocalDate::class] = false; |
| 167 | + } |
| 168 | + |
| 169 | + if (in_array($character, ['z', 'Z', 'O', 'v', 'V', 'X', 'x'], true)) { |
| 170 | + $supportedTypesMap[LocalDate::class] = false; |
| 171 | + $supportedTypesMap[LocalDateTime::class] = false; |
| 172 | + $supportedTypesMap[LocalTime::class] = false; |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + $supportedTypes = array_keys($supportedTypesMap, true, true); |
| 177 | + foreach ($supportedTypes as $supportedType) { |
| 178 | + if ($value instanceof $supportedType) { |
| 179 | + return; |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + throw new DateTimeFormatException(sprintf("IntlFormatter with pattern '%s' is incompatible with type %s.", $pattern, get_class($value))); |
| 184 | + } |
| 185 | +} |
0 commit comments