Chris@14
|
1 <?php
|
Chris@14
|
2
|
Chris@14
|
3 /*
|
Chris@14
|
4 * This file is part of the Symfony package.
|
Chris@14
|
5 *
|
Chris@14
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@14
|
7 *
|
Chris@14
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@14
|
9 * file that was distributed with this source code.
|
Chris@14
|
10 */
|
Chris@14
|
11
|
Chris@14
|
12 namespace Symfony\Component\Serializer\Normalizer;
|
Chris@14
|
13
|
Chris@14
|
14 use Symfony\Component\Serializer\Exception\InvalidArgumentException;
|
Chris@14
|
15 use Symfony\Component\Serializer\Exception\UnexpectedValueException;
|
Chris@14
|
16
|
Chris@14
|
17 /**
|
Chris@14
|
18 * Normalizes an instance of {@see \DateInterval} to an interval string.
|
Chris@14
|
19 * Denormalizes an interval string to an instance of {@see \DateInterval}.
|
Chris@14
|
20 *
|
Chris@14
|
21 * @author Jérôme Parmentier <jerome@prmntr.me>
|
Chris@14
|
22 */
|
Chris@14
|
23 class DateIntervalNormalizer implements NormalizerInterface, DenormalizerInterface
|
Chris@14
|
24 {
|
Chris@14
|
25 const FORMAT_KEY = 'dateinterval_format';
|
Chris@14
|
26
|
Chris@14
|
27 private $format;
|
Chris@14
|
28
|
Chris@14
|
29 /**
|
Chris@14
|
30 * @param string $format
|
Chris@14
|
31 */
|
Chris@14
|
32 public function __construct($format = 'P%yY%mM%dDT%hH%iM%sS')
|
Chris@14
|
33 {
|
Chris@14
|
34 $this->format = $format;
|
Chris@14
|
35 }
|
Chris@14
|
36
|
Chris@14
|
37 /**
|
Chris@14
|
38 * {@inheritdoc}
|
Chris@14
|
39 *
|
Chris@14
|
40 * @throws InvalidArgumentException
|
Chris@14
|
41 */
|
Chris@17
|
42 public function normalize($object, $format = null, array $context = [])
|
Chris@14
|
43 {
|
Chris@14
|
44 if (!$object instanceof \DateInterval) {
|
Chris@14
|
45 throw new InvalidArgumentException('The object must be an instance of "\DateInterval".');
|
Chris@14
|
46 }
|
Chris@14
|
47
|
Chris@14
|
48 $dateIntervalFormat = isset($context[self::FORMAT_KEY]) ? $context[self::FORMAT_KEY] : $this->format;
|
Chris@14
|
49
|
Chris@14
|
50 return $object->format($dateIntervalFormat);
|
Chris@14
|
51 }
|
Chris@14
|
52
|
Chris@14
|
53 /**
|
Chris@14
|
54 * {@inheritdoc}
|
Chris@14
|
55 */
|
Chris@14
|
56 public function supportsNormalization($data, $format = null)
|
Chris@14
|
57 {
|
Chris@14
|
58 return $data instanceof \DateInterval;
|
Chris@14
|
59 }
|
Chris@14
|
60
|
Chris@14
|
61 /**
|
Chris@14
|
62 * {@inheritdoc}
|
Chris@14
|
63 *
|
Chris@14
|
64 * @throws InvalidArgumentException
|
Chris@14
|
65 * @throws UnexpectedValueException
|
Chris@14
|
66 */
|
Chris@17
|
67 public function denormalize($data, $class, $format = null, array $context = [])
|
Chris@14
|
68 {
|
Chris@17
|
69 if (!\is_string($data)) {
|
Chris@17
|
70 throw new InvalidArgumentException(sprintf('Data expected to be a string, %s given.', \gettype($data)));
|
Chris@14
|
71 }
|
Chris@14
|
72
|
Chris@14
|
73 if (!$this->isISO8601($data)) {
|
Chris@14
|
74 throw new UnexpectedValueException('Expected a valid ISO 8601 interval string.');
|
Chris@14
|
75 }
|
Chris@14
|
76
|
Chris@14
|
77 $dateIntervalFormat = isset($context[self::FORMAT_KEY]) ? $context[self::FORMAT_KEY] : $this->format;
|
Chris@14
|
78
|
Chris@14
|
79 $valuePattern = '/^'.preg_replace('/%([yYmMdDhHiIsSwW])(\w)/', '(?P<$1>\d+)$2', $dateIntervalFormat).'$/';
|
Chris@14
|
80 if (!preg_match($valuePattern, $data)) {
|
Chris@14
|
81 throw new UnexpectedValueException(sprintf('Value "%s" contains intervals not accepted by format "%s".', $data, $dateIntervalFormat));
|
Chris@14
|
82 }
|
Chris@14
|
83
|
Chris@14
|
84 try {
|
Chris@14
|
85 return new \DateInterval($data);
|
Chris@14
|
86 } catch (\Exception $e) {
|
Chris@14
|
87 throw new UnexpectedValueException($e->getMessage(), $e->getCode(), $e);
|
Chris@14
|
88 }
|
Chris@14
|
89 }
|
Chris@14
|
90
|
Chris@14
|
91 /**
|
Chris@14
|
92 * {@inheritdoc}
|
Chris@14
|
93 */
|
Chris@14
|
94 public function supportsDenormalization($data, $type, $format = null)
|
Chris@14
|
95 {
|
Chris@14
|
96 return \DateInterval::class === $type;
|
Chris@14
|
97 }
|
Chris@14
|
98
|
Chris@14
|
99 private function isISO8601($string)
|
Chris@14
|
100 {
|
Chris@14
|
101 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);
|
Chris@14
|
102 }
|
Chris@14
|
103 }
|