1<?php 2 3namespace Doctrine\DBAL\Types; 4 5use DateTime; 6use DateTimeInterface; 7use Doctrine\DBAL\Platforms\AbstractPlatform; 8 9/** 10 * Type that maps an SQL TIME to a PHP DateTime object. 11 */ 12class TimeType extends Type 13{ 14 /** 15 * {@inheritdoc} 16 */ 17 public function getName() 18 { 19 return Types::TIME_MUTABLE; 20 } 21 22 /** 23 * {@inheritdoc} 24 */ 25 public function getSQLDeclaration(array $column, AbstractPlatform $platform) 26 { 27 return $platform->getTimeTypeDeclarationSQL($column); 28 } 29 30 /** 31 * {@inheritdoc} 32 */ 33 public function convertToDatabaseValue($value, AbstractPlatform $platform) 34 { 35 if ($value === null) { 36 return $value; 37 } 38 39 if ($value instanceof DateTimeInterface) { 40 return $value->format($platform->getTimeFormatString()); 41 } 42 43 throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', 'DateTime']); 44 } 45 46 /** 47 * {@inheritdoc} 48 */ 49 public function convertToPHPValue($value, AbstractPlatform $platform) 50 { 51 if ($value === null || $value instanceof DateTimeInterface) { 52 return $value; 53 } 54 55 $val = DateTime::createFromFormat('!' . $platform->getTimeFormatString(), $value); 56 if (! $val) { 57 throw ConversionException::conversionFailedFormat( 58 $value, 59 $this->getName(), 60 $platform->getTimeFormatString() 61 ); 62 } 63 64 return $val; 65 } 66} 67