MonthTransformer.php 3.1 KB

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