-
Notifications
You must be signed in to change notification settings - Fork 4
/
Calendar.php
376 lines (326 loc) · 12.6 KB
/
Calendar.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<?php
namespace ICanBoogie\CLDR\Dates;
use ArrayObject;
use DateTimeInterface;
use ICanBoogie\Accessor\AccessorTrait;
use ICanBoogie\CLDR\Core\Locale;
use ICanBoogie\CLDR\General\Transforms\ContextTransforms;
/**
* Representation of a locale calendar.
*
* @property-read DateTimeFormatter $datetime_formatter A datetime formatter.
* @property-read DateFormatter $date_formatter A date formatter.
* @property-read TimeFormatter $time_formatter A time formatter.
*
* @property-read string[] $standalone_abbreviated_days Shortcut to `days/stand-alone/abbreviated`.
* @property-read string[] $standalone_abbreviated_eras Shortcut to `eras/eraAbbr`.
* @property-read string[] $standalone_abbreviated_months Shortcut to `months/stand-alone/abbreviated`.
* @property-read string[] $standalone_abbreviated_quarters Shortcut to `quarters/stand-alone/abbreviated`.
* @property-read string[] $standalone_narrow_days Shortcut to `days/stand-alone/narrow`.
* @property-read string[] $standalone_narrow_eras Shortcut to `eras/eraNarrow`.
* @property-read string[] $standalone_narrow_months Shortcut to `months/stand-alone/narrow`.
* @property-read string[] $standalone_narrow_quarters Shortcut to `quarters/stand-alone/narrow`.
* @property-read string[] $standalone_short_days Shortcut to `days/stand-alone/short`.
* @property-read string[] $standalone_short_eras Shortcut to `eras/eraAbbr`.
* @property-read string[] $standalone_short_months Shortcut to `months/stand-alone/abbreviated`.
* @property-read string[] $standalone_short_quarters Shortcut to `quarters/stand-alone/abbreviated`.
* @property-read string[] $standalone_wide_days Shortcut to `days/stand-alone/wide`.
* @property-read string[] $standalone_wide_eras Shortcut to `eras/eraNames`.
* @property-read string[] $standalone_wide_months Shortcut to `months/stand-alone/wide`.
* @property-read string[] $standalone_wide_quarters Shortcut to `quarters/stand-alone/wide`.
* @property-read string[] $abbreviated_days Shortcut to `days/format/abbreviated`.
* @property-read string[] $abbreviated_eras Shortcut to `eras/eraAbbr`.
* @property-read string[] $abbreviated_months Shortcut to `months/format/abbreviated`.
* @property-read string[] $abbreviated_quarters Shortcut to `quarters/format/abbreviated`.
* @property-read string[] $narrow_days Shortcut to `days/format/narrow`.
* @property-read string[] $narrow_eras Shortcut to `eras/eraNarrow`.
* @property-read string[] $narrow_months Shortcut to `months/format/narrow`.
* @property-read string[] $narrow_quarters Shortcut to `quarters/format/narrow`.
* @property-read string[] $short_days Shortcut to `days/format/short`.
* @property-read string[] $short_eras Shortcut to `eras/eraAbbr`.
* @property-read string[] $short_months Shortcut to `months/format/abbreviated`.
* @property-read string[] $short_quarters Shortcut to `quarters/format/abbreviated`.
* @property-read string[] $wide_days Shortcut to `days/format/wide`.
* @property-read string[] $wide_eras Shortcut to `eras/eraNames`.
* @property-read string[] $wide_months Shortcut to `months/format/wide`.
* @property-read string[] $wide_quarters Shortcut to `quarters/format/wide`.
*
* @extends ArrayObject<string, mixed>
*/
final class Calendar extends ArrayObject
{
/**
* @uses get_datetime_formatter
* @uses get_date_formatter
* @uses get_time_formatter
*/
use AccessorTrait;
public const SHORTHANDS_REGEX = '#^(standalone_)?(abbreviated|narrow|short|wide)_(days|eras|months|quarters)$#';
public const WIDTH_ABBR = 'abbreviated';
public const WIDTH_NARROW = 'narrow';
public const WIDTH_SHORT = 'short';
public const WIDTH_WIDE = 'wide';
public const ERA_NAMES = 'eraNames';
public const ERA_ABBR = 'eraAbbr';
public const ERA_NARROW = 'eraNarrow';
public const CALENDAR_MONTHS = 'months';
public const CALENDAR_DAYS = 'days';
public const CALENDAR_QUARTERS = 'quarters';
public const CALENDAR_ERAS = 'eras';
public const CONTEXT_FORMAT = 'format';
public const CONTEXT_STAND_ALONE = 'stand-alone';
/**
* @var array<string, string>
*/
private static array $era_widths_mapping = [
self::WIDTH_ABBR => self::ERA_ABBR,
self::WIDTH_NARROW => self::ERA_NARROW,
self::WIDTH_SHORT => self::ERA_ABBR,
self::WIDTH_WIDE => self::ERA_NAMES
];
private DateTimeFormatter $datetime_formatter;
private function get_datetime_formatter(): DateTimeFormatter
{
return $this->datetime_formatter ??= new DateTimeFormatter($this);
}
private DateFormatter $date_formatter;
private function get_date_formatter(): DateFormatter
{
return $this->date_formatter ??= new DateFormatter($this);
}
private TimeFormatter $time_formatter;
private function get_time_formatter(): TimeFormatter
{
return $this->time_formatter ??= new TimeFormatter($this);
}
private readonly ContextTransforms $context_transforms;
/**
* @param array<string, mixed> $data
*/
public function __construct(
public readonly Locale $locale,
array $data
) {
$this->context_transforms = $locale->context_transforms;
$data = $this->transform_data($data);
parent::__construct($data);
}
/**
* @phpstan-ignore-next-line
*/
private array $shortcuts = [];
/**
* @return mixed
*/
public function __get(string $property)
{
if (!preg_match(self::SHORTHANDS_REGEX, $property, $matches)) {
return $this->accessor_get($property);
}
$make = function () use ($matches) {
[ , $standalone, $width, $type ] = $matches;
$data = $this[$type];
if ($type === self::CALENDAR_ERAS) {
return $data[self::$era_widths_mapping[$width]];
}
$data = $data[$standalone ? self::CONTEXT_STAND_ALONE : self::CONTEXT_FORMAT];
if ($width === self::WIDTH_SHORT && empty($data[$width])) {
$width = self::WIDTH_ABBR;
}
return $data[$width];
};
return $this->shortcuts[$property] ??= $make();
}
/**
* @see DateTimeFormatter::format
*/
public function format_datetime(
DateTimeInterface|int|string $datetime,
string|DateTimeFormatLength $pattern_or_length_or_skeleton
): string {
return $this->get_datetime_formatter()->format($datetime, $pattern_or_length_or_skeleton);
}
/**
* @see DateFormatter::format
*/
public function format_date(
DateTimeInterface|int|string $datetime,
string|DateTimeFormatLength $pattern_or_length_or_skeleton
): string {
return $this->get_date_formatter()->format($datetime, $pattern_or_length_or_skeleton);
}
/**
* @see TimeFormatter::format
*/
public function format_time(
DateTimeInterface|int|string $datetime,
string|DateTimeFormatLength $pattern_or_length_or_skeleton
): string {
return $this->get_time_formatter()->format($datetime, $pattern_or_length_or_skeleton);
}
/**
* Transforms calendar data according to context transforms rules.
*
* @param array<string, mixed> $data
*
* @return array<string, mixed>
*
* @uses transform_months
* @uses transform_days
* @uses transform_quarters
*/
private function transform_data(array $data): array
{
static $transformable = [
self::CALENDAR_MONTHS,
self::CALENDAR_DAYS,
self::CALENDAR_QUARTERS
];
foreach ($transformable as $name) {
array_walk($data[$name], function (array &$data, string $context) use ($name): void {
$is_stand_alone = self::CONTEXT_STAND_ALONE === $context;
array_walk($data, function (array &$names, string $width) use ($name, $is_stand_alone): void {
$names = $this->{'transform_' . $name}($names, $width, $is_stand_alone);
});
});
}
if (isset($data[self::CALENDAR_ERAS])) {
array_walk($data[self::CALENDAR_ERAS], function (array &$names, string $width): void {
$names = $this->transform_eras($names, $width);
});
}
return $data;
}
/**
* Transforms month names according to context transforms rules.
*
* @param string[] $names
*
* @return string[]
*/
private function transform_months(array $names, string $width, bool $standalone): array
{
return $this->transform_months_or_days(
$names,
$width,
$standalone,
ContextTransforms::USAGE_MONTH_STANDALONE_EXCEPT_NARROW
);
}
/**
* Transforms day names according to context transforms rules.
*
* @param string[] $names
*
* @return string[]
*/
private function transform_days(array $names, string $width, bool $standalone): array
{
return $this->transform_months_or_days(
$names,
$width,
$standalone,
ContextTransforms::USAGE_DAY_STANDALONE_EXCEPT_NARROW
);
}
/**
* Transforms day names according to context transforms rules.
*
* @param string[] $names
*
* @return string[]
*/
private function transform_months_or_days(array $names, string $width, bool $standalone, string $usage): array
{
if ($width === self::WIDTH_NARROW || !$standalone) {
return $names;
}
return $this->apply_transform(
$names,
$usage,
ContextTransforms::TYPE_STAND_ALONE
);
}
/**
* Transforms era names according to context transforms rules.
*
* @param string[] $names
*
* @return string[]
*/
private function transform_eras(array $names, string $width): array
{
return match ($width) {
self::ERA_ABBR => $this->apply_transform(
$names,
ContextTransforms::USAGE_ERA_ABBR,
ContextTransforms::TYPE_STAND_ALONE
),
self::ERA_NAMES => $this->apply_transform(
$names,
ContextTransforms::USAGE_ERA_NAME,
ContextTransforms::TYPE_STAND_ALONE
),
self::ERA_NARROW => $this->apply_transform(
$names,
ContextTransforms::USAGE_ERA_NARROW,
ContextTransforms::TYPE_STAND_ALONE
),
default => $names,
};
}
/**
* Transforms quarters names according to context transforms rules.
*
* @param string[] $names
*
* @return string[]
*/
private function transform_quarters(array $names, string $width, bool $standalone): array
{
if ($standalone) {
if ($width !== self::WIDTH_WIDE) {
return $names;
}
return $this->apply_transform(
$names,
ContextTransforms::USAGE_QUARTER_STANDALONE_WIDE,
ContextTransforms::TYPE_STAND_ALONE
);
}
return match ($width) {
self::WIDTH_ABBR => $this->apply_transform(
$names,
ContextTransforms::USAGE_QUARTER_ABBREVIATED,
ContextTransforms::TYPE_STAND_ALONE
),
self::WIDTH_WIDE => $this->apply_transform(
$names,
ContextTransforms::USAGE_QUARTER_FORMAT_WIDE,
ContextTransforms::TYPE_STAND_ALONE
),
self::WIDTH_NARROW => $this->apply_transform(
$names,
ContextTransforms::USAGE_QUARTER_NARROW,
ContextTransforms::TYPE_STAND_ALONE
),
default => $names,
};
// @codeCoverageIgnore
}
/**
* Applies transformation to names.
*
* @param string[] $names
*
* @return string[]
*/
private function apply_transform(array $names, string $usage, string $type): array
{
$context_transforms = $this->context_transforms;
return array_map(
static fn(string $str): string => $context_transforms->transform($str, $usage, $type),
$names
);
}
}