Skip to content

Commit 2f0fd37

Browse files
committed
upgrade to DBAL 4
1 parent 6709883 commit 2f0fd37

File tree

5 files changed

+56
-75
lines changed

5 files changed

+56
-75
lines changed

api/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,4 @@
161161
}
162162
}
163163
}
164-
}
164+
}

api/composer.lock

Lines changed: 14 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/src/Types/Doctrine/UTCDateTimeType.php

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
namespace App\Types\Doctrine;
44

5+
use DateTime;
56
use Doctrine\DBAL\Platforms\AbstractPlatform;
67
use Doctrine\DBAL\Types\ConversionException;
78
use Doctrine\DBAL\Types\DateTimeImmutableType;
89
use Doctrine\DBAL\Types\DateTimeType;
10+
use Doctrine\DBAL\Types\Exception\InvalidFormat;
11+
use Doctrine\DBAL\Types\Exception\InvalidType;
912

1013
/**
1114
* Replacement for Doctrine's DateTime Type.
@@ -31,7 +34,7 @@ class UTCDateTimeType extends DateTimeType {
3134
*
3235
* @template T
3336
*/
34-
public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string {
37+
public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform): ?string {
3538
if (null === $value) {
3639
return null;
3740
}
@@ -46,7 +49,11 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform): ?str
4649
return parent::convertToDatabaseValue($value, $platform);
4750
}
4851

49-
throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', 'DateTime']);
52+
throw InvalidType::new(
53+
$value,
54+
static::class,
55+
['null', \DateTime::class],
56+
);
5057
}
5158

5259
/**
@@ -60,22 +67,27 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform): ?str
6067
*
6168
* @template T
6269
*/
63-
public function convertToPHPValue($value, AbstractPlatform $platform): ?\DateTimeInterface {
64-
if (null === $value || $value instanceof \DateTimeInterface) {
70+
public function convertToPHPValue(mixed $value, AbstractPlatform $platform): ?\DateTime {
71+
if (null === $value || $value instanceof \DateTime) {
6572
return $value;
6673
}
6774

68-
$val = \DateTime::createFromFormat($platform->getDateTimeFormatString(), $value, self::getUtc());
75+
$dateTime = \DateTime::createFromFormat($platform->getDateTimeFormatString(), $value, self::getUtc());
6976

70-
if (!$val) {
71-
throw ConversionException::conversionFailedFormat(
77+
if (false !== $dateTime) {
78+
return $dateTime;
79+
}
80+
81+
try {
82+
return new \DateTime($value);
83+
} catch (\Exception $e) {
84+
throw InvalidFormat::new(
7285
$value,
73-
$this->getName(),
74-
$platform->getDateTimeFormatString()
86+
static::class,
87+
$platform->getDateTimeFormatString(),
88+
$e,
7589
);
7690
}
77-
78-
return $val;
7991
}
8092

8193
private static function getUtc(): \DateTimeZone {

api/src/Types/Doctrine/UTCDateType.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Doctrine\DBAL\Types\ConversionException;
77
use Doctrine\DBAL\Types\DateImmutableType;
88
use Doctrine\DBAL\Types\DateType;
9+
use Doctrine\DBAL\Types\Exception\InvalidFormat;
910

1011
class UTCDateType extends DateType {
1112
private static ?\DateTimeZone $utc = null;
@@ -19,7 +20,7 @@ class UTCDateType extends DateType {
1920
*
2021
* @template T
2122
*/
22-
public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string {
23+
public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform): ?string {
2324
if ($value instanceof \DateTime || $value instanceof \DateTimeImmutable) {
2425
$value = $value->setTimezone(self::getUtc());
2526
}
@@ -42,21 +43,21 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform): ?str
4243
*
4344
* @template T
4445
*/
45-
public function convertToPHPValue($value, AbstractPlatform $platform): ?\DateTimeInterface {
46-
if (null === $value || $value instanceof \DateTimeInterface) {
46+
public function convertToPHPValue(mixed $value, AbstractPlatform $platform): ?\DateTime {
47+
if (null === $value || $value instanceof \DateTime) {
4748
return $value;
4849
}
4950

50-
$val = \DateTime::createFromFormat('!'.$platform->getDateFormatString(), $value, self::getUtc());
51-
if (!$val) {
52-
throw ConversionException::conversionFailedFormat(
53-
$value,
54-
$this->getName(),
55-
$platform->getDateFormatString()
56-
);
51+
$dateTime = \DateTime::createFromFormat('!'.$platform->getDateFormatString(), $value, self::getUtc());
52+
if (false !== $dateTime) {
53+
return $dateTime;
5754
}
5855

59-
return $val;
56+
throw InvalidFormat::new(
57+
$value,
58+
static::class,
59+
$platform->getDateFormatString(),
60+
);
6061
}
6162

6263
private static function getUtc(): \DateTimeZone {

api/tests/Types/Doctrine/BaseDateTypeTestCase.php

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
/**
4-
* copied 1:1 from https://github.com/doctrine/dbal/blob/3.2.x/tests/Types/BaseDateTypeTestCase.php.
4+
* copied 1:1 from https://github.com/doctrine/dbal/blob/4.2.x/tests/Types/BaseDateTypeTestCase.php.
55
*/
66

77
namespace App\Tests\Types\Doctrine;
@@ -14,20 +14,15 @@
1414
use PHPUnit\Framework\TestCase;
1515

1616
abstract class BaseDateTypeTestCase extends TestCase {
17-
/** @var AbstractPlatform&MockObject */
18-
protected $platform;
17+
protected AbstractPlatform&MockObject $platform;
18+
protected Type $type;
1919

20-
/** @var Type */
21-
protected $type;
22-
23-
/** @var string */
24-
private $currentTimezone;
20+
/** @var non-empty-string */
21+
private string $currentTimezone;
2522

2623
protected function setUp(): void {
2724
$this->platform = $this->getMockForAbstractClass(AbstractPlatform::class);
2825
$this->currentTimezone = \date_default_timezone_get();
29-
30-
self::assertInstanceOf(Type::class, $this->type);
3126
}
3227

3328
protected function tearDown(): void {
@@ -55,29 +50,7 @@ public function testConvertDateTimeToPHPValue(): void {
5550
self::assertSame($date, $this->type->convertToPHPValue($date, $this->platform));
5651
}
5752

58-
/**
59-
* Note that while \@see \DateTimeImmutable is supposed to be handled
60-
* by @see \Doctrine\DBAL\Types\DateTimeImmutableType, previous DBAL versions handled it just fine.
61-
* This test is just in place to prevent further regressions, even if the type is being misused.
62-
*/
63-
public function testConvertDateTimeImmutableToPHPValue(): void {
64-
$date = new \DateTimeImmutable('now');
65-
66-
self::assertSame($date, $this->type->convertToPHPValue($date, $this->platform));
67-
}
68-
69-
/**
70-
* Note that while \@see \DateTimeImmutable is supposed to be handled
71-
* by @see \Doctrine\DBAL\Types\DateTimeImmutableType, previous DBAL versions handled it just fine.
72-
* This test is just in place to prevent further regressions, even if the type is being misused.
73-
*/
74-
public function testDateTimeImmutableConvertsToDatabaseValue(): void {
75-
self::assertIsString($this->type->convertToDatabaseValue(new \DateTimeImmutable(), $this->platform));
76-
}
77-
78-
/**
79-
* @return mixed[][]
80-
*/
53+
/** @return mixed[][] */
8154
public static function invalidPHPValuesProvider(): iterable {
8255
return [
8356
[0],

0 commit comments

Comments
 (0)