YearTransformer.php 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 year format.
  13. *
  14. * @author Igor Wiedler <igor@wiedler.ch>
  15. *
  16. * @internal
  17. */
  18. class YearTransformer extends Transformer
  19. {
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public function format(\DateTime $dateTime, int $length): string
  24. {
  25. if (2 === $length) {
  26. return $dateTime->format('y');
  27. }
  28. return $this->padLeft($dateTime->format('Y'), $length);
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function getReverseMatchingRegExp(int $length): string
  34. {
  35. return 2 === $length ? '\d{2}' : '\d{1,4}';
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function extractDateOptions(string $matched, int $length): array
  41. {
  42. return [
  43. 'year' => (int) $matched,
  44. ];
  45. }
  46. }