1 | <?php |
||
2 | |||
3 | namespace ICanBoogie\CLDR\Dates; |
||
4 | |||
5 | use DateTimeInterface; |
||
6 | use ICanBoogie\CLDR\Core\Locale; |
||
7 | use ICanBoogie\CLDR\Core\LocalizedObjectWithFormatter; |
||
8 | |||
9 | /** |
||
10 | * A localized date time. |
||
11 | * |
||
12 | * <pre> |
||
13 | * <?php |
||
14 | * |
||
15 | * namespace ICanBoogie\CLDR; |
||
16 | * |
||
17 | * $ldt = new LocalizedDateTime(new \DateTime('2013-11-04 20:21:22 UTC'), $repository->locales['fr']); |
||
18 | * |
||
19 | * echo $ldt->as_full; // lundi 4 novembre 2013 20:21:22 UTC |
||
20 | * # or |
||
21 | * echo $ldt->format_as_full(); // lundi 4 novembre 2013 20:21:22 UTC |
||
22 | * |
||
23 | * echo $ldt->as_long; // 4 novembre 2013 20:21:22 UTC |
||
24 | * echo $ldt->as_medium; // 4 nov. 2013 20:21:22 |
||
25 | * echo $ldt->as_short; // 04/11/2013 20:21 |
||
26 | * </pre> |
||
27 | * |
||
28 | * @extends LocalizedObjectWithFormatter<DateTimeInterface, DateTimeFormatter> |
||
29 | * |
||
30 | * @property-read string $as_full |
||
31 | * @property-read string $as_long |
||
32 | * @property-read string $as_medium |
||
33 | * @property-read string $as_short |
||
34 | */ |
||
35 | final class LocalizedDateTime extends LocalizedObjectWithFormatter |
||
36 | { |
||
37 | public function __construct(DateTimeInterface $target, Locale $locale) |
||
38 | { |
||
39 | parent::__construct($target, $locale, $locale->calendar->datetime_formatter); |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * @param string $property |
||
44 | * |
||
45 | * @return mixed |
||
46 | */ |
||
47 | public function __get($property) |
||
48 | { |
||
49 | return match ($property) { |
||
50 | 'as_full' => $this->format_as_full(), |
||
51 | 'as_long' => $this->format_as_long(), |
||
52 | 'as_medium' => $this->format_as_medium(), |
||
53 | 'as_short' => $this->format_as_short(), |
||
54 | default => $this->target->$property, |
||
55 | }; |
||
56 | } |
||
57 | |||
58 | public function __toString(): string |
||
59 | { |
||
60 | // `ATOM` is used instead of `ISO8601` because of a bug in the pattern |
||
61 | // @link https://php.net/manual/en/class.datetime.php#datetime.constants.iso8601 |
||
62 | |||
63 | return $this->target->format(DateTimeInterface::ATOM); |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * @see DateTimeFormatter::format() |
||
68 | * |
||
69 | * @throws \Exception |
||
70 | */ |
||
71 | public function format(string|DateTimeFormatLength|DateTimeFormatId $pattern_or_length_or_id): string |
||
72 | { |
||
73 | return $this->formatter->format($this->target, $pattern_or_length_or_id); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
74 | } |
||
75 | |||
76 | /** |
||
77 | * Formats the instance according to the {@see DateTimeFormatLength::FULL} length. |
||
78 | */ |
||
79 | public function format_as_full(): string |
||
80 | { |
||
81 | return $this->format(DateTimeFormatLength::FULL); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Formats the instance according to the {@see DateTimeFormatLength::LONG} length. |
||
86 | */ |
||
87 | public function format_as_long(): string |
||
88 | { |
||
89 | return $this->format(DateTimeFormatLength::LONG); |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Formats the instance according to the {@see DateTimeFormatLength::MEDIUM} length. |
||
94 | */ |
||
95 | public function format_as_medium(): string |
||
96 | { |
||
97 | return $this->format(DateTimeFormatLength::MEDIUM); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * Formats the instance according to the {@see DateTimeFormatLength::SHORT} length. |
||
102 | */ |
||
103 | public function format_as_short(): string |
||
104 | { |
||
105 | return $this->format(DateTimeFormatLength::SHORT); |
||
106 | } |
||
107 | } |
||
108 |