feat: handle common database datetime formats (#40)

This commit is contained in:
Brandon Savage 2021-12-17 11:55:17 -05:00 committed by GitHub
parent e5ccbe201b
commit 179ba3df29
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 3 deletions

View File

@ -21,6 +21,9 @@ use function is_string;
final class DateTimeObjectBuilder implements ObjectBuilder
{
public const DATE_MYSQL = 'Y-m-d H:i:s';
public const DATE_PGSQL = 'Y-m-d H:i:s.u';
/** @var class-string<DateTime|DateTimeImmutable> */
private string $className;
@ -73,9 +76,9 @@ final class DateTimeObjectBuilder implements ObjectBuilder
private function tryAllFormats(string $value): ?DateTimeInterface
{
$formats = [
DATE_ATOM, DATE_RFC850, DATE_COOKIE, DATE_RFC822, DATE_RFC1036,
DATE_RFC1123, DATE_RFC2822, DATE_RFC3339, DATE_RFC3339_EXTENDED,
DATE_RFC7231, DATE_RSS, DATE_W3C,
self::DATE_MYSQL, self::DATE_PGSQL, DATE_ATOM, DATE_RFC850, DATE_COOKIE,
DATE_RFC822, DATE_RFC1036, DATE_RFC1123, DATE_RFC2822, DATE_RFC3339,
DATE_RFC3339_EXTENDED, DATE_RFC7231, DATE_RSS, DATE_W3C,
];
foreach ($formats as $format) {

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace CuyZ\Valinor\Tests\Integration\Mapping\Type;
use CuyZ\Valinor\Mapper\MappingError;
use CuyZ\Valinor\Mapper\Object\DateTimeObjectBuilder;
use CuyZ\Valinor\Mapper\Object\Exception\CannotParseToDateTime;
use CuyZ\Valinor\Tests\Integration\IntegrationTest;
use CuyZ\Valinor\Type\Resolver\Exception\UnionTypeDoesNotAllowNull;
@ -28,6 +29,8 @@ final class DateTimeMappingTest extends IntegrationTest
'datetime' => '2012-12-21 13:37:42',
'format' => 'Y-m-d H:i:s',
];
$mysqlDate = '2012-12-21 13:37:42';
$pgsqlDate = '2012-12-21 13:37:42.123456';
try {
$result = $this->mapperBuilder->mapper()->map(AllDateTimeValues::class, [
@ -37,6 +40,9 @@ final class DateTimeMappingTest extends IntegrationTest
'dateTimeFromTimestampWithFormat' => $dateTimeFromTimestampWithFormat,
'dateTimeFromAtomFormat' => $dateTimeFromAtomFormat,
'dateTimeFromArray' => $dateTimeFromArray,
'mysqlDate' => $mysqlDate,
'pgsqlDate' => $pgsqlDate,
]);
} catch (MappingError $error) {
$this->mappingFail($error);
@ -49,6 +55,8 @@ final class DateTimeMappingTest extends IntegrationTest
self::assertEquals(new DateTimeImmutable("@{$dateTimeFromTimestampWithFormat['datetime']}"), $result->dateTimeFromTimestampWithFormat);
self::assertEquals(DateTimeImmutable::createFromFormat(DATE_ATOM, $dateTimeFromAtomFormat), $result->dateTimeFromAtomFormat);
self::assertEquals(DateTimeImmutable::createFromFormat($dateTimeFromArray['format'], $dateTimeFromArray['datetime']), $result->dateTimeFromArray);
self::assertEquals(DateTimeImmutable::createFromFormat(DateTimeObjectBuilder::DATE_MYSQL, $mysqlDate), $result->mysqlDate);
self::assertEquals(DateTimeImmutable::createFromFormat(DateTimeObjectBuilder::DATE_PGSQL, $pgsqlDate), $result->pgsqlDate);
}
public function test_invalid_datetime_throws_exception(): void
@ -128,4 +136,8 @@ final class AllDateTimeValues
public DateTimeInterface $dateTimeFromAtomFormat;
public DateTimeInterface $dateTimeFromArray;
public DateTimeInterface $mysqlDate;
public DateTimeInterface $pgsqlDate;
}