Transformer.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 date formats
  13. *
  14. * @author Igor Wiedler <igor@wiedler.ch>
  15. */
  16. abstract class Transformer
  17. {
  18. /**
  19. * Format a value using a configured DateTime as date/time source
  20. *
  21. *
  22. * @param DateTime $dateTime A DateTime object to be used to generate the formatted value
  23. * @param int $length The formatted value string length
  24. * @return string The formatted value
  25. */
  26. abstract public function format(\DateTime $dateTime, $length);
  27. /**
  28. * Returns a reverse matching regular expression of a string generated by format()
  29. *
  30. * @param int $length The length of the value to be reverse matched
  31. * @return string The reverse matching regular expression
  32. */
  33. abstract public function getReverseMatchingRegExp($length);
  34. /**
  35. * Extract date options from a matched value returned by the processing of the reverse matching
  36. * regular expression
  37. *
  38. * @param string $matched The matched value
  39. * @param int $length The length of the Transformer pattern string
  40. * @return array An associative array
  41. */
  42. abstract public function extractDateOptions($matched, $length);
  43. /**
  44. * Pad a string with zeros to the left
  45. *
  46. * @param string $value The string to be padded
  47. * @param int $length The length to pad
  48. * @return string The padded string
  49. */
  50. protected function padLeft($value, $length)
  51. {
  52. return str_pad($value, $length, '0', STR_PAD_LEFT);
  53. }
  54. }