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