Skip to content

Commit 2a2c16d

Browse files
committed
feature symfony#60237 [DoctrineBridge] Add new DayPointType and TimePointType Doctrine type (wkania)
This PR was merged into the 7.4 branch. Discussion ---------- [DoctrineBridge] Add new `DayPointType` and `TimePointType` Doctrine type | Q | A | ------------- | --- | Branch | 7.4 | Bug fix | no | New feature | yes | Deprecations | no | License | MIT Doctrine provides both [date_immutable](https://www.doctrine-project.org/projects/doctrine-dbal/en/4.2/reference/types.html#date-immutable) and [datetime_immutable](https://www.doctrine-project.org/projects/doctrine-dbal/en/4.2/reference/types.html#datetime-immutable) types. Restricting the conversion of DatePoint only to datetime_immutable is problematic. New version: Previous [pull request](symfony#59900). ```yaml doctrine: dbal: types: date_point: Symfony\Bridge\Doctrine\Types\DatePointType day_point: Symfony\Bridge\Doctrine\Types\DayPointType time_point: Symfony\Bridge\Doctrine\Types\TimePointType ``` ```php #[ORM\Column(type: 'date_point')] public DatePoint $createdAt; #[ORM\Column(type: 'day_point')] public DatePoint $birthday; #[ORM\Column(type: 'time_point')] public DatePoint $openAt; ``` Old version: Therefore, I propose renaming the current DatePointType to DateTimePointType, and introducing a new DatePointType. Previous [pull request](symfony#59900). If this pull request is to be merged, it should be included in version 7.3 to avoid any breaking changes in the future. ```yaml doctrine: dbal: types: date_point: Symfony\Bridge\Doctrine\Types\DatePointType datetime_point: Symfony\Bridge\Doctrine\Types\DateTimePointType ``` ```php #[ORM\Column(type: 'date_point')] public DatePoint $birthday; #[ORM\Column(type: 'datetime_point')] public DatePoint $createdAt; Commits ------- 7f383ef [DoctrineBridge] add new DayPointType and TimePointType Doctrine type
2 parents f1f6344 + 7f383ef commit 2a2c16d

File tree

7 files changed

+301
-0
lines changed

7 files changed

+301
-0
lines changed

src/Symfony/Bridge/Doctrine/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* Deprecate `UniqueEntity::getRequiredOptions()` and `UniqueEntity::getDefaultOption()`
88
* Use a single table named `_schema_subscriber_check` in schema listeners to detect same database connections
9+
* Add support for `Symfony\Component\Clock\DatePoint` as `DayPointType` and `TimePointType` Doctrine type
910

1011
7.3
1112
---

src/Symfony/Bridge/Doctrine/DependencyInjection/CompilerPass/RegisterDatePointTypePass.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass;
1313

1414
use Symfony\Bridge\Doctrine\Types\DatePointType;
15+
use Symfony\Bridge\Doctrine\Types\DayPointType;
16+
use Symfony\Bridge\Doctrine\Types\TimePointType;
1517
use Symfony\Component\Clock\DatePoint;
1618
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1719
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -31,6 +33,8 @@ public function process(ContainerBuilder $container): void
3133
$types = $container->getParameter('doctrine.dbal.connection_factory.types');
3234

3335
$types['date_point'] ??= ['class' => DatePointType::class];
36+
$types['day_point'] ??= ['class' => DayPointType::class];
37+
$types['time_point'] ??= ['class' => TimePointType::class];
3438

3539
$container->setParameter('doctrine.dbal.connection_factory.types', $types);
3640
}

src/Symfony/Bridge/Doctrine/Tests/DependencyInjection/CompilerPass/RegisterDatePointTypePassTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass\RegisterDatePointTypePass;
1616
use Symfony\Bridge\Doctrine\Types\DatePointType;
17+
use Symfony\Bridge\Doctrine\Types\DayPointType;
18+
use Symfony\Bridge\Doctrine\Types\TimePointType;
1719
use Symfony\Component\Clock\DatePoint;
1820
use Symfony\Component\DependencyInjection\ContainerBuilder;
1921

@@ -35,6 +37,8 @@ public function testRegistered()
3537
$expected = [
3638
'foo' => 'bar',
3739
'date_point' => ['class' => DatePointType::class],
40+
'day_point' => ['class' => DayPointType::class],
41+
'time_point' => ['class' => TimePointType::class],
3842
];
3943
$this->assertSame($expected, $container->getParameter('doctrine.dbal.connection_factory.types'));
4044
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Tests\Types;
13+
14+
use Doctrine\DBAL\Exception;
15+
use Doctrine\DBAL\Platforms\AbstractPlatform;
16+
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
17+
use Doctrine\DBAL\Types\Type;
18+
use PHPUnit\Framework\TestCase;
19+
use Symfony\Bridge\Doctrine\Types\DayPointType;
20+
use Symfony\Component\Clock\DatePoint;
21+
22+
final class DayPointTypeTest extends TestCase
23+
{
24+
private DayPointType $type;
25+
26+
public static function setUpBeforeClass(): void
27+
{
28+
$name = DayPointType::NAME;
29+
if (Type::hasType($name)) {
30+
Type::overrideType($name, DayPointType::class);
31+
} else {
32+
Type::addType($name, DayPointType::class);
33+
}
34+
}
35+
36+
protected function setUp(): void
37+
{
38+
if (!class_exists(DatePoint::class)) {
39+
self::markTestSkipped('The DatePoint class is not available.');
40+
}
41+
$this->type = Type::getType(DayPointType::NAME);
42+
}
43+
44+
public function testDatePointConvertsToDatabaseValue()
45+
{
46+
$datePoint = DatePoint::createFromFormat('!Y-m-d', '2025-03-03');
47+
48+
$expected = $datePoint->format('Y-m-d');
49+
$actual = $this->type->convertToDatabaseValue($datePoint, new PostgreSQLPlatform());
50+
51+
$this->assertSame($expected, $actual);
52+
}
53+
54+
public function testDatePointConvertsToPHPValue()
55+
{
56+
$datePoint = new DatePoint();
57+
$actual = $this->type->convertToPHPValue($datePoint, self::getSqlitePlatform());
58+
59+
$this->assertSame($datePoint, $actual);
60+
}
61+
62+
public function testNullConvertsToPHPValue()
63+
{
64+
$actual = $this->type->convertToPHPValue(null, self::getSqlitePlatform());
65+
66+
$this->assertNull($actual);
67+
}
68+
69+
public function testDateTimeImmutableConvertsToPHPValue()
70+
{
71+
$format = 'Y-m-d H:i:s.u';
72+
$date = '2025-03-03';
73+
$dateTime = \DateTimeImmutable::createFromFormat('!Y-m-d', $date);
74+
$actual = $this->type->convertToPHPValue($dateTime, self::getSqlitePlatform());
75+
$expected = DatePoint::createFromFormat('!Y-m-d', $date);
76+
77+
$this->assertInstanceOf(DatePoint::class, $actual);
78+
$this->assertSame($expected->format($format), $actual->format($format));
79+
}
80+
81+
public function testDatabaseValueConvertsToPHPValue()
82+
{
83+
$format = 'Y-m-d H:i:s.u';
84+
$date = '2025-03-03';
85+
$actual = $this->type->convertToPHPValue($date, new PostgreSQLPlatform());
86+
$expected = DatePoint::createFromFormat('!Y-m-d', $date);
87+
88+
$this->assertInstanceOf(DatePoint::class, $actual);
89+
$this->assertSame($expected->format($format), $actual->format($format));
90+
}
91+
92+
public function testGetName()
93+
{
94+
$this->assertSame('day_point', $this->type->getName());
95+
}
96+
97+
private static function getSqlitePlatform(): AbstractPlatform
98+
{
99+
if (interface_exists(Exception::class)) {
100+
// DBAL 4+
101+
return new \Doctrine\DBAL\Platforms\SQLitePlatform();
102+
}
103+
104+
return new \Doctrine\DBAL\Platforms\SqlitePlatform();
105+
}
106+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Tests\Types;
13+
14+
use Doctrine\DBAL\Exception;
15+
use Doctrine\DBAL\Platforms\AbstractPlatform;
16+
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
17+
use Doctrine\DBAL\Types\Type;
18+
use PHPUnit\Framework\TestCase;
19+
use Symfony\Bridge\Doctrine\Types\TimePointType;
20+
use Symfony\Component\Clock\DatePoint;
21+
22+
final class TimePointTypeTest extends TestCase
23+
{
24+
private TimePointType $type;
25+
26+
public static function setUpBeforeClass(): void
27+
{
28+
$name = TimePointType::NAME;
29+
if (Type::hasType($name)) {
30+
Type::overrideType($name, TimePointType::class);
31+
} else {
32+
Type::addType($name, TimePointType::class);
33+
}
34+
}
35+
36+
protected function setUp(): void
37+
{
38+
if (!class_exists(DatePoint::class)) {
39+
self::markTestSkipped('The DatePoint class is not available.');
40+
}
41+
$this->type = Type::getType(TimePointType::NAME);
42+
}
43+
44+
public function testDatePointConvertsToDatabaseValue()
45+
{
46+
$datePoint = DatePoint::createFromFormat('!H:i:s', '05:10:15');
47+
48+
$expected = $datePoint->format('H:i:s');
49+
$actual = $this->type->convertToDatabaseValue($datePoint, new PostgreSQLPlatform());
50+
51+
$this->assertSame($expected, $actual);
52+
}
53+
54+
public function testDatePointConvertsToPHPValue()
55+
{
56+
$datePoint = new DatePoint();
57+
$actual = $this->type->convertToPHPValue($datePoint, self::getSqlitePlatform());
58+
59+
$this->assertSame($datePoint, $actual);
60+
}
61+
62+
public function testNullConvertsToPHPValue()
63+
{
64+
$actual = $this->type->convertToPHPValue(null, self::getSqlitePlatform());
65+
66+
$this->assertNull($actual);
67+
}
68+
69+
public function testDateTimeImmutableConvertsToPHPValue()
70+
{
71+
$format = 'Y-m-d H:i:s.u';
72+
$time = '05:10:15';
73+
$dateTime = \DateTimeImmutable::createFromFormat('!H:i:s', $time);
74+
$actual = $this->type->convertToPHPValue($dateTime, self::getSqlitePlatform());
75+
$expected = DatePoint::createFromFormat('!H:i:s', $time);
76+
77+
$this->assertInstanceOf(DatePoint::class, $actual);
78+
$this->assertSame($expected->format($format), $actual->format($format));
79+
}
80+
81+
public function testDatabaseValueConvertsToPHPValue()
82+
{
83+
$format = 'Y-m-d H:i:s.u';
84+
$time = '05:10:15';
85+
$actual = $this->type->convertToPHPValue($time, new PostgreSQLPlatform());
86+
$expected = DatePoint::createFromFormat('!H:i:s', $time);
87+
88+
$this->assertInstanceOf(DatePoint::class, $actual);
89+
$this->assertSame($expected->format($format), $actual->format($format));
90+
}
91+
92+
public function testGetName()
93+
{
94+
$this->assertSame('time_point', $this->type->getName());
95+
}
96+
97+
private static function getSqlitePlatform(): AbstractPlatform
98+
{
99+
if (interface_exists(Exception::class)) {
100+
// DBAL 4+
101+
return new \Doctrine\DBAL\Platforms\SQLitePlatform();
102+
}
103+
104+
return new \Doctrine\DBAL\Platforms\SqlitePlatform();
105+
}
106+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Types;
13+
14+
use Doctrine\DBAL\Platforms\AbstractPlatform;
15+
use Doctrine\DBAL\Types\DateImmutableType;
16+
use Symfony\Component\Clock\DatePoint;
17+
18+
final class DayPointType extends DateImmutableType
19+
{
20+
public const NAME = 'day_point';
21+
22+
/**
23+
* @return ($value is null ? null : DatePoint)
24+
*/
25+
public function convertToPHPValue(mixed $value, AbstractPlatform $platform): ?DatePoint
26+
{
27+
if (null === $value || $value instanceof DatePoint) {
28+
return $value;
29+
}
30+
31+
$value = parent::convertToPHPValue($value, $platform);
32+
33+
return DatePoint::createFromInterface($value);
34+
}
35+
36+
public function getName(): string
37+
{
38+
return self::NAME;
39+
}
40+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Types;
13+
14+
use Doctrine\DBAL\Platforms\AbstractPlatform;
15+
use Doctrine\DBAL\Types\TimeImmutableType;
16+
use Symfony\Component\Clock\DatePoint;
17+
18+
final class TimePointType extends TimeImmutableType
19+
{
20+
public const NAME = 'time_point';
21+
22+
/**
23+
* @return ($value is null ? null : DatePoint)
24+
*/
25+
public function convertToPHPValue(mixed $value, AbstractPlatform $platform): ?DatePoint
26+
{
27+
if (null === $value || $value instanceof DatePoint) {
28+
return $value;
29+
}
30+
31+
$value = parent::convertToPHPValue($value, $platform);
32+
33+
return DatePoint::createFromInterface($value);
34+
}
35+
36+
public function getName(): string
37+
{
38+
return self::NAME;
39+
}
40+
}

0 commit comments

Comments
 (0)