Mercurial > hg > isophonics-drupal-site
comparison vendor/symfony/serializer/Normalizer/DateTimeNormalizer.php @ 17:129ea1e6d783
Update, including to Drupal core 8.6.10
author | Chris Cannam |
---|---|
date | Thu, 28 Feb 2019 13:21:36 +0000 |
parents | 1fec387a4317 |
children | af1871eacc83 |
comparison
equal
deleted
inserted
replaced
16:c2387f117808 | 17:129ea1e6d783 |
---|---|
26 const TIMEZONE_KEY = 'datetime_timezone'; | 26 const TIMEZONE_KEY = 'datetime_timezone'; |
27 | 27 |
28 private $format; | 28 private $format; |
29 private $timezone; | 29 private $timezone; |
30 | 30 |
31 private static $supportedTypes = array( | 31 private static $supportedTypes = [ |
32 \DateTimeInterface::class => true, | 32 \DateTimeInterface::class => true, |
33 \DateTimeImmutable::class => true, | 33 \DateTimeImmutable::class => true, |
34 \DateTime::class => true, | 34 \DateTime::class => true, |
35 ); | 35 ]; |
36 | 36 |
37 /** | 37 /** |
38 * @param string $format | 38 * @param string $format |
39 * @param \DateTimeZone|null $timezone | 39 * @param \DateTimeZone|null $timezone |
40 */ | 40 */ |
47 /** | 47 /** |
48 * {@inheritdoc} | 48 * {@inheritdoc} |
49 * | 49 * |
50 * @throws InvalidArgumentException | 50 * @throws InvalidArgumentException |
51 */ | 51 */ |
52 public function normalize($object, $format = null, array $context = array()) | 52 public function normalize($object, $format = null, array $context = []) |
53 { | 53 { |
54 if (!$object instanceof \DateTimeInterface) { | 54 if (!$object instanceof \DateTimeInterface) { |
55 throw new InvalidArgumentException('The object must implement the "\DateTimeInterface".'); | 55 throw new InvalidArgumentException('The object must implement the "\DateTimeInterface".'); |
56 } | 56 } |
57 | 57 |
58 $format = isset($context[self::FORMAT_KEY]) ? $context[self::FORMAT_KEY] : $this->format; | 58 $format = isset($context[self::FORMAT_KEY]) ? $context[self::FORMAT_KEY] : $this->format; |
59 $timezone = $this->getTimezone($context); | 59 $timezone = $this->getTimezone($context); |
60 | 60 |
61 if (null !== $timezone) { | 61 if (null !== $timezone) { |
62 $object = (new \DateTimeImmutable('@'.$object->getTimestamp()))->setTimezone($timezone); | 62 $object = clone $object; |
63 $object = $object->setTimezone($timezone); | |
63 } | 64 } |
64 | 65 |
65 return $object->format($format); | 66 return $object->format($format); |
66 } | 67 } |
67 | 68 |
76 /** | 77 /** |
77 * {@inheritdoc} | 78 * {@inheritdoc} |
78 * | 79 * |
79 * @throws NotNormalizableValueException | 80 * @throws NotNormalizableValueException |
80 */ | 81 */ |
81 public function denormalize($data, $class, $format = null, array $context = array()) | 82 public function denormalize($data, $class, $format = null, array $context = []) |
82 { | 83 { |
83 $dateTimeFormat = isset($context[self::FORMAT_KEY]) ? $context[self::FORMAT_KEY] : null; | 84 $dateTimeFormat = isset($context[self::FORMAT_KEY]) ? $context[self::FORMAT_KEY] : null; |
84 $timezone = $this->getTimezone($context); | 85 $timezone = $this->getTimezone($context); |
85 | 86 |
86 if ('' === $data || null === $data) { | 87 if ('' === $data || null === $data) { |
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.'); | 88 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.'); |
88 } | 89 } |
89 | 90 |
90 if (null !== $dateTimeFormat) { | 91 if (null !== $dateTimeFormat) { |
91 if (null === $timezone && PHP_VERSION_ID < 70000) { | 92 if (null === $timezone && \PHP_VERSION_ID < 70000) { |
92 // https://bugs.php.net/bug.php?id=68669 | 93 // https://bugs.php.net/bug.php?id=68669 |
93 $object = \DateTime::class === $class ? \DateTime::createFromFormat($dateTimeFormat, $data) : \DateTimeImmutable::createFromFormat($dateTimeFormat, $data); | 94 $object = \DateTime::class === $class ? \DateTime::createFromFormat($dateTimeFormat, $data) : \DateTimeImmutable::createFromFormat($dateTimeFormat, $data); |
94 } else { | 95 } else { |
95 $object = \DateTime::class === $class ? \DateTime::createFromFormat($dateTimeFormat, $data, $timezone) : \DateTimeImmutable::createFromFormat($dateTimeFormat, $data, $timezone); | 96 $object = \DateTime::class === $class ? \DateTime::createFromFormat($dateTimeFormat, $data, $timezone) : \DateTimeImmutable::createFromFormat($dateTimeFormat, $data, $timezone); |
96 } | 97 } |
130 * | 131 * |
131 * @return string[] | 132 * @return string[] |
132 */ | 133 */ |
133 private function formatDateTimeErrors(array $errors) | 134 private function formatDateTimeErrors(array $errors) |
134 { | 135 { |
135 $formattedErrors = array(); | 136 $formattedErrors = []; |
136 | 137 |
137 foreach ($errors as $pos => $message) { | 138 foreach ($errors as $pos => $message) { |
138 $formattedErrors[] = sprintf('at position %d: %s', $pos, $message); | 139 $formattedErrors[] = sprintf('at position %d: %s', $pos, $message); |
139 } | 140 } |
140 | 141 |