forked from kornrunner/CLDR
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocalizedDateTime.php
More file actions
143 lines (126 loc) · 3.32 KB
/
LocalizedDateTime.php
File metadata and controls
143 lines (126 loc) · 3.32 KB
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
<?php
/*
* This file is part of the ICanBoogie package.
*
* (c) Olivier Laviale <olivier.laviale@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ICanBoogie\CLDR;
use DateTime;
use DateTimeInterface;
use ICanBoogie\PropertyNotDefined;
use function substr;
/**
* A localized date time.
*
* <pre>
* <?php
*
* namespace ICanBoogie\CLDR;
*
* $ldt = new LocalizedDateTime(new \DateTime('2013-11-04 20:21:22 UTC'), $repository->locales['fr']);
*
* echo $ldt->as_full; // lundi 4 novembre 2013 20:21:22 UTC
* # or
* echo $ldt->format_as_full(); // lundi 4 novembre 2013 20:21:22 UTC
*
* echo $ldt->as_long; // 4 novembre 2013 20:21:22 UTC
* echo $ldt->as_medium; // 4 nov. 2013 20:21:22
* echo $ldt->as_short; // 04/11/2013 20:21
* </pre>
*
* @extends LocalizedObjectWithFormatter<DateTimeInterface, DateTimeFormatter>
*
* @property-read string $as_full
* @property-read string $as_long
* @property-read string $as_medium
* @property-read string $as_short
*
* @method string format_as_full() format_as_full() Formats the instance according to the `full` datetime pattern.
* @method string format_as_long() format_as_long() Formats the instance according to the `long` datetime pattern.
* @method string format_as_medium() format_as_medium() Formats the instance according to the `medium` datetime pattern.
* @method string format_as_short() format_as_short() Formats the instance according to the `short` datetime pattern.
*/
final class LocalizedDateTime extends LocalizedObjectWithFormatter
{
/**
* @var string[]
*/
static private $format_widths = [ 'full', 'long', 'medium', 'short' ];
/**
* @inheritDoc
*
* @return DateTimeFormatter
*/
protected function lazy_get_formatter(): Formatter
{
return $this->locale->calendar->datetime_formatter;
}
/**
* @param string $property
*
* @return mixed
*/
public function __get($property)
{
$width = substr($property, 3);
if (strpos($property, 'as_') === 0 && in_array($width, self::$format_widths))
{
return $this->{ 'format_as_' . $width }();
}
try
{
return parent::__get($property);
}
catch (PropertyNotDefined $e)
{
return $this->target->$property;
}
}
/**
* @param string $property
* @param mixed $value
*/
public function __set($property, $value): void
{
$this->target->$property = $value;
}
/**
* @param string $method
* @param array<string, mixed> $arguments
*
* @return mixed
*
* @throws \Exception
*/
public function __call($method, $arguments)
{
if (strpos($method, 'format_as_') === 0 && in_array($width = substr($method, 10), self::$format_widths))
{
return $this->format($width);
}
return $this->target->$method(...$arguments);
}
public function __toString(): string
{
$target = $this->target;
if (method_exists($target, __FUNCTION__))
{
return (string) $target;
}
// `ATOM` is used instead of `ISO8601` because of a bug in the pattern
// @see http://php.net/manual/en/class.datetime.php#datetime.constants.iso8601
return $this->target->format(DateTime::ATOM);
}
/**
* @inheritDoc
*
* @throws \Exception
*/
public function format(?string $pattern = null): string
{
return $this->formatter->format($this->target, $pattern);
}
}