Deprecated: Constant E_STRICT is deprecated in /home/normanv/www/annuairepro/vendor/symfony/error-handler/ErrorHandler.php on line 58

Deprecated: Constant E_STRICT is deprecated in /home/normanv/www/annuairepro/vendor/symfony/error-handler/ErrorHandler.php on line 76
Symfony Profiler

vendor/symfony/serializer/Normalizer/DateTimeNormalizer.php line 89

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