-
Notifications
You must be signed in to change notification settings - Fork 0
/
DateTimeFormatter.php
188 lines (162 loc) · 5.51 KB
/
DateTimeFormatter.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
<?php
namespace Kyoya\DateTime;
class DateTimeFormatter implements FormatterInterface
{
/**
* Mapping of the supported formats.
*
* @var array
*/
protected $supportedFormats = array(
'none' => \IntlDateFormatter::NONE,
'short' => \IntlDateFormatter::SHORT,
'medium' => \IntlDateFormatter::MEDIUM,
'long' => \IntlDateFormatter::LONG,
'full' => \IntlDateFormatter::FULL,
);
/**
* Locale to use for formatting a date and/or a time.
*
* @var string
*/
private $locale;
/**
* Initializes the new instance. Sets the locale.
*
* @param string $locale Locale to use.
*
* @return DateTimeFormatter
*/
public function __construct($locale)
{
$this->locale = $locale;
}
/**
* Formats the date and time of a DateTime object using the locale specific default date and time format.
*
* @param \DateTimeInterface $dateTime Object to format.
*
* @return string
*/
public function format(\DateTimeInterface $dateTime)
{
$timezone = $this->extractTimezone($dateTime);
$formatter = $this->createFormatter($timezone);
return $this->realFormat($formatter, $dateTime);
}
/**
* Formats the date of a DateTime object using the specified format.
*
* @param \DateTimeInterface $dateTime Object to format.
* @param string $desiredFormat Date format to use.
*
* @return string
*/
public function formatDate(\DateTimeInterface $dateTime, $desiredFormat)
{
if (!isset($this->supportedFormats[$desiredFormat])) {
throw $this->createUnknownFormatException($desiredFormat);
}
$timezone = $this->extractTimezone($dateTime);
$dateType = $this->supportedFormats[$desiredFormat];
$formatter = $this->createFormatter($timezone, $dateType, $this->supportedFormats['none']);
return $this->realFormat($formatter, $dateTime);
}
/**
* Formats the time of a DateTime object using the specified format.
*
* @param \DateTimeInterface $dateTime Object to format.
* @param string $desiredFormat Time format to use.
*
* @return string
*/
public function formatTime(\DateTimeInterface $dateTime, $desiredFormat)
{
if (!isset($this->supportedFormats[$desiredFormat])) {
throw $this->createUnknownFormatException($desiredFormat);
}
$timezone = $this->extractTimezone($dateTime);
$timeType = $this->supportedFormats[$desiredFormat];
$formatter = $this->createFormatter($timezone, $this->supportedFormats['none'], $timeType);
return $this->realFormat($formatter, $dateTime);
}
/**
* Formats the date and the time of a DateTime object using the specified formats.
*
* @param \DateTimeInterface $dateTime Object to format.
* @param string $desiredDateFormat Date format to use.
* @param string $desiredTimeFormat Time format to use.
*
* @return string
*/
public function formatDateTime(\DateTimeInterface $dateTime, $desiredDateFormat, $desiredTimeFormat)
{
if (!isset($this->supportedFormats[$desiredDateFormat])) {
throw $this->createUnknownFormatException($desiredDateFormat);
}
if (!isset($this->supportedFormats[$desiredTimeFormat])) {
throw $this->createUnknownFormatException($desiredTimeFormat);
}
$timezone = $this->extractTimezone($dateTime);
$dateType = $this->supportedFormats[$desiredDateFormat];
$timeType = $this->supportedFormats[$desiredTimeFormat];
$formatter = $this->createFormatter($timezone, $dateType, $timeType);
return $this->realFormat($formatter, $dateTime);
}
/**
* Executes the format method.
*
* @param \IntlDateFormatter $formatter Formatter instance used to format the DateTime object.
* @param \DateTimeInterface $dateTime Instance of the DateTime object to format.
*
* @return string
*/
private function realFormat(\IntlDateFormatter $formatter, \DateTimeInterface $dateTime)
{
return $formatter->format($dateTime);
}
/**
* Creates the formatter.
*
* @param string $timezone Timezone to use.
* @param int $dateType Type of the formatted date.
* @param int $timeType Type of the formatted time.
*
* @return \IntlDateFormatter
*/
private function createFormatter($timezone, $dateType = null, $timeType = null)
{
return \IntlDateFormatter::create(
$this->locale,
$dateType,
$timeType,
$timezone
);
}
/**
* Creates an UnknownFormatException if an unsupported format was requested.
*
* @param string $desiredFormat Unsupported format.
*
* @return UnknownFormatException
*/
private function createUnknownFormatException($desiredFormat)
{
return new UnknownFormatException(
sprintf(
'The desired format "%s" is invalid. It must be one of the following: %s',
$desiredFormat,
implode(", ", array_keys($this->supportedFormats))
)
);
}
/**
* @param \DateTimeInterface $dateTime
*
* @return string
*/
private function extractTimezone(\DateTimeInterface $dateTime)
{
return $dateTime->getTimezone()->getName();
}
}