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