MonthTransformer.php 3.2 KB

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