-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
InstantType.php
55 lines (45 loc) · 1.29 KB
/
InstantType.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
declare(strict_types=1);
namespace Brick\DateTime\Doctrine\Types;
use Brick\DateTime\Instant;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Types\Exception\InvalidType;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Platforms\AbstractPlatform;
/**
* Doctrine type for Instant.
*
* Maps to a database integer column, storing the epoch second, and silently discarding the nanos.
*/
final class InstantType extends Type
{
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
{
return $platform->getIntegerTypeDeclarationSQL($column);
}
public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform): ?int
{
if ($value === null) {
return null;
}
if ($value instanceof Instant) {
return $value->getEpochSecond();
}
throw InvalidType::new(
$value,
static::class,
[Instant::class, 'null'],
);
}
public function convertToPHPValue(mixed $value, AbstractPlatform $platform): ?Instant
{
if ($value === null) {
return null;
}
return Instant::of((int) $value);
}
public function getBindingType(): ParameterType
{
return ParameterType::INTEGER;
}
}