-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
AbstractEnumType.php
61 lines (49 loc) · 1.07 KB
/
AbstractEnumType.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
<?php
declare(strict_types=1);
/*
* This file is part of the "elao/enum" package.
*
* Copyright (C) Elao
*
* @author Elao <contact@elao.com>
*/
namespace Elao\Enum\Bridge\Doctrine\ODM\Types;
use Doctrine\ODM\MongoDB\Types\ClosureToPHP;
use Doctrine\ODM\MongoDB\Types\Type;
/**
* @template TEnum of \BackedEnum
*/
abstract class AbstractEnumType extends Type
{
use ClosureToPHP;
/**
* {@inheritdoc}
*/
public function convertToDatabaseValue($value): int|string|null
{
if (null === $value) {
return null;
}
if ($value instanceof \BackedEnum) {
return $value->value;
}
return $value;
}
/**
* {@inheritdoc}
*
* @psalm-return TEnum
*/
public function convertToPHPValue($value): ?\BackedEnum
{
if (null === $value) {
return null;
}
$class = $this->getEnumClass();
return $class::from($value);
}
/**
* @return class-string<TEnum>
*/
abstract protected function getEnumClass(): string;
}