Transformer.php 1.8 KB

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