YearTransformer.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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\Locale\Stub\DateFormat;
  11. /**
  12. * Parser and formatter for year format
  13. *
  14. * @author Igor Wiedler <igor@wiedler.ch>
  15. */
  16. class YearTransformer extends Transformer
  17. {
  18. /**
  19. * {@inheritDoc}
  20. */
  21. public function format(\DateTime $dateTime, $length)
  22. {
  23. if (2 == $length) {
  24. return $dateTime->format('y');
  25. } else {
  26. return $this->padLeft($dateTime->format('Y'), $length);
  27. }
  28. }
  29. /**
  30. * {@inheritDoc}
  31. */
  32. public function getReverseMatchingRegExp($length)
  33. {
  34. if (2 == $length) {
  35. $regExp = '\d{2}';
  36. } else {
  37. $regExp = '\d{4}';
  38. }
  39. return $regExp;
  40. }
  41. /**
  42. * {@inheritDoc}
  43. */
  44. public function extractDateOptions($matched, $length)
  45. {
  46. return array(
  47. 'year' => (int) $matched,
  48. );
  49. }
  50. }