QuarterTransformer.php 1.5 KB

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