DayOfWeekTransformer.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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\Intl\DateFormatter\DateFormat;
  11. /**
  12. * Parser and formatter for day of week format.
  13. *
  14. * @author Igor Wiedler <igor@wiedler.ch>
  15. *
  16. * @internal
  17. */
  18. class DayOfWeekTransformer extends Transformer
  19. {
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public function format(\DateTime $dateTime, int $length): string
  24. {
  25. $dayOfWeek = $dateTime->format('l');
  26. switch ($length) {
  27. case 4:
  28. return $dayOfWeek;
  29. case 5:
  30. return $dayOfWeek[0];
  31. case 6:
  32. return substr($dayOfWeek, 0, 2);
  33. default:
  34. return substr($dayOfWeek, 0, 3);
  35. }
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function getReverseMatchingRegExp(int $length): string
  41. {
  42. switch ($length) {
  43. case 4:
  44. return 'Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday';
  45. case 5:
  46. return '[MTWFS]';
  47. case 6:
  48. return 'Mo|Tu|We|Th|Fr|Sa|Su';
  49. default:
  50. return 'Mon|Tue|Wed|Thu|Fri|Sat|Sun';
  51. }
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function extractDateOptions(string $matched, int $length): array
  57. {
  58. return [];
  59. }
  60. }