MonthTransformer.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.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\Intl\DateFormatter\DateFormat;
  11. /**
  12. * Parser and formatter for month format.
  13. *
  14. * @author Igor Wiedler <igor@wiedler.ch>
  15. *
  16. * @internal
  17. */
  18. class MonthTransformer extends Transformer
  19. {
  20. /**
  21. * @var array
  22. */
  23. protected static $months = array(
  24. 'January',
  25. 'February',
  26. 'March',
  27. 'April',
  28. 'May',
  29. 'June',
  30. 'July',
  31. 'August',
  32. 'September',
  33. 'October',
  34. 'November',
  35. 'December',
  36. );
  37. /**
  38. * Short months names (first 3 letters).
  39. *
  40. * @var array
  41. */
  42. protected static $shortMonths = array();
  43. /**
  44. * Flipped $months array, $name => $index.
  45. *
  46. * @var array
  47. */
  48. protected static $flippedMonths = array();
  49. /**
  50. * Flipped $shortMonths array, $name => $index.
  51. *
  52. * @var array
  53. */
  54. protected static $flippedShortMonths = array();
  55. /**
  56. * Constructor.
  57. */
  58. public function __construct()
  59. {
  60. if (0 === count(self::$shortMonths)) {
  61. self::$shortMonths = array_map(function ($month) {
  62. return substr($month, 0, 3);
  63. }, self::$months);
  64. self::$flippedMonths = array_flip(self::$months);
  65. self::$flippedShortMonths = array_flip(self::$shortMonths);
  66. }
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public function format(\DateTime $dateTime, $length)
  72. {
  73. $matchLengthMap = array(
  74. 1 => 'n',
  75. 2 => 'm',
  76. 3 => 'M',
  77. 4 => 'F',
  78. );
  79. if (isset($matchLengthMap[$length])) {
  80. return $dateTime->format($matchLengthMap[$length]);
  81. }
  82. if (5 === $length) {
  83. return substr($dateTime->format('M'), 0, 1);
  84. }
  85. return $this->padLeft($dateTime->format('m'), $length);
  86. }
  87. /**
  88. * {@inheritdoc}
  89. */
  90. public function getReverseMatchingRegExp($length)
  91. {
  92. switch ($length) {
  93. case 1:
  94. $regExp = '\d{1,2}';
  95. break;
  96. case 3:
  97. $regExp = implode('|', self::$shortMonths);
  98. break;
  99. case 4:
  100. $regExp = implode('|', self::$months);
  101. break;
  102. case 5:
  103. $regExp = '[JFMASOND]';
  104. break;
  105. default:
  106. $regExp = '\d{'.$length.'}';
  107. break;
  108. }
  109. return $regExp;
  110. }
  111. /**
  112. * {@inheritdoc}
  113. */
  114. public function extractDateOptions($matched, $length)
  115. {
  116. if (!is_numeric($matched)) {
  117. if (3 === $length) {
  118. $matched = self::$flippedShortMonths[$matched] + 1;
  119. } elseif (4 === $length) {
  120. $matched = self::$flippedMonths[$matched] + 1;
  121. } elseif (5 === $length) {
  122. // IntlDateFormatter::parse() always returns false for MMMMM or LLLLL
  123. $matched = false;
  124. }
  125. } else {
  126. $matched = (int) $matched;
  127. }
  128. return array(
  129. 'month' => $matched,
  130. );
  131. }
  132. }