-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathPeriodType.php
64 lines (54 loc) · 1.51 KB
/
PeriodType.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
<?php
declare(strict_types=1);
namespace Brick\DateTime\Doctrine\Types;
use Brick\DateTime\DateTimeException;
use Brick\DateTime\Period;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Exception\InvalidType;
use Doctrine\DBAL\Types\Exception\ValueNotConvertible;
use Doctrine\DBAL\Types\Type;
/**
* Doctrine type for Period.
*
* Maps its string representation to a VARCHAR column.
*/
final class PeriodType extends Type
{
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
{
if (!isset($column['length'])) {
$column['length'] = 64;
}
return $platform->getStringTypeDeclarationSQL($column);
}
public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform): ?string
{
if ($value === null) {
return null;
}
if ($value instanceof Period) {
return (string) $value;
}
throw InvalidType::new(
$value,
static::class,
[Period::class, 'null'],
);
}
public function convertToPHPValue(mixed $value, AbstractPlatform $platform): ?Period
{
if ($value === null) {
return null;
}
try {
return Period::parse((string) $value);
} catch (DateTimeException $e) {
throw ValueNotConvertible::new(
$value,
Period::class,
$e->getMessage(),
$e,
);
}
}
}