Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\Serializer\Normalizer;
|
Chris@0
|
13
|
Chris@0
|
14 use Symfony\Component\Serializer\Exception\InvalidArgumentException;
|
Chris@14
|
15 use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@0
|
18 * Normalizes an object implementing the {@see \DateTimeInterface} to a date string.
|
Chris@0
|
19 * Denormalizes a date string to an instance of {@see \DateTime} or {@see \DateTimeImmutable}.
|
Chris@0
|
20 *
|
Chris@0
|
21 * @author Kévin Dunglas <dunglas@gmail.com>
|
Chris@0
|
22 */
|
Chris@0
|
23 class DateTimeNormalizer implements NormalizerInterface, DenormalizerInterface
|
Chris@0
|
24 {
|
Chris@0
|
25 const FORMAT_KEY = 'datetime_format';
|
Chris@14
|
26 const TIMEZONE_KEY = 'datetime_timezone';
|
Chris@14
|
27
|
Chris@14
|
28 private $format;
|
Chris@14
|
29 private $timezone;
|
Chris@14
|
30
|
Chris@14
|
31 private static $supportedTypes = array(
|
Chris@14
|
32 \DateTimeInterface::class => true,
|
Chris@14
|
33 \DateTimeImmutable::class => true,
|
Chris@14
|
34 \DateTime::class => true,
|
Chris@14
|
35 );
|
Chris@0
|
36
|
Chris@0
|
37 /**
|
Chris@14
|
38 * @param string $format
|
Chris@14
|
39 * @param \DateTimeZone|null $timezone
|
Chris@0
|
40 */
|
Chris@14
|
41 public function __construct($format = \DateTime::RFC3339, \DateTimeZone $timezone = null)
|
Chris@0
|
42 {
|
Chris@0
|
43 $this->format = $format;
|
Chris@14
|
44 $this->timezone = $timezone;
|
Chris@0
|
45 }
|
Chris@0
|
46
|
Chris@0
|
47 /**
|
Chris@0
|
48 * {@inheritdoc}
|
Chris@0
|
49 *
|
Chris@0
|
50 * @throws InvalidArgumentException
|
Chris@0
|
51 */
|
Chris@0
|
52 public function normalize($object, $format = null, array $context = array())
|
Chris@0
|
53 {
|
Chris@0
|
54 if (!$object instanceof \DateTimeInterface) {
|
Chris@0
|
55 throw new InvalidArgumentException('The object must implement the "\DateTimeInterface".');
|
Chris@0
|
56 }
|
Chris@0
|
57
|
Chris@0
|
58 $format = isset($context[self::FORMAT_KEY]) ? $context[self::FORMAT_KEY] : $this->format;
|
Chris@14
|
59 $timezone = $this->getTimezone($context);
|
Chris@14
|
60
|
Chris@14
|
61 if (null !== $timezone) {
|
Chris@14
|
62 $object = (new \DateTimeImmutable('@'.$object->getTimestamp()))->setTimezone($timezone);
|
Chris@14
|
63 }
|
Chris@0
|
64
|
Chris@0
|
65 return $object->format($format);
|
Chris@0
|
66 }
|
Chris@0
|
67
|
Chris@0
|
68 /**
|
Chris@0
|
69 * {@inheritdoc}
|
Chris@0
|
70 */
|
Chris@0
|
71 public function supportsNormalization($data, $format = null)
|
Chris@0
|
72 {
|
Chris@0
|
73 return $data instanceof \DateTimeInterface;
|
Chris@0
|
74 }
|
Chris@0
|
75
|
Chris@0
|
76 /**
|
Chris@0
|
77 * {@inheritdoc}
|
Chris@0
|
78 *
|
Chris@14
|
79 * @throws NotNormalizableValueException
|
Chris@0
|
80 */
|
Chris@0
|
81 public function denormalize($data, $class, $format = null, array $context = array())
|
Chris@0
|
82 {
|
Chris@0
|
83 $dateTimeFormat = isset($context[self::FORMAT_KEY]) ? $context[self::FORMAT_KEY] : null;
|
Chris@14
|
84 $timezone = $this->getTimezone($context);
|
Chris@14
|
85
|
Chris@14
|
86 if ('' === $data || null === $data) {
|
Chris@14
|
87 throw new NotNormalizableValueException('The data is either an empty string or null, you should pass a string that can be parsed with the passed format or a valid DateTime string.');
|
Chris@14
|
88 }
|
Chris@0
|
89
|
Chris@0
|
90 if (null !== $dateTimeFormat) {
|
Chris@14
|
91 if (null === $timezone && PHP_VERSION_ID < 70000) {
|
Chris@14
|
92 // https://bugs.php.net/bug.php?id=68669
|
Chris@14
|
93 $object = \DateTime::class === $class ? \DateTime::createFromFormat($dateTimeFormat, $data) : \DateTimeImmutable::createFromFormat($dateTimeFormat, $data);
|
Chris@14
|
94 } else {
|
Chris@14
|
95 $object = \DateTime::class === $class ? \DateTime::createFromFormat($dateTimeFormat, $data, $timezone) : \DateTimeImmutable::createFromFormat($dateTimeFormat, $data, $timezone);
|
Chris@14
|
96 }
|
Chris@0
|
97
|
Chris@0
|
98 if (false !== $object) {
|
Chris@0
|
99 return $object;
|
Chris@0
|
100 }
|
Chris@0
|
101
|
Chris@0
|
102 $dateTimeErrors = \DateTime::class === $class ? \DateTime::getLastErrors() : \DateTimeImmutable::getLastErrors();
|
Chris@0
|
103
|
Chris@14
|
104 throw new NotNormalizableValueException(sprintf(
|
Chris@0
|
105 'Parsing datetime string "%s" using format "%s" resulted in %d errors:'."\n".'%s',
|
Chris@0
|
106 $data,
|
Chris@0
|
107 $dateTimeFormat,
|
Chris@0
|
108 $dateTimeErrors['error_count'],
|
Chris@0
|
109 implode("\n", $this->formatDateTimeErrors($dateTimeErrors['errors']))
|
Chris@0
|
110 ));
|
Chris@0
|
111 }
|
Chris@0
|
112
|
Chris@0
|
113 try {
|
Chris@14
|
114 return \DateTime::class === $class ? new \DateTime($data, $timezone) : new \DateTimeImmutable($data, $timezone);
|
Chris@0
|
115 } catch (\Exception $e) {
|
Chris@14
|
116 throw new NotNormalizableValueException($e->getMessage(), $e->getCode(), $e);
|
Chris@0
|
117 }
|
Chris@0
|
118 }
|
Chris@0
|
119
|
Chris@0
|
120 /**
|
Chris@0
|
121 * {@inheritdoc}
|
Chris@0
|
122 */
|
Chris@0
|
123 public function supportsDenormalization($data, $type, $format = null)
|
Chris@0
|
124 {
|
Chris@14
|
125 return isset(self::$supportedTypes[$type]);
|
Chris@0
|
126 }
|
Chris@0
|
127
|
Chris@0
|
128 /**
|
Chris@0
|
129 * Formats datetime errors.
|
Chris@0
|
130 *
|
Chris@0
|
131 * @return string[]
|
Chris@0
|
132 */
|
Chris@0
|
133 private function formatDateTimeErrors(array $errors)
|
Chris@0
|
134 {
|
Chris@0
|
135 $formattedErrors = array();
|
Chris@0
|
136
|
Chris@0
|
137 foreach ($errors as $pos => $message) {
|
Chris@0
|
138 $formattedErrors[] = sprintf('at position %d: %s', $pos, $message);
|
Chris@0
|
139 }
|
Chris@0
|
140
|
Chris@0
|
141 return $formattedErrors;
|
Chris@0
|
142 }
|
Chris@14
|
143
|
Chris@14
|
144 private function getTimezone(array $context)
|
Chris@14
|
145 {
|
Chris@14
|
146 $dateTimeZone = array_key_exists(self::TIMEZONE_KEY, $context) ? $context[self::TIMEZONE_KEY] : $this->timezone;
|
Chris@14
|
147
|
Chris@14
|
148 if (null === $dateTimeZone) {
|
Chris@14
|
149 return null;
|
Chris@14
|
150 }
|
Chris@14
|
151
|
Chris@14
|
152 return $dateTimeZone instanceof \DateTimeZone ? $dateTimeZone : new \DateTimeZone($dateTimeZone);
|
Chris@14
|
153 }
|
Chris@0
|
154 }
|