DateIntervalNormalizer.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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\Serializer\Normalizer;
  11. use Symfony\Component\Serializer\Exception\InvalidArgumentException;
  12. use Symfony\Component\Serializer\Exception\UnexpectedValueException;
  13. /**
  14. * Normalizes an instance of {@see \DateInterval} to an interval string.
  15. * Denormalizes an interval string to an instance of {@see \DateInterval}.
  16. *
  17. * @author Jérôme Parmentier <jerome@prmntr.me>
  18. */
  19. class DateIntervalNormalizer implements NormalizerInterface, DenormalizerInterface, CacheableSupportsMethodInterface
  20. {
  21. public const FORMAT_KEY = 'dateinterval_format';
  22. private $defaultContext = [
  23. self::FORMAT_KEY => '%rP%yY%mM%dDT%hH%iM%sS',
  24. ];
  25. public function __construct(array $defaultContext = [])
  26. {
  27. $this->defaultContext = array_merge($this->defaultContext, $defaultContext);
  28. }
  29. /**
  30. * {@inheritdoc}
  31. *
  32. * @throws InvalidArgumentException
  33. *
  34. * @return string
  35. */
  36. public function normalize($object, string $format = null, array $context = [])
  37. {
  38. if (!$object instanceof \DateInterval) {
  39. throw new InvalidArgumentException('The object must be an instance of "\DateInterval".');
  40. }
  41. return $object->format($context[self::FORMAT_KEY] ?? $this->defaultContext[self::FORMAT_KEY]);
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function supportsNormalization($data, string $format = null)
  47. {
  48. return $data instanceof \DateInterval;
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function hasCacheableSupportsMethod(): bool
  54. {
  55. return __CLASS__ === static::class;
  56. }
  57. /**
  58. * {@inheritdoc}
  59. *
  60. * @throws InvalidArgumentException
  61. * @throws UnexpectedValueException
  62. *
  63. * @return \DateInterval
  64. */
  65. public function denormalize($data, string $type, string $format = null, array $context = [])
  66. {
  67. if (!\is_string($data)) {
  68. throw new InvalidArgumentException(sprintf('Data expected to be a string, "%s" given.', get_debug_type($data)));
  69. }
  70. if (!$this->isISO8601($data)) {
  71. throw new UnexpectedValueException('Expected a valid ISO 8601 interval string.');
  72. }
  73. $dateIntervalFormat = $context[self::FORMAT_KEY] ?? $this->defaultContext[self::FORMAT_KEY];
  74. $signPattern = '';
  75. switch (substr($dateIntervalFormat, 0, 2)) {
  76. case '%R':
  77. $signPattern = '[-+]';
  78. $dateIntervalFormat = substr($dateIntervalFormat, 2);
  79. break;
  80. case '%r':
  81. $signPattern = '-?';
  82. $dateIntervalFormat = substr($dateIntervalFormat, 2);
  83. break;
  84. }
  85. $valuePattern = '/^'.$signPattern.preg_replace('/%([yYmMdDhHiIsSwW])(\w)/', '(?:(?P<$1>\d+)$2)?', preg_replace('/(T.*)$/', '($1)?', $dateIntervalFormat)).'$/';
  86. if (!preg_match($valuePattern, $data)) {
  87. throw new UnexpectedValueException(sprintf('Value "%s" contains intervals not accepted by format "%s".', $data, $dateIntervalFormat));
  88. }
  89. try {
  90. if ('-' === $data[0]) {
  91. $interval = new \DateInterval(substr($data, 1));
  92. $interval->invert = 1;
  93. return $interval;
  94. }
  95. if ('+' === $data[0]) {
  96. return new \DateInterval(substr($data, 1));
  97. }
  98. return new \DateInterval($data);
  99. } catch (\Exception $e) {
  100. throw new UnexpectedValueException($e->getMessage(), $e->getCode(), $e);
  101. }
  102. }
  103. /**
  104. * {@inheritdoc}
  105. */
  106. public function supportsDenormalization($data, string $type, string $format = null)
  107. {
  108. return \DateInterval::class === $type;
  109. }
  110. private function isISO8601(string $string): bool
  111. {
  112. return preg_match('/^[\-+]?P(?=\w*(?:\d|%\w))(?:\d+Y|%[yY]Y)?(?:\d+M|%[mM]M)?(?:(?:\d+D|%[dD]D)|(?:\d+W|%[wW]W))?(?:T(?:\d+H|[hH]H)?(?:\d+M|[iI]M)?(?:\d+S|[sS]S)?)?$/', $string);
  113. }
  114. }