vendor/doctrine/dbal/src/Types/DateTimeImmutableType.php line 27

  1. <?php
  2. namespace Doctrine\DBAL\Types;
  3. use DateTimeImmutable;
  4. use Doctrine\DBAL\Platforms\AbstractPlatform;
  5. use Doctrine\Deprecations\Deprecation;
  6. use function date_create_immutable;
  7. /**
  8.  * Immutable type of {@see DateTimeType}.
  9.  */
  10. class DateTimeImmutableType extends DateTimeType
  11. {
  12.     /**
  13.      * {@inheritdoc}
  14.      */
  15.     public function getName()
  16.     {
  17.         return Types::DATETIME_IMMUTABLE;
  18.     }
  19.     /**
  20.      * {@inheritdoc}
  21.      */
  22.     public function convertToDatabaseValue($valueAbstractPlatform $platform)
  23.     {
  24.         if ($value === null) {
  25.             return $value;
  26.         }
  27.         if ($value instanceof DateTimeImmutable) {
  28.             return $value->format($platform->getDateTimeFormatString());
  29.         }
  30.         throw ConversionException::conversionFailedInvalidType(
  31.             $value,
  32.             $this->getName(),
  33.             ['null'DateTimeImmutable::class],
  34.         );
  35.     }
  36.     /**
  37.      * {@inheritdoc}
  38.      */
  39.     public function convertToPHPValue($valueAbstractPlatform $platform)
  40.     {
  41.         if ($value === null || $value instanceof DateTimeImmutable) {
  42.             return $value;
  43.         }
  44.         $dateTime DateTimeImmutable::createFromFormat($platform->getDateTimeFormatString(), $value);
  45.         if ($dateTime === false) {
  46.             $dateTime date_create_immutable($value);
  47.         }
  48.         if ($dateTime === false) {
  49.             throw ConversionException::conversionFailedFormat(
  50.                 $value,
  51.                 $this->getName(),
  52.                 $platform->getDateTimeFormatString(),
  53.             );
  54.         }
  55.         return $dateTime;
  56.     }
  57.     /**
  58.      * {@inheritdoc}
  59.      *
  60.      * @deprecated
  61.      */
  62.     public function requiresSQLCommentHint(AbstractPlatform $platform)
  63.     {
  64.         Deprecation::triggerIfCalledFromOutside(
  65.             'doctrine/dbal',
  66.             'https://github.com/doctrine/dbal/pull/5509',
  67.             '%s is deprecated.',
  68.             __METHOD__,
  69.         );
  70.         return true;
  71.     }
  72. }