Hour1200Transformer.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 12 hour format (0-11).
  13. *
  14. * @author Igor Wiedler <igor@wiedler.ch>
  15. *
  16. * @internal
  17. */
  18. class Hour1200Transformer extends HourTransformer
  19. {
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public function format(\DateTime $dateTime, int $length): string
  24. {
  25. $hourOfDay = $dateTime->format('g');
  26. $hourOfDay = '12' === $hourOfDay ? '0' : $hourOfDay;
  27. return $this->padLeft($hourOfDay, $length);
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function normalizeHour(int $hour, string $marker = null): int
  33. {
  34. if ('PM' === $marker) {
  35. $hour += 12;
  36. }
  37. return $hour;
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function getReverseMatchingRegExp(int $length): string
  43. {
  44. return '\d{1,2}';
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function extractDateOptions(string $matched, int $length): array
  50. {
  51. return [
  52. 'hour' => (int) $matched,
  53. 'hourInstance' => $this,
  54. ];
  55. }
  56. }