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