-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathDayOfWeekType.php
66 lines (56 loc) · 1.54 KB
/
DayOfWeekType.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\DayOfWeek;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Types\Exception\InvalidType;
use Doctrine\DBAL\Types\Exception\ValueNotConvertible;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use ValueError;
/**
* Doctrine type for DayOfWeek.
*
* Maps to a database small integer column.
*/
final class DayOfWeekType extends Type
{
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
{
return $platform->getSmallIntTypeDeclarationSQL($column);
}
public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform): ?int
{
if ($value === null) {
return null;
}
if ($value instanceof DayOfWeek) {
return $value->value;
}
throw InvalidType::new(
$value,
static::class,
[DayOfWeek::class, 'null'],
);
}
public function convertToPHPValue(mixed $value, AbstractPlatform $platform): ?DayOfWeek
{
if ($value === null) {
return null;
}
try {
return DayOfWeek::from((int) $value);
} catch (ValueError $e) {
throw ValueNotConvertible::new(
$value,
DayOfWeek::class,
$e->getMessage(),
$e,
);
}
}
public function getBindingType(): ParameterType
{
return ParameterType::INTEGER;
}
}