Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c61752e

Browse filesBrowse files
garakfabpot
authored andcommitted
[DoctrineBridge] add new DatePointType Doctrine type
1 parent 6f598f9 commit c61752e
Copy full SHA for c61752e

File tree

5 files changed

+208
-0
lines changed
Filter options

5 files changed

+208
-0
lines changed

‎src/Symfony/Bridge/Doctrine/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* Reset the manager registry using native lazy objects when applicable
88
* Deprecate the `DoctrineExtractor::getTypes()` method, use `DoctrineExtractor::getType()` instead
9+
* Add support for `Symfony\Component\Clock\DatePoint` as `DatePointType` Doctrine type
910

1011
7.2
1112
---
+37Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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\DependencyInjection\CompilerPass;
13+
14+
use Symfony\Bridge\Doctrine\Types\DatePointType;
15+
use Symfony\Component\Clock\DatePoint;
16+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
17+
use Symfony\Component\DependencyInjection\ContainerBuilder;
18+
19+
final class RegisterDatePointTypePass implements CompilerPassInterface
20+
{
21+
public function process(ContainerBuilder $container): void
22+
{
23+
if (!class_exists(DatePoint::class)) {
24+
return;
25+
}
26+
27+
if (!$container->hasParameter('doctrine.dbal.connection_factory.types')) {
28+
return;
29+
}
30+
31+
$types = $container->getParameter('doctrine.dbal.connection_factory.types');
32+
33+
$types['date_point'] ??= ['class' => DatePointType::class];
34+
35+
$container->setParameter('doctrine.dbal.connection_factory.types', $types);
36+
}
37+
}
+41Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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\DependencyInjection\CompilerPass;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass\RegisterDatePointTypePass;
16+
use Symfony\Bridge\Doctrine\Types\DatePointType;
17+
use Symfony\Component\Clock\DatePoint;
18+
use Symfony\Component\DependencyInjection\ContainerBuilder;
19+
20+
class RegisterDatePointTypePassTest extends TestCase
21+
{
22+
protected function setUp(): void
23+
{
24+
if (!class_exists(DatePoint::class)) {
25+
self::markTestSkipped('The DatePoint class is not available.');
26+
}
27+
}
28+
29+
public function testRegistered()
30+
{
31+
$container = new ContainerBuilder();
32+
$container->setParameter('doctrine.dbal.connection_factory.types', ['foo' => 'bar']);
33+
(new RegisterDatePointTypePass())->process($container);
34+
35+
$expected = [
36+
'foo' => 'bar',
37+
'date_point' => ['class' => DatePointType::class],
38+
];
39+
$this->assertSame($expected, $container->getParameter('doctrine.dbal.connection_factory.types'));
40+
}
41+
}
+85Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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\Platforms\AbstractPlatform;
15+
use Doctrine\DBAL\Platforms\MariaDBPlatform;
16+
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
17+
use Doctrine\DBAL\Platforms\SqlitePlatform;
18+
use Doctrine\DBAL\Types\ConversionException;
19+
use Doctrine\DBAL\Types\Type;
20+
use PHPUnit\Framework\TestCase;
21+
use Symfony\Bridge\Doctrine\Types\DatePointType;
22+
use Symfony\Component\Clock\DatePoint;
23+
24+
final class DatePointTypeTest extends TestCase
25+
{
26+
private DatePointType $type;
27+
28+
public static function setUpBeforeClass(): void
29+
{
30+
$name = DatePointType::NAME;
31+
if (Type::hasType($name)) {
32+
Type::overrideType($name, DatePointType::class);
33+
} else {
34+
Type::addType($name, DatePointType::class);
35+
}
36+
}
37+
38+
protected function setUp(): void
39+
{
40+
if (!class_exists(DatePoint::class)) {
41+
self::markTestSkipped('The DatePoint class is not available.');
42+
}
43+
$this->type = Type::getType(DatePointType::NAME);
44+
}
45+
46+
public function testDatePointConvertsToDatabaseValue()
47+
{
48+
$datePoint = new DatePoint('2025-03-03 12:13:14');
49+
50+
$expected = $datePoint->format('Y-m-d H:i:s');
51+
$actual = $this->type->convertToDatabaseValue($datePoint, new PostgreSQLPlatform());
52+
53+
$this->assertSame($expected, $actual);
54+
}
55+
56+
public function testDatePointConvertsToPHPValue()
57+
{
58+
$datePoint = new DatePoint();
59+
$actual = $this->type->convertToPHPValue($datePoint, new SqlitePlatform());
60+
61+
$this->assertSame($datePoint, $actual);
62+
}
63+
64+
public function testNullConvertsToPHPValue()
65+
{
66+
$actual = $this->type->convertToPHPValue(null, new SqlitePlatform());
67+
68+
$this->assertNull($actual);
69+
}
70+
71+
public function testDateTimeImmutableConvertsToPHPValue()
72+
{
73+
$format = 'Y-m-d H:i:s';
74+
$dateTime = new \DateTimeImmutable('2025-03-03 12:13:14');
75+
$actual = $this->type->convertToPHPValue($dateTime, new SqlitePlatform());
76+
$expected = DatePoint::createFromInterface($dateTime);
77+
78+
$this->assertSame($expected->format($format), $actual->format($format));
79+
}
80+
81+
public function testGetName()
82+
{
83+
$this->assertSame('date_point', $this->type->getName());
84+
}
85+
}
+44Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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\DateTimeImmutableType;
16+
use Symfony\Component\Clock\DatePoint;
17+
18+
final class DatePointType extends DateTimeImmutableType
19+
{
20+
public const NAME = 'date_point';
21+
22+
/**
23+
* @param T $value
24+
*
25+
* @return (T is null ? null : DatePoint)
26+
*
27+
* @template T
28+
*/
29+
public function convertToPHPValue(mixed $value, AbstractPlatform $platform): ?DatePoint
30+
{
31+
if (null === $value || $value instanceof DatePoint) {
32+
return $value;
33+
}
34+
35+
$value = parent::convertToPHPValue($value, $platform);
36+
37+
return DatePoint::createFromInterface($value);
38+
}
39+
40+
public function getName(): string
41+
{
42+
return self::NAME;
43+
}
44+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.