123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Component\Serializer\Normalizer;
- use Symfony\Component\Serializer\Exception\InvalidArgumentException;
- use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
- /**
- * Normalizes an object implementing the {@see \DateTimeInterface} to a date string.
- * Denormalizes a date string to an instance of {@see \DateTime} or {@see \DateTimeImmutable}.
- *
- * @author Kévin Dunglas <dunglas@gmail.com>
- */
- class DateTimeNormalizer implements NormalizerInterface, DenormalizerInterface, CacheableSupportsMethodInterface
- {
- public const FORMAT_KEY = 'datetime_format';
- public const TIMEZONE_KEY = 'datetime_timezone';
- private $defaultContext = [
- self::FORMAT_KEY => \DateTime::RFC3339,
- self::TIMEZONE_KEY => null,
- ];
- private const SUPPORTED_TYPES = [
- \DateTimeInterface::class => true,
- \DateTimeImmutable::class => true,
- \DateTime::class => true,
- ];
- public function __construct(array $defaultContext = [])
- {
- $this->defaultContext = array_merge($this->defaultContext, $defaultContext);
- }
- /**
- * {@inheritdoc}
- *
- * @throws InvalidArgumentException
- *
- * @return string
- */
- public function normalize($object, string $format = null, array $context = [])
- {
- if (!$object instanceof \DateTimeInterface) {
- throw new InvalidArgumentException('The object must implement the "\DateTimeInterface".');
- }
- $dateTimeFormat = $context[self::FORMAT_KEY] ?? $this->defaultContext[self::FORMAT_KEY];
- $timezone = $this->getTimezone($context);
- if (null !== $timezone) {
- $object = clone $object;
- $object = $object->setTimezone($timezone);
- }
- return $object->format($dateTimeFormat);
- }
- /**
- * {@inheritdoc}
- */
- public function supportsNormalization($data, string $format = null)
- {
- return $data instanceof \DateTimeInterface;
- }
- /**
- * {@inheritdoc}
- *
- * @throws NotNormalizableValueException
- *
- * @return \DateTimeInterface
- */
- public function denormalize($data, string $type, string $format = null, array $context = [])
- {
- $dateTimeFormat = $context[self::FORMAT_KEY] ?? null;
- $timezone = $this->getTimezone($context);
- if ('' === $data || null === $data) {
- 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.');
- }
- if (null !== $dateTimeFormat) {
- $object = \DateTime::class === $type ? \DateTime::createFromFormat($dateTimeFormat, $data, $timezone) : \DateTimeImmutable::createFromFormat($dateTimeFormat, $data, $timezone);
- if (false !== $object) {
- return $object;
- }
- $dateTimeErrors = \DateTime::class === $type ? \DateTime::getLastErrors() : \DateTimeImmutable::getLastErrors();
- throw new NotNormalizableValueException(sprintf('Parsing datetime string "%s" using format "%s" resulted in %d errors: ', $data, $dateTimeFormat, $dateTimeErrors['error_count'])."\n".implode("\n", $this->formatDateTimeErrors($dateTimeErrors['errors'])));
- }
- try {
- return \DateTime::class === $type ? new \DateTime($data, $timezone) : new \DateTimeImmutable($data, $timezone);
- } catch (\Exception $e) {
- throw new NotNormalizableValueException($e->getMessage(), $e->getCode(), $e);
- }
- }
- /**
- * {@inheritdoc}
- */
- public function supportsDenormalization($data, string $type, string $format = null)
- {
- return isset(self::SUPPORTED_TYPES[$type]);
- }
- /**
- * {@inheritdoc}
- */
- public function hasCacheableSupportsMethod(): bool
- {
- return __CLASS__ === static::class;
- }
- /**
- * Formats datetime errors.
- *
- * @return string[]
- */
- private function formatDateTimeErrors(array $errors): array
- {
- $formattedErrors = [];
- foreach ($errors as $pos => $message) {
- $formattedErrors[] = sprintf('at position %d: %s', $pos, $message);
- }
- return $formattedErrors;
- }
- private function getTimezone(array $context): ?\DateTimeZone
- {
- $dateTimeZone = $context[self::TIMEZONE_KEY] ?? $this->defaultContext[self::TIMEZONE_KEY];
- if (null === $dateTimeZone) {
- return null;
- }
- return $dateTimeZone instanceof \DateTimeZone ? $dateTimeZone : new \DateTimeZone($dateTimeZone);
- }
- }
|