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