|
22 | 22 |
|
23 | 23 | namespace FormatPHP; |
24 | 24 |
|
25 | | -use FormatPHP\Exception\InvalidArgumentException; |
26 | | -use FormatPHP\Exception\UnableToGenerateMessageIdException; |
| 25 | +use DateTimeImmutable as PhpDateTimeImmutable; |
| 26 | +use DateTimeInterface as PhpDateTimeInterface; |
| 27 | +use Exception as PhpException; |
| 28 | +use FormatPHP\Intl\DateTimeFormat; |
| 29 | +use FormatPHP\Intl\DateTimeFormatOptions; |
27 | 30 | use FormatPHP\Intl\MessageFormat; |
28 | 31 | use FormatPHP\Util\MessageCleaner; |
29 | 32 | use FormatPHP\Util\MessageRetriever; |
30 | 33 |
|
31 | 34 | use function array_merge; |
| 35 | +use function gettype; |
32 | 36 | use function is_int; |
| 37 | +use function is_string; |
| 38 | +use function sprintf; |
33 | 39 |
|
34 | 40 | /** |
35 | 41 | * FormatPHP internationalization and localization |
| 42 | + * |
| 43 | + * @psalm-import-type DateTimeType from FormatterInterface |
36 | 44 | */ |
37 | 45 | class FormatPHP implements FormatterInterface |
38 | 46 | { |
@@ -76,19 +84,80 @@ public function formatMessage(array $descriptor, array $values = []): string |
76 | 84 | $descriptor['description'] ?? null, |
77 | 85 | ), |
78 | 86 | ); |
79 | | - } catch (UnableToGenerateMessageIdException $exception) { |
80 | | - throw new InvalidArgumentException( |
| 87 | + } catch (Exception\UnableToGenerateMessageIdException $exception) { |
| 88 | + throw new Exception\InvalidArgumentException( |
81 | 89 | 'The message descriptor must have an ID or default message', |
82 | | - is_int($exception->getCode()) ? $exception->getCode() : 0, // @phpstan-ignore-line |
| 90 | + (int) $exception->getCode(), |
83 | 91 | $exception, |
84 | 92 | ); |
85 | 93 | } |
86 | 94 |
|
87 | 95 | return $this->messageFormat->format($this->cleanMessage($messagePattern), $values); |
88 | 96 | } |
89 | 97 |
|
| 98 | + /** |
| 99 | + * @throws Exception\InvalidArgumentException |
| 100 | + * @throws Exception\UnableToFormatDateTimeException |
| 101 | + * |
| 102 | + * @inheritdoc |
| 103 | + */ |
| 104 | + public function formatDate($date = null, ?DateTimeFormatOptions $options = null): string |
| 105 | + { |
| 106 | + $formatter = new DateTimeFormat($this->config->getLocale(), $options); |
| 107 | + |
| 108 | + return $formatter->format($this->convertToDateTime($date)); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * @throws Exception\InvalidArgumentException |
| 113 | + * @throws Exception\UnableToFormatDateTimeException |
| 114 | + * |
| 115 | + * @inheritdoc |
| 116 | + */ |
| 117 | + public function formatTime($date = null, ?DateTimeFormatOptions $options = null): string |
| 118 | + { |
| 119 | + $options = $options ? clone $options : new DateTimeFormatOptions(); |
| 120 | + |
| 121 | + if ($options->dateStyle === null && $options->timeStyle === null) { |
| 122 | + $options->hour = $options->hour ?? 'numeric'; |
| 123 | + $options->minute = $options->minute ?? 'numeric'; |
| 124 | + } |
| 125 | + |
| 126 | + return $this->formatDate($date, $options); |
| 127 | + } |
| 128 | + |
90 | 129 | protected function getConfig(): ConfigInterface |
91 | 130 | { |
92 | 131 | return $this->config; |
93 | 132 | } |
| 133 | + |
| 134 | + /** |
| 135 | + * @param DateTimeType | mixed $date |
| 136 | + * |
| 137 | + * @throws Exception\InvalidArgumentException |
| 138 | + * @throws PhpException |
| 139 | + */ |
| 140 | + private function convertToDateTime($date): PhpDateTimeInterface |
| 141 | + { |
| 142 | + if ($date === null) { |
| 143 | + return new PhpDateTimeImmutable(); |
| 144 | + } |
| 145 | + |
| 146 | + if ($date instanceof PhpDateTimeInterface) { |
| 147 | + return $date; |
| 148 | + } |
| 149 | + |
| 150 | + if (is_string($date)) { |
| 151 | + return new PhpDateTimeImmutable($date); |
| 152 | + } |
| 153 | + |
| 154 | + if (is_int($date)) { |
| 155 | + return new PhpDateTimeImmutable('@' . $date); |
| 156 | + } |
| 157 | + |
| 158 | + throw new Exception\InvalidArgumentException(sprintf( |
| 159 | + 'Value must be a string, integer, or instance of DateTimeInterface; received \'%s\'', |
| 160 | + gettype($date), |
| 161 | + )); |
| 162 | + } |
94 | 163 | } |
0 commit comments