|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of the Symfony package. |
|
|
5 |
* |
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com> |
|
|
7 |
* |
|
|
8 |
* For the full copyright and license information, please view the LICENSE |
|
|
9 |
* file that was distributed with this source code. |
|
|
10 |
*/ |
|
|
11 |
|
|
|
12 |
namespace Symfony\Component\Locale\Stub; |
|
|
13 |
|
|
|
14 |
use Symfony\Component\Locale\Stub\StubLocale; |
|
|
15 |
use Symfony\Component\Locale\Stub\DateFormat\FullTransformer; |
|
|
16 |
use Symfony\Component\Locale\Exception\NotImplementedException; |
|
|
17 |
use Symfony\Component\Locale\Exception\MethodNotImplementedException; |
|
|
18 |
use Symfony\Component\Locale\Exception\MethodArgumentNotImplementedException; |
|
|
19 |
use Symfony\Component\Locale\Exception\MethodArgumentValueNotImplementedException; |
|
|
20 |
|
|
|
21 |
/** |
|
|
22 |
* Provides a stub IntlDateFormatter for the 'en' locale. |
|
|
23 |
* |
|
|
24 |
* @author Igor Wiedler <igor@wiedler.ch> |
|
|
25 |
*/ |
|
|
26 |
class StubIntlDateFormatter |
|
|
27 |
{ |
|
|
28 |
/** |
|
|
29 |
* Constants defined by the intl extension, not class constants in IntlDateFormatter |
|
|
30 |
* TODO: remove if the Form component drop the call to the intl_is_failure() function |
|
|
31 |
* |
|
|
32 |
* @see StubIntlDateFormatter::getErrorCode() |
|
|
33 |
* @see StubIntlDateFormatter::getErrorMessage() |
|
|
34 |
*/ |
|
|
35 |
const U_ZERO_ERROR = 0; |
|
|
36 |
const U_ZERO_ERROR_MESSAGE = 'U_ZERO_ERROR'; |
|
|
37 |
|
|
|
38 |
/* date/time format types */ |
|
|
39 |
const NONE = -1; |
|
|
40 |
const FULL = 0; |
|
|
41 |
const LONG = 1; |
|
|
42 |
const MEDIUM = 2; |
|
|
43 |
const SHORT = 3; |
|
|
44 |
|
|
|
45 |
/* calendar formats */ |
|
|
46 |
const TRADITIONAL = 0; |
|
|
47 |
const GREGORIAN = 1; |
|
|
48 |
|
|
|
49 |
/** |
|
|
50 |
* Patterns used to format the date when no pattern is provided |
|
|
51 |
* @var array |
|
|
52 |
*/ |
|
|
53 |
private $defaultDateFormats = array( |
|
|
54 |
self::NONE => '', |
|
|
55 |
self::FULL => 'EEEE, LLLL d, y', |
|
|
56 |
self::LONG => 'LLLL d, y', |
|
|
57 |
self::MEDIUM => 'LLL d, y', |
|
|
58 |
self::SHORT => 'M/d/yy', |
|
|
59 |
); |
|
|
60 |
|
|
|
61 |
/** |
|
|
62 |
* Patterns used to format the time when no pattern is provided |
|
|
63 |
* @var array |
|
|
64 |
*/ |
|
|
65 |
private $defaultTimeFormats = array( |
|
|
66 |
self::FULL => 'h:mm:ss a zzzz', |
|
|
67 |
self::LONG => 'h:mm:ss a z', |
|
|
68 |
self::MEDIUM => 'h:mm:ss a', |
|
|
69 |
self::SHORT => 'h:mm a', |
|
|
70 |
); |
|
|
71 |
|
|
|
72 |
/** |
|
|
73 |
* @var int |
|
|
74 |
*/ |
|
|
75 |
private $datetype; |
|
|
76 |
|
|
|
77 |
/** |
|
|
78 |
* @var int |
|
|
79 |
*/ |
|
|
80 |
private $timetype; |
|
|
81 |
|
|
|
82 |
/** |
|
|
83 |
* @var string |
|
|
84 |
*/ |
|
|
85 |
private $pattern; |
|
|
86 |
|
|
|
87 |
/** |
|
|
88 |
* @var DateTimeZone |
|
|
89 |
*/ |
|
|
90 |
private $dateTimeZone; |
|
|
91 |
|
|
|
92 |
/** |
|
|
93 |
* @var Boolean |
|
|
94 |
*/ |
|
|
95 |
private $unitializedTimeZoneId = false; |
|
|
96 |
|
|
|
97 |
/** |
|
|
98 |
* @var string |
|
|
99 |
*/ |
|
|
100 |
private $timeZoneId; |
|
|
101 |
|
|
|
102 |
/** |
|
|
103 |
* Constructor |
|
|
104 |
* |
|
|
105 |
* @param string $locale The locale code |
|
|
106 |
* @param int $datetype Type of date formatting, one of the format type constants |
|
|
107 |
* @param int $timetype Type of time formatting, one of the format type constants |
|
|
108 |
* @param string $timezone Timezone identifier |
|
|
109 |
* @param int $calendar Calendar to use for formatting or parsing; default is Gregorian. |
|
|
110 |
* One of the calendar constants. |
|
|
111 |
* @param string $pattern Optional pattern to use when formatting. |
|
|
112 |
* @see http://www.php.net/manual/en/intldateformatter.create.php |
|
|
113 |
* @see http://userguide.icu-project.org/formatparse/datetime |
|
|
114 |
* @throws MethodArgumentValueNotImplementedException When $locale different than 'en' is passed |
|
|
115 |
* @throws MethodArgumentValueNotImplementedException When $calendar different than GREGORIAN is passed |
|
|
116 |
*/ |
|
|
117 |
public function __construct($locale, $datetype, $timetype, $timezone = null, $calendar = self::GREGORIAN, $pattern = null) |
|
|
118 |
{ |
|
|
119 |
if ('en' != $locale) { |
|
|
120 |
throw new MethodArgumentValueNotImplementedException(__METHOD__, 'locale', $locale, 'Only the \'en\' locale is supported'); |
|
|
121 |
} |
|
|
122 |
|
|
|
123 |
if (self::GREGORIAN != $calendar) { |
|
|
124 |
throw new MethodArgumentValueNotImplementedException(__METHOD__, 'calendar', $calendar, 'Only the GREGORIAN calendar is supported'); |
|
|
125 |
} |
|
|
126 |
|
|
|
127 |
$this->datetype = $datetype; |
|
|
128 |
$this->timetype = $timetype; |
|
|
129 |
|
|
|
130 |
$this->setPattern($pattern); |
|
|
131 |
$this->setTimeZoneId($timezone); |
|
|
132 |
} |
|
|
133 |
|
|
|
134 |
/** |
|
|
135 |
* Static constructor |
|
|
136 |
* |
|
|
137 |
* @param string $locale The locale code |
|
|
138 |
* @param int $datetype Type of date formatting, one of the format type constants |
|
|
139 |
* @param int $timetype Type of time formatting, one of the format type constants |
|
|
140 |
* @param string $timezone Timezone identifier |
|
|
141 |
* @param int $calendar Calendar to use for formatting or parsing; default is Gregorian. |
|
|
142 |
* One of the calendar constants. |
|
|
143 |
* @param string $pattern Optional pattern to use when formatting |
|
|
144 |
* @see http://www.php.net/manual/en/intldateformatter.create.php |
|
|
145 |
* @see http://userguide.icu-project.org/formatparse/datetime |
|
|
146 |
* @throws MethodArgumentValueNotImplementedException When $locale different than 'en' is passed |
|
|
147 |
*/ |
|
|
148 |
static public function create($locale, $datetype, $timetype, $timezone = null, $calendar = self::GREGORIAN, $pattern = null) |
|
|
149 |
{ |
|
|
150 |
return new self($locale, $datetype, $timetype, $timezone, $calendar, $pattern); |
|
|
151 |
} |
|
|
152 |
|
|
|
153 |
/** |
|
|
154 |
* Format the date/time value (timestamp) as a string |
|
|
155 |
* |
|
|
156 |
* @param mixed $timestamp Unix timestamp to format |
|
|
157 |
* @return string The formatted value |
|
|
158 |
* @see http://www.php.net/manual/en/intldateformatter.format.php |
|
|
159 |
* @throws NotImplementedException If one of the formatting characters is not implemented |
|
|
160 |
*/ |
|
|
161 |
public function format($timestamp) |
|
|
162 |
{ |
|
|
163 |
// intl allows timestamps to be passed as arrays - we don't |
|
|
164 |
if (is_array($timestamp)) { |
|
|
165 |
throw new MethodArgumentValueNotImplementedException(__METHOD__, 'timestamp', $timestamp, 'Only integer unix timestamps are supported'); |
|
|
166 |
} |
|
|
167 |
|
|
|
168 |
if (!is_int($timestamp)) { |
|
|
169 |
// behave like the intl extension |
|
|
170 |
StubIntl::setErrorCode(StubIntl::U_ILLEGAL_ARGUMENT_ERROR); |
|
|
171 |
|
|
|
172 |
return false; |
|
|
173 |
} |
|
|
174 |
|
|
|
175 |
$transformer = new FullTransformer($this->getPattern(), $this->getTimeZoneId()); |
|
|
176 |
$formatted = $transformer->format($this->createDateTime($timestamp)); |
|
|
177 |
|
|
|
178 |
return $formatted; |
|
|
179 |
} |
|
|
180 |
|
|
|
181 |
/** |
|
|
182 |
* Returns the formatter's calendar |
|
|
183 |
* |
|
|
184 |
* @return int The calendar being used by the formatter |
|
|
185 |
* @see http://www.php.net/manual/en/intldateformatter.getcalendar.php |
|
|
186 |
*/ |
|
|
187 |
public function getCalendar() |
|
|
188 |
{ |
|
|
189 |
return self::GREGORIAN; |
|
|
190 |
} |
|
|
191 |
|
|
|
192 |
/** |
|
|
193 |
* Returns the formatter's datetype |
|
|
194 |
* |
|
|
195 |
* @return int The current value of the formatter |
|
|
196 |
* @see http://www.php.net/manual/en/intldateformatter.getdatetype.php |
|
|
197 |
*/ |
|
|
198 |
public function getDateType() |
|
|
199 |
{ |
|
|
200 |
return $this->datetype; |
|
|
201 |
} |
|
|
202 |
|
|
|
203 |
/** |
|
|
204 |
* Returns formatter's last error code. Always returns the U_ZERO_ERROR class constant value |
|
|
205 |
* |
|
|
206 |
* @return int The error code from last formatter call |
|
|
207 |
* @see http://www.php.net/manual/en/intldateformatter.geterrorcode.php |
|
|
208 |
*/ |
|
|
209 |
public function getErrorCode() |
|
|
210 |
{ |
|
|
211 |
return self::U_ZERO_ERROR; |
|
|
212 |
} |
|
|
213 |
|
|
|
214 |
/** |
|
|
215 |
* Returns formatter's last error message. Always returns the U_ZERO_ERROR_MESSAGE class constant value |
|
|
216 |
* |
|
|
217 |
* @return string The error message from last formatter call |
|
|
218 |
* @see http://www.php.net/manual/en/intldateformatter.geterrormessage.php |
|
|
219 |
*/ |
|
|
220 |
public function getErrorMessage() |
|
|
221 |
{ |
|
|
222 |
return self::U_ZERO_ERROR_MESSAGE; |
|
|
223 |
} |
|
|
224 |
|
|
|
225 |
/** |
|
|
226 |
* Returns the formatter's locale |
|
|
227 |
* |
|
|
228 |
* @param int $type The locale name type to return between valid or actual (StubLocale::VALID_LOCALE or StubLocale::ACTUAL_LOCALE, respectively) |
|
|
229 |
* @return string The locale name used to create the formatter |
|
|
230 |
* @see http://www.php.net/manual/en/intldateformatter.getlocale.php |
|
|
231 |
*/ |
|
|
232 |
public function getLocale($type = StubLocale::ACTUAL_LOCALE) |
|
|
233 |
{ |
|
|
234 |
return 'en'; |
|
|
235 |
} |
|
|
236 |
|
|
|
237 |
/** |
|
|
238 |
* Returns the formatter's pattern |
|
|
239 |
* |
|
|
240 |
* @return string The pattern string used by the formatter |
|
|
241 |
* @see http://www.php.net/manual/en/intldateformatter.getpattern.php |
|
|
242 |
*/ |
|
|
243 |
public function getPattern() |
|
|
244 |
{ |
|
|
245 |
return $this->pattern; |
|
|
246 |
} |
|
|
247 |
|
|
|
248 |
/** |
|
|
249 |
* Returns the formatter's time type |
|
|
250 |
* |
|
|
251 |
* @return string The time type used by the formatter |
|
|
252 |
* @see http://www.php.net/manual/en/intldateformatter.gettimetype.php |
|
|
253 |
*/ |
|
|
254 |
public function getTimeType() |
|
|
255 |
{ |
|
|
256 |
return $this->timetype; |
|
|
257 |
} |
|
|
258 |
|
|
|
259 |
/** |
|
|
260 |
* Returns the formatter's timezone identifier |
|
|
261 |
* |
|
|
262 |
* @return string The timezone identifier used by the formatter |
|
|
263 |
* @see http://www.php.net/manual/en/intldateformatter.gettimezoneid.php |
|
|
264 |
*/ |
|
|
265 |
public function getTimeZoneId() |
|
|
266 |
{ |
|
|
267 |
if (!$this->unitializedTimeZoneId) { |
|
|
268 |
return $this->timeZoneId; |
|
|
269 |
} |
|
|
270 |
|
|
|
271 |
return null; |
|
|
272 |
} |
|
|
273 |
|
|
|
274 |
/** |
|
|
275 |
* Returns whether the formatter is lenient |
|
|
276 |
* |
|
|
277 |
* @return string The timezone identifier used by the formatter |
|
|
278 |
* @see http://www.php.net/manual/en/intldateformatter.islenient.php |
|
|
279 |
* @throws MethodNotImplementedException |
|
|
280 |
*/ |
|
|
281 |
public function isLenient() |
|
|
282 |
{ |
|
|
283 |
throw new MethodNotImplementedException(__METHOD__); |
|
|
284 |
} |
|
|
285 |
|
|
|
286 |
/** |
|
|
287 |
* Parse string to a field-based time value |
|
|
288 |
* |
|
|
289 |
* @param string $value String to convert to a time value |
|
|
290 |
* @param int $position Position at which to start the parsing in $value (zero-based). |
|
|
291 |
* If no error occurs before $value is consumed, $parse_pos will |
|
|
292 |
* contain -1 otherwise it will contain the position at which parsing |
|
|
293 |
* ended. If $parse_pos > strlen($value), the parse fails immediately. |
|
|
294 |
* @return string Localtime compatible array of integers: contains 24 hour clock value in tm_hour field |
|
|
295 |
* @see http://www.php.net/manual/en/intldateformatter.localtime.php |
|
|
296 |
* @throws MethodNotImplementedException |
|
|
297 |
*/ |
|
|
298 |
public function localtime($value, &$position = 0) |
|
|
299 |
{ |
|
|
300 |
throw new MethodNotImplementedException(__METHOD__); |
|
|
301 |
} |
|
|
302 |
|
|
|
303 |
/** |
|
|
304 |
* Parse string to a timestamp value |
|
|
305 |
* |
|
|
306 |
* @param string $value String to convert to a time value |
|
|
307 |
* @param int $position Position at which to start the parsing in $value (zero-based). |
|
|
308 |
* If no error occurs before $value is consumed, $parse_pos will |
|
|
309 |
* contain -1 otherwise it will contain the position at which parsing |
|
|
310 |
* ended. If $parse_pos > strlen($value), the parse fails immediately. |
|
|
311 |
* @return string Parsed value as a timestamp |
|
|
312 |
* @see http://www.php.net/manual/en/intldateformatter.parse.php |
|
|
313 |
* @throws MethodArgumentNotImplementedException When $position different than null, behavior not implemented |
|
|
314 |
*/ |
|
|
315 |
public function parse($value, &$position = null) |
|
|
316 |
{ |
|
|
317 |
// We don't calculate the position when parsing the value |
|
|
318 |
if (null !== $position) { |
|
|
319 |
throw new MethodArgumentNotImplementedException(__METHOD__, 'position'); |
|
|
320 |
} |
|
|
321 |
|
|
|
322 |
StubIntl::setErrorCode(StubIntl::U_ZERO_ERROR); |
|
|
323 |
|
|
|
324 |
$dateTime = $this->createDateTime(0); |
|
|
325 |
$transformer = new FullTransformer($this->getPattern(), $this->getTimeZoneId()); |
|
|
326 |
|
|
|
327 |
return $transformer->parse($dateTime, $value); |
|
|
328 |
} |
|
|
329 |
|
|
|
330 |
/** |
|
|
331 |
* Set the formatter's calendar |
|
|
332 |
* |
|
|
333 |
* @param string $calendar The calendar to use. Default is IntlDateFormatter::GREGORIAN. |
|
|
334 |
* @return Boolean true on success or false on failure |
|
|
335 |
* @see http://www.php.net/manual/en/intldateformatter.setcalendar.php |
|
|
336 |
* @throws MethodNotImplementedException |
|
|
337 |
*/ |
|
|
338 |
public function setCalendar($calendar) |
|
|
339 |
{ |
|
|
340 |
throw new MethodNotImplementedException(__METHOD__); |
|
|
341 |
} |
|
|
342 |
|
|
|
343 |
/** |
|
|
344 |
* Set the leniency of the parser |
|
|
345 |
* |
|
|
346 |
* Define if the parser is strict or lenient in interpreting inputs that do not match the pattern |
|
|
347 |
* exactly. Enabling lenient parsing allows the parser to accept otherwise flawed date or time |
|
|
348 |
* patterns, parsing as much as possible to obtain a value. Extra space, unrecognized tokens, or |
|
|
349 |
* invalid values ("February 30th") are not accepted. |
|
|
350 |
* |
|
|
351 |
* @param Boolean $lenient Sets whether the parser is lenient or not, default is false (strict) |
|
|
352 |
* @return Boolean true on success or false on failure |
|
|
353 |
* @see http://www.php.net/manual/en/intldateformatter.setlenient.php |
|
|
354 |
* @throws MethodNotImplementedException |
|
|
355 |
*/ |
|
|
356 |
public function setLenient($lenient) |
|
|
357 |
{ |
|
|
358 |
throw new MethodNotImplementedException(__METHOD__); |
|
|
359 |
} |
|
|
360 |
|
|
|
361 |
/** |
|
|
362 |
* Set the formatter's pattern |
|
|
363 |
* |
|
|
364 |
* @param string $pattern A pattern string in conformance with the ICU IntlDateFormatter documentation |
|
|
365 |
* @return Boolean true on success or false on failure |
|
|
366 |
* @see http://www.php.net/manual/en/intldateformatter.setpattern.php |
|
|
367 |
* @see http://userguide.icu-project.org/formatparse/datetime |
|
|
368 |
*/ |
|
|
369 |
public function setPattern($pattern) |
|
|
370 |
{ |
|
|
371 |
if (null === $pattern) { |
|
|
372 |
$pattern = $this->getDefaultPattern(); |
|
|
373 |
} |
|
|
374 |
|
|
|
375 |
$this->pattern = $pattern; |
|
|
376 |
} |
|
|
377 |
|
|
|
378 |
/** |
|
|
379 |
* Set the formatter's timezone identifier |
|
|
380 |
* |
|
|
381 |
* @param string $timeZoneId The time zone ID string of the time zone to use. |
|
|
382 |
* If NULL or the empty string, the default time zone for the |
|
|
383 |
* runtime is used. |
|
|
384 |
* @return Boolean true on success or false on failure |
|
|
385 |
* @see http://www.php.net/manual/en/intldateformatter.settimezoneid.php |
|
|
386 |
*/ |
|
|
387 |
public function setTimeZoneId($timeZoneId) |
|
|
388 |
{ |
|
|
389 |
if (null === $timeZoneId) { |
|
|
390 |
$timeZoneId = date_default_timezone_get(); |
|
|
391 |
$this->unitializedTimeZoneId = true; |
|
|
392 |
} |
|
|
393 |
|
|
|
394 |
// Backup original passed time zone |
|
|
395 |
$timeZone = $timeZoneId; |
|
|
396 |
|
|
|
397 |
// Get an Etc/GMT time zone that is accepted for \DateTimeZone |
|
|
398 |
if ('GMT' !== $timeZoneId && 'GMT' === substr($timeZoneId, 0, 3)) { |
|
|
399 |
try { |
|
|
400 |
$timeZoneId = DateFormat\TimeZoneTransformer::getEtcTimeZoneId($timeZoneId); |
|
|
401 |
} catch (\InvalidArgumentException $e) { |
|
|
402 |
// Does nothing, will fallback to UTC |
|
|
403 |
} |
|
|
404 |
} |
|
|
405 |
|
|
|
406 |
try { |
|
|
407 |
$this->dateTimeZone = new \DateTimeZone($timeZoneId); |
|
|
408 |
} catch (\Exception $e) { |
|
|
409 |
$this->dateTimeZone = new \DateTimeZone('UTC'); |
|
|
410 |
} |
|
|
411 |
|
|
|
412 |
$this->timeZoneId = $timeZone; |
|
|
413 |
|
|
|
414 |
return true; |
|
|
415 |
} |
|
|
416 |
|
|
|
417 |
/** |
|
|
418 |
* Create and returns a DateTime object with the specified timestamp and with the |
|
|
419 |
* current time zone |
|
|
420 |
* |
|
|
421 |
* @param int $timestamp |
|
|
422 |
* @return DateTime |
|
|
423 |
*/ |
|
|
424 |
protected function createDateTime($timestamp) |
|
|
425 |
{ |
|
|
426 |
$dateTime = new \DateTime(); |
|
|
427 |
$dateTime->setTimestamp($timestamp); |
|
|
428 |
$dateTime->setTimezone($this->dateTimeZone); |
|
|
429 |
|
|
|
430 |
return $dateTime; |
|
|
431 |
} |
|
|
432 |
|
|
|
433 |
/** |
|
|
434 |
* Returns a pattern string based in the datetype and timetype values |
|
|
435 |
* @return string |
|
|
436 |
*/ |
|
|
437 |
protected function getDefaultPattern() |
|
|
438 |
{ |
|
|
439 |
$patternParts = array(); |
|
|
440 |
if (self::NONE !== $this->datetype) { |
|
|
441 |
$patternParts[] = $this->defaultDateFormats[$this->datetype]; |
|
|
442 |
} |
|
|
443 |
if (self::NONE !== $this->timetype) { |
|
|
444 |
$patternParts[] = $this->defaultTimeFormats[$this->timetype]; |
|
|
445 |
} |
|
|
446 |
$pattern = implode(' ', $patternParts); |
|
|
447 |
|
|
|
448 |
return $pattern; |
|
|
449 |
} |
|
|
450 |
} |