StubIntlDateFormatter.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Locale\Stub;
  11. use Symfony\Component\Locale\Locale;
  12. /**
  13. * Provides a stub IntlDateFormatter for the 'en' locale.
  14. */
  15. class StubIntlDateFormatter
  16. {
  17. /* formats */
  18. const NONE = -1;
  19. const FULL = 0;
  20. const LONG = 1;
  21. const MEDIUM = 2;
  22. const SHORT = 3;
  23. /* formats */
  24. const TRADITIONAL = 0;
  25. const GREGORIAN = 1;
  26. private $defaultDateFormats = array(
  27. self::NONE => '',
  28. self::FULL => 'EEEE, LLLL d, y',
  29. self::LONG => 'LLLL d, y',
  30. self::MEDIUM => 'LLL d, y',
  31. self::SHORT => 'M/d/yy',
  32. );
  33. private $defaultTimeFormats = array(
  34. self::FULL => 'h:mm:ss a zzzz',
  35. self::LONG => 'h:mm:ss a z',
  36. self::MEDIUM => 'h:mm:ss a',
  37. self::SHORT => 'h:mm a',
  38. );
  39. private $datetype;
  40. private $timetype;
  41. private $pattern;
  42. private $dateTimeZone;
  43. public function __construct($locale, $datetype, $timetype, $timezone = null, $calendar = null, $pattern = null)
  44. {
  45. if ('en' != $locale) {
  46. throw new \InvalidArgumentException('Unsupported $locale value. Only the \'en\' locale is supported. Install the intl extension for full localization capabilities.');
  47. }
  48. $this->datetype = $datetype;
  49. $this->timetype = $timetype;
  50. $this->setPattern($pattern);
  51. try {
  52. $this->dateTimeZone = new \DateTimeZone($timezone);
  53. } catch (\Exception $e) {
  54. $this->dateTimeZone = new \DateTimeZone('UTC');
  55. }
  56. }
  57. public function format($timestamp)
  58. {
  59. $dateTime = new \DateTime();
  60. $dateTime->setTimestamp($timestamp);
  61. $dateTime->setTimezone($this->dateTimeZone);
  62. // not implemented: YuwWFgecSAZvVW
  63. $specialChars = 'MLydGQqhDEaHkKmsz';
  64. $specialCharsArray = str_split($specialChars);
  65. $specialCharsMatch = implode('|', array_map(function($char) {
  66. return $char . '+';
  67. }, $specialCharsArray));
  68. $quoteMatch = "'(?>(?>[^']+|'{2}+)?)'|'{2}";
  69. $regExp = "/($quoteMatch|$specialCharsMatch)/";
  70. $callback = function($matches) use ($dateTime) {
  71. $pattern = $matches[0];
  72. $length = strlen($pattern);
  73. if ("'" === $pattern[0]) {
  74. return preg_replace("/'{2}/", "'", substr($pattern, 1, -1));
  75. }
  76. switch ($pattern[0]) {
  77. case 'M':
  78. case 'L':
  79. $matchLengthMap = array(
  80. 1 => 'n',
  81. 2 => 'm',
  82. 3 => 'M',
  83. 4 => 'F',
  84. );
  85. if (isset($matchLengthMap[$length])) {
  86. return $dateTime->format($matchLengthMap[$length]);
  87. } else if (5 == $length) {
  88. return substr($dateTime->format('M'), 0, 1);
  89. } else {
  90. return str_pad($dateTime->format('m'), $length, '0', STR_PAD_LEFT);
  91. }
  92. break;
  93. case 'y':
  94. $matchLengthMap = array(
  95. 1 => 'Y',
  96. 2 => 'y',
  97. 3 => 'Y',
  98. 4 => 'Y',
  99. );
  100. if (isset($matchLengthMap[$length])) {
  101. return $dateTime->format($matchLengthMap[$length]);
  102. } else {
  103. return str_pad($dateTime->format('Y'), $length, '0', STR_PAD_LEFT);
  104. }
  105. break;
  106. case 'd':
  107. return str_pad($dateTime->format('j'), $length, '0', STR_PAD_LEFT);
  108. break;
  109. case 'G':
  110. $year = (int) $dateTime->format('Y');
  111. return $year >= 0 ? 'AD' : 'BC';
  112. break;
  113. case 'q':
  114. case 'Q':
  115. $month = (int) $dateTime->format('n');
  116. $quarter = (int) floor(($month - 1) / 3) + 1;
  117. switch ($length) {
  118. case 1:
  119. case 2:
  120. return str_pad($quarter, $length, '0', STR_PAD_LEFT);
  121. break;
  122. case 3:
  123. return 'Q' . $quarter;
  124. break;
  125. default:
  126. $map = array(1 => '1st quarter', 2 => '2nd quarter', 3 => '3rd quarter', 4 => '4th quarter');
  127. return $map[$quarter];
  128. break;
  129. }
  130. break;
  131. case 'h':
  132. return str_pad($dateTime->format('g'), $length, '0', STR_PAD_LEFT);
  133. break;
  134. case 'D':
  135. $dayOfYear = $dateTime->format('z') + 1;
  136. return str_pad($dayOfYear, $length, '0', STR_PAD_LEFT);
  137. break;
  138. case 'E':
  139. $dayOfWeek = $dateTime->format('l');
  140. switch ($length) {
  141. case 4:
  142. return $dayOfWeek;
  143. break;
  144. case 5:
  145. return $dayOfWeek[0];
  146. break;
  147. default:
  148. return substr($dayOfWeek, 0, 3);
  149. }
  150. break;
  151. case 'a':
  152. return $dateTime->format('A');
  153. break;
  154. case 'H':
  155. return str_pad($dateTime->format('G'), $length, '0', STR_PAD_LEFT);
  156. break;
  157. case 'k':
  158. $hourOfDay = $dateTime->format('G');
  159. $hourOfDay = ('0' == $hourOfDay) ? '24' : $hourOfDay;
  160. return str_pad($hourOfDay, $length, '0', STR_PAD_LEFT);
  161. break;
  162. case 'K':
  163. $hourOfDay = $dateTime->format('g');
  164. $hourOfDay = ('12' == $hourOfDay) ? '0' : $hourOfDay;
  165. return str_pad($hourOfDay, $length, '0', STR_PAD_LEFT);
  166. break;
  167. case 'm':
  168. $minuteOfHour = (int) $dateTime->format('i');
  169. return str_pad($minuteOfHour, $length, '0', STR_PAD_LEFT);
  170. break;
  171. case 's':
  172. $secondOfMinute = (int) $dateTime->format('s');
  173. return str_pad($secondOfMinute, $length, '0', STR_PAD_LEFT);
  174. break;
  175. case 'z':
  176. return $dateTime->format('\G\M\TP');
  177. break;
  178. }
  179. };
  180. $formatted = preg_replace_callback($regExp, $callback, $this->getPattern());
  181. return $formatted;
  182. }
  183. public function getLocale()
  184. {
  185. return 'en';
  186. }
  187. public function getPattern()
  188. {
  189. return $this->pattern;
  190. }
  191. public function getCalendar()
  192. {
  193. return self::GREGORIAN;
  194. }
  195. public function setLocale($locale)
  196. {
  197. $this->throwMethodNotImplementException(__METHOD__);
  198. }
  199. public function setPattern($pattern)
  200. {
  201. if (null === $pattern) {
  202. $patternParts = array();
  203. if (self::NONE !== $this->datetype) {
  204. $patternParts[] = $this->defaultDateFormats[$this->datetype];
  205. }
  206. if (self::NONE !== $this->timetype) {
  207. $patternParts[] = $this->defaultTimeFormats[$this->timetype];
  208. }
  209. $pattern = implode(' ', $patternParts);
  210. }
  211. $this->pattern = $pattern;
  212. }
  213. public function setCalendar()
  214. {
  215. $this->throwMethodNotImplementException(__METHOD__);
  216. }
  217. private function throwMethodNotImplementException($methodName)
  218. {
  219. $message = sprintf('The %s::%s() is not implemented. Install the intl extension for full localization capabilities.', __CLASS__, $methodName);
  220. throw new \RuntimeException($message);
  221. }
  222. }