Transformer.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 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. * @param \DateTime $dateTime A DateTime object to be used to generate the formatted value
  24. * @param int $length The formatted value string length
  25. *
  26. * @return string The formatted value
  27. */
  28. abstract public function format(\DateTime $dateTime, int $length): string;
  29. /**
  30. * Returns a reverse matching regular expression of a string generated by format().
  31. *
  32. * @param int $length The length of the value to be reverse matched
  33. *
  34. * @return string The reverse matching regular expression
  35. */
  36. abstract public function getReverseMatchingRegExp(int $length): string;
  37. /**
  38. * Extract date options from a matched value returned by the processing of the reverse matching
  39. * regular expression.
  40. *
  41. * @param string $matched The matched value
  42. * @param int $length The length of the Transformer pattern string
  43. *
  44. * @return array An associative array
  45. */
  46. abstract public function extractDateOptions(string $matched, int $length): array;
  47. /**
  48. * Pad a string with zeros to the left.
  49. *
  50. * @param string $value The string to be padded
  51. * @param int $length The length to pad
  52. *
  53. * @return string The padded string
  54. */
  55. protected function padLeft(string $value, int $length): string
  56. {
  57. return str_pad($value, $length, '0', \STR_PAD_LEFT);
  58. }
  59. }