-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathLocalTimeType.php
66 lines (55 loc) · 1.6 KB
/
LocalTimeType.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
56
57
58
59
60
61
62
63
64
65
66
<?php
declare(strict_types=1);
namespace Brick\DateTime\Doctrine\Types;
use Brick\DateTime\DateTimeException;
use Brick\DateTime\LocalTime;
use Doctrine\DBAL\Types\Exception\InvalidType;
use Doctrine\DBAL\Types\Exception\ValueNotConvertible;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Platforms\AbstractPlatform;
/**
* Doctrine type for LocalTime.
*
* Maps to a database TIME type if supported.
*/
final class LocalTimeType extends Type
{
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
{
return $platform->getTimeTypeDeclarationSQL($column);
}
public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform): ?string
{
if ($value === null) {
return null;
}
if ($value instanceof LocalTime) {
$stringValue = (string) $value;
if ($value->getSecond() === 0 && $value->getNano() === 0) {
$stringValue .= ':00';
}
return $stringValue;
}
throw InvalidType::new(
$value,
static::class,
[LocalTime::class, 'null'],
);
}
public function convertToPHPValue(mixed $value, AbstractPlatform $platform): ?LocalTime
{
if ($value === null) {
return null;
}
try {
return LocalTime::parse((string) $value);
} catch (DateTimeException $e) {
throw ValueNotConvertible::new(
$value,
LocalTime::class,
$e->getMessage(),
$e,
);
}
}
}