123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Component\Locale\Stub;
- use Symfony\Component\Locale\Locale;
- /**
- * Provides a stub IntlDateFormatter for the 'en' locale.
- */
- class StubIntlDateFormatter
- {
- /* formats */
- const NONE = -1;
- const FULL = 0;
- const LONG = 1;
- const MEDIUM = 2;
- const SHORT = 3;
- /* formats */
- const TRADITIONAL = 0;
- const GREGORIAN = 1;
- public function __construct($locale, $datetype, $timetype, $timezone = null, $calendar = null, $pattern = null)
- {
- if ('en' != $locale) {
- throw new \InvalidArgumentException('Unsupported $locale value. Only the \'en\' locale is supported. Install the intl extension for full localization capabilities.');
- }
- $this->setPattern($pattern);
- }
- public function format($timestamp)
- {
- $specialChars = 'MLydGQqhDEaHkKm';
- $specialCharsArray = str_split($specialChars);
- $specialCharsMatch = implode('|', array_map(function($char) {
- return $char . '+';
- }, $specialCharsArray));
- $regExp = "/('($specialCharsMatch|[^$specialChars])|$specialCharsMatch)/";
- $callback = function($matches) use ($timestamp) {
- $pattern = $matches[0];
- $length = strlen($pattern);
- if ("'" === $pattern[0]) {
- return substr($pattern, 1);
- }
- switch ($pattern[0]) {
- case 'M':
- case 'L':
- $matchLengthMap = array(
- 1 => 'n',
- 2 => 'm',
- 3 => 'M',
- 4 => 'F',
- );
- if (isset($matchLengthMap[$length])) {
- return gmdate($matchLengthMap[$length], $timestamp);
- } else if (5 == $length) {
- return substr(gmdate('M', $timestamp), 0, 1);
- } else {
- return str_pad(gmdate('m', $timestamp), $length, '0', STR_PAD_LEFT);
- }
- break;
- case 'y':
- $matchLengthMap = array(
- 1 => 'Y',
- 2 => 'y',
- 3 => 'Y',
- 4 => 'Y',
- );
- if (isset($matchLengthMap[$length])) {
- return gmdate($matchLengthMap[$length], $timestamp);
- } else {
- return str_pad(gmdate('Y', $timestamp), $length, '0', STR_PAD_LEFT);
- }
- break;
- case 'd':
- return str_pad(gmdate('j', $timestamp), $length, '0', STR_PAD_LEFT);
- break;
- case 'G':
- $year = (int) gmdate('Y', $timestamp);
- return $year >= 0 ? 'AD' : 'BC';
- break;
- case 'q':
- case 'Q':
- $month = (int) gmdate('n', $timestamp);
- $quarter = (int) floor(($month - 1) / 3) + 1;
- switch ($length) {
- case 1:
- case 2:
- return str_pad($quarter, $length, '0', STR_PAD_LEFT);
- break;
- case 3:
- return 'Q' . $quarter;
- break;
- default:
- $map = array(1 => '1st quarter', 2 => '2nd quarter', 3 => '3rd quarter', 4 => '4th quarter');
- return $map[$quarter];
- break;
- }
- break;
- case 'h':
- return str_pad(gmdate('g', $timestamp), $length, '0', STR_PAD_LEFT);
- break;
- case 'D':
- $dayOfYear = gmdate('z', $timestamp) + 1;
- return str_pad($dayOfYear, $length, '0', STR_PAD_LEFT);
- break;
- case 'E':
- $dayOfWeek = gmdate('l', $timestamp);
- switch ($length) {
- case 4:
- return $dayOfWeek;
- break;
- case 5:
- return $dayOfWeek[0];
- break;
- default:
- return substr($dayOfWeek, 0, 3);
- }
- break;
- case 'a':
- return gmdate('A', $timestamp);
- break;
- case 'H':
- return str_pad(gmdate('G', $timestamp), $length, '0', STR_PAD_LEFT);
- break;
- case 'k':
- $hourOfDay = gmdate('G', $timestamp);
- $hourOfDay = ('0' == $hourOfDay) ? '24' : $hourOfDay;
- return str_pad($hourOfDay, $length, '0', STR_PAD_LEFT);
- break;
- case 'K':
- $hourOfDay = gmdate('g', $timestamp);
- $hourOfDay = ('12' == $hourOfDay) ? '0' : $hourOfDay;
- return str_pad($hourOfDay, $length, '0', STR_PAD_LEFT);
- break;
- case 'm':
- $minuteOfHour = (int) gmdate('i', $timestamp);
- return str_pad($minuteOfHour, $length, '0', STR_PAD_LEFT);
- break;
- }
- };
- $formatted = preg_replace_callback($regExp, $callback, $this->getPattern());
- return $formatted;
- }
- public function getPattern()
- {
- return $this->pattern;
- }
- public function getCalendar()
- {
- $this->throwMethodNotImplementException(__METHOD__);
- }
- public function setPattern($pattern)
- {
- $this->pattern = $pattern;
- }
- private function throwMethodNotImplementException($methodName)
- {
- $message = sprintf('The %s::%s() is not implemented. Install the intl extension for full localization capabilities.', __CLASS__, $methodName);
- throw new \RuntimeException($message);
- }
- }
|