-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use their own classes for UTC correct date types
- Loading branch information
Showing
3 changed files
with
149 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
|
||
namespace App\Doctrine\Types; | ||
|
||
use DateTime; | ||
use DateTimeZone; | ||
use Doctrine\DBAL\Platforms\AbstractPlatform; | ||
use Doctrine\DBAL\Types\DateImmutableType; | ||
use Doctrine\DBAL\Types\DateType; | ||
use Doctrine\DBAL\Types\Exception\InvalidFormat; | ||
|
||
class UTCDateImmutableType extends DateImmutableType | ||
{ | ||
private static ?DateTimeZone $utc_timezone = null; | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @param T $value | ||
* | ||
* @return (T is null ? null : string) | ||
* | ||
* @template T | ||
*/ | ||
public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string | ||
{ | ||
if (!self::$utc_timezone instanceof \DateTimeZone) { | ||
self::$utc_timezone = new DateTimeZone('UTC'); | ||
} | ||
|
||
if ($value instanceof \DateTimeImmutable) { | ||
$value->setTimezone(self::$utc_timezone); | ||
} | ||
|
||
return parent::convertToDatabaseValue($value, $platform); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
* | ||
* @param T $value | ||
* | ||
* @template T | ||
*/ | ||
public function convertToPHPValue($value, AbstractPlatform $platform): ?\DateTimeImmutable | ||
{ | ||
if (!self::$utc_timezone instanceof \DateTimeZone) { | ||
self::$utc_timezone = new DateTimeZone('UTC'); | ||
} | ||
|
||
if (null === $value || $value instanceof \DateTimeImmutable) { | ||
return $value; | ||
} | ||
|
||
$converted = \DateTimeImmutable::createFromFormat( | ||
'!' . $platform->getDateFormatString(), | ||
$value, | ||
self::$utc_timezone | ||
); | ||
|
||
if (!$converted) { | ||
throw InvalidFormat::new( | ||
$value, | ||
static::class, | ||
$platform->getDateFormatString(), | ||
); | ||
} | ||
|
||
return $converted; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
|
||
namespace App\Doctrine\Types; | ||
|
||
use DateTime; | ||
use DateTimeZone; | ||
use Doctrine\DBAL\Platforms\AbstractPlatform; | ||
use Doctrine\DBAL\Types\DateType; | ||
use Doctrine\DBAL\Types\Exception\InvalidFormat; | ||
|
||
class UTCDateType extends DateType | ||
{ | ||
private static ?DateTimeZone $utc_timezone = null; | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @param T $value | ||
* | ||
* @return (T is null ? null : string) | ||
* | ||
* @template T | ||
*/ | ||
public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string | ||
{ | ||
if (!self::$utc_timezone instanceof \DateTimeZone) { | ||
self::$utc_timezone = new DateTimeZone('UTC'); | ||
} | ||
|
||
if ($value instanceof DateTime) { | ||
$value->setTimezone(self::$utc_timezone); | ||
} | ||
|
||
return parent::convertToDatabaseValue($value, $platform); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
* | ||
* @param T $value | ||
* | ||
* @template T | ||
*/ | ||
public function convertToPHPValue($value, AbstractPlatform $platform): ?DateTime | ||
{ | ||
if (!self::$utc_timezone instanceof \DateTimeZone) { | ||
self::$utc_timezone = new DateTimeZone('UTC'); | ||
} | ||
|
||
if (null === $value || $value instanceof DateTime) { | ||
return $value; | ||
} | ||
|
||
$converted = DateTime::createFromFormat( | ||
'!' . $platform->getDateFormatString(), | ||
$value, | ||
self::$utc_timezone | ||
); | ||
|
||
if (!$converted) { | ||
throw InvalidFormat::new( | ||
$value, | ||
static::class, | ||
$platform->getDateFormatString(), | ||
); | ||
} | ||
|
||
return $converted; | ||
} | ||
} |