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