MonthTransformer.php 3.2 KB

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