|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ICanBoogie\CLDR\Dates; |
|
4
|
|
|
|
|
5
|
|
|
use DateTimeInterface; |
|
6
|
|
|
use LogicException; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @property-read int $timestamp Unix timestamp. |
|
10
|
|
|
* @property-read int $year Year. |
|
11
|
|
|
* @property-read int<1, 12> $month Month of the year. |
|
12
|
|
|
* @property-read int<1, 31> $day Day of the month. |
|
13
|
|
|
* @property-read int<0, 23> $hour Hour of the day. |
|
14
|
|
|
* @property-read int<1, 59> $minute Minute of the hour. |
|
15
|
|
|
* @property-read int<1, 59> $second Second of the minute. |
|
16
|
|
|
* @property-read int<1, 4> $quarter Quarter of the year. |
|
17
|
|
|
* @property-read int<1, 53> $week Week of the year. |
|
18
|
|
|
* @property-read int<1, 7> $weekday Day of the week. |
|
19
|
|
|
* @property-read int $year_day Day of the year. |
|
20
|
|
|
*/ |
|
21
|
|
|
class DateTimeAccessor |
|
22
|
|
|
{ |
|
23
|
|
|
public function __construct( |
|
24
|
|
|
public readonly DateTimeInterface $delegate |
|
25
|
|
|
) { |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function __get(string $property): int |
|
29
|
|
|
{ |
|
30
|
|
|
$f = $this->delegate->format(...); |
|
31
|
|
|
|
|
32
|
|
|
return match ($property) { |
|
33
|
|
|
'year' => (int)$f('Y'), |
|
34
|
|
|
'month' => (int)$f('m'), |
|
35
|
|
|
'day' => (int)$f('d'), |
|
36
|
|
|
'hour' => (int)$f('H'), |
|
37
|
|
|
'minute' => (int)$f('i'), |
|
38
|
|
|
'second' => (int)$f('s'), |
|
39
|
|
|
'quarter' => (int)floor(($this->month - 1) / 3) + 1, |
|
40
|
|
|
'week' => (int)$f('W'), |
|
41
|
|
|
'year_day' => (int)$f('z') + 1, |
|
42
|
|
|
'weekday' => (int)$f('w') ?: 7, |
|
43
|
|
|
default => throw new LogicException("Undefined property: $property"), |
|
44
|
|
|
}; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @see DateTimeInterface::format |
|
49
|
|
|
*/ |
|
50
|
|
|
public function format(string $pattern): string |
|
51
|
|
|
{ |
|
52
|
|
|
return $this->delegate->format($pattern); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|