QuarterTransformer.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 quarter format
  13. *
  14. * @author Igor Wiedler <igor@wiedler.ch>
  15. */
  16. class QuarterTransformer extends Transformer
  17. {
  18. /**
  19. * {@inheritDoc}
  20. */
  21. public function format(\DateTime $dateTime, $length)
  22. {
  23. $month = (int) $dateTime->format('n');
  24. $quarter = (int) floor(($month - 1) / 3) + 1;
  25. switch ($length) {
  26. case 1:
  27. case 2:
  28. return $this->padLeft($quarter, $length);
  29. case 3:
  30. return 'Q' . $quarter;
  31. default:
  32. $map = array(1 => '1st quarter', 2 => '2nd quarter', 3 => '3rd quarter', 4 => '4th quarter');
  33. return $map[$quarter];
  34. }
  35. }
  36. /**
  37. * {@inheritDoc}
  38. */
  39. public function getReverseMatchingRegExp($length)
  40. {
  41. switch ($length) {
  42. case 1:
  43. case 2:
  44. return '\d{'.$length.'}';
  45. case 3:
  46. return 'Q\d';
  47. default:
  48. return '(?:1st|2nd|3rd|4th) quarter';
  49. }
  50. }
  51. /**
  52. * {@inheritDoc}
  53. */
  54. public function extractDateOptions($matched, $length)
  55. {
  56. return array();
  57. }
  58. }