StubIntlDateFormatter.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. public function __construct($locale, $datetype, $timetype, $timezone = null, $calendar = null, $pattern = null)
  27. {
  28. if ('en' != $locale) {
  29. throw new \InvalidArgumentException('Unsupported $locale value. Only the \'en\' locale is supported. Install the intl extension for full localization capabilities.');
  30. }
  31. $this->setPattern($pattern);
  32. }
  33. public function format($timestamp)
  34. {
  35. $specialChars = 'MLydGQqhDEaHkKm';
  36. $specialCharsArray = str_split($specialChars);
  37. $specialCharsMatch = implode('|', array_map(function($char) {
  38. return $char . '+';
  39. }, $specialCharsArray));
  40. $regExp = "/('($specialCharsMatch|[^$specialChars])|$specialCharsMatch)/";
  41. $callback = function($matches) use ($timestamp) {
  42. $pattern = $matches[0];
  43. $length = strlen($pattern);
  44. if ("'" === $pattern[0]) {
  45. return substr($pattern, 1);
  46. }
  47. switch ($pattern[0]) {
  48. case 'M':
  49. case 'L':
  50. $matchLengthMap = array(
  51. 1 => 'n',
  52. 2 => 'm',
  53. 3 => 'M',
  54. 4 => 'F',
  55. );
  56. if (isset($matchLengthMap[$length])) {
  57. return gmdate($matchLengthMap[$length], $timestamp);
  58. } else if (5 == $length) {
  59. return substr(gmdate('M', $timestamp), 0, 1);
  60. } else {
  61. return str_pad(gmdate('m', $timestamp), $length, '0', STR_PAD_LEFT);
  62. }
  63. break;
  64. case 'y':
  65. $matchLengthMap = array(
  66. 1 => 'Y',
  67. 2 => 'y',
  68. 3 => 'Y',
  69. 4 => 'Y',
  70. );
  71. if (isset($matchLengthMap[$length])) {
  72. return gmdate($matchLengthMap[$length], $timestamp);
  73. } else {
  74. return str_pad(gmdate('Y', $timestamp), $length, '0', STR_PAD_LEFT);
  75. }
  76. break;
  77. case 'd':
  78. return str_pad(gmdate('j', $timestamp), $length, '0', STR_PAD_LEFT);
  79. break;
  80. case 'G':
  81. $year = (int) gmdate('Y', $timestamp);
  82. return $year >= 0 ? 'AD' : 'BC';
  83. break;
  84. case 'q':
  85. case 'Q':
  86. $month = (int) gmdate('n', $timestamp);
  87. $quarter = (int) floor(($month - 1) / 3) + 1;
  88. switch ($length) {
  89. case 1:
  90. case 2:
  91. return str_pad($quarter, $length, '0', STR_PAD_LEFT);
  92. break;
  93. case 3:
  94. return 'Q' . $quarter;
  95. break;
  96. default:
  97. $map = array(1 => '1st quarter', 2 => '2nd quarter', 3 => '3rd quarter', 4 => '4th quarter');
  98. return $map[$quarter];
  99. break;
  100. }
  101. break;
  102. case 'h':
  103. return str_pad(gmdate('g', $timestamp), $length, '0', STR_PAD_LEFT);
  104. break;
  105. case 'D':
  106. $dayOfYear = gmdate('z', $timestamp) + 1;
  107. return str_pad($dayOfYear, $length, '0', STR_PAD_LEFT);
  108. break;
  109. case 'E':
  110. $dayOfWeek = gmdate('l', $timestamp);
  111. switch ($length) {
  112. case 4:
  113. return $dayOfWeek;
  114. break;
  115. case 5:
  116. return $dayOfWeek[0];
  117. break;
  118. default:
  119. return substr($dayOfWeek, 0, 3);
  120. }
  121. break;
  122. case 'a':
  123. return gmdate('A', $timestamp);
  124. break;
  125. case 'H':
  126. return str_pad(gmdate('G', $timestamp), $length, '0', STR_PAD_LEFT);
  127. break;
  128. case 'k':
  129. $hourOfDay = gmdate('G', $timestamp);
  130. $hourOfDay = ('0' == $hourOfDay) ? '24' : $hourOfDay;
  131. return str_pad($hourOfDay, $length, '0', STR_PAD_LEFT);
  132. break;
  133. case 'K':
  134. $hourOfDay = gmdate('g', $timestamp);
  135. $hourOfDay = ('12' == $hourOfDay) ? '0' : $hourOfDay;
  136. return str_pad($hourOfDay, $length, '0', STR_PAD_LEFT);
  137. break;
  138. case 'm':
  139. $minuteOfHour = (int) gmdate('i', $timestamp);
  140. return str_pad($minuteOfHour, $length, '0', STR_PAD_LEFT);
  141. break;
  142. }
  143. };
  144. $formatted = preg_replace_callback($regExp, $callback, $this->getPattern());
  145. return $formatted;
  146. }
  147. public function getPattern()
  148. {
  149. return $this->pattern;
  150. }
  151. public function getCalendar()
  152. {
  153. $this->throwMethodNotImplementException(__METHOD__);
  154. }
  155. public function setPattern($pattern)
  156. {
  157. $this->pattern = $pattern;
  158. }
  159. private function throwMethodNotImplementException($methodName)
  160. {
  161. $message = sprintf('The %s::%s() is not implemented. Install the intl extension for full localization capabilities.', __CLASS__, $methodName);
  162. throw new \RuntimeException($message);
  163. }
  164. }