Completed
Push — master ( 9d6293...1b9c64 )
by Tarmo
18s queued 13s
created

RealEnumType::convertToDatabaseValue()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
nc 2
nop 2
dl 0
loc 13
ccs 9
cts 9
cp 1
crap 3
rs 10
c 1
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * /src/Doctrine/DBAL/Types/RealEnumType.php
5
 *
6
 * @author TLe, Tarmo Leppänen <[email protected]>
7
 */
8
9
namespace App\Doctrine\DBAL\Types;
10
11
use App\Enum\Interfaces\DatabaseEnumInterface;
12
use BackedEnum;
13
use Doctrine\DBAL\Platforms\AbstractPlatform;
14
use Doctrine\DBAL\Types\ConversionException;
15
use Doctrine\DBAL\Types\Type;
16
use InvalidArgumentException;
17
use function array_map;
18
use function gettype;
19
use function implode;
20
use function in_array;
21
use function is_string;
22
23
/**
24
 * Class RealEnumType
25
 *
26
 * @package App\Doctrine\DBAL\Types
27
 * @author TLe, Tarmo Leppänen <[email protected]>
28
 */
29
abstract class RealEnumType extends Type
30
{
31
    protected static string $name;
32
33
    /**
34
     * @psalm-var class-string<DatabaseEnumInterface&BackedEnum>
35
     */
36
    protected static string $enum;
37
38
    /**
39
     * @return array<int, string>
40
     */
41 9
    public static function getValues(): array
42
    {
43 9
        return static::$enum::getValues();
44
    }
45
46 2
    public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
47
    {
48 2
        $enumDefinition = implode(
49 2
            ', ',
50 2
            array_map(static fn (string $value): string => "'" . $value . "'", static::getValues()),
51 2
        );
52
53 2
        return 'ENUM(' . $enumDefinition . ')';
54
    }
55
56
    /**
57
     * @inheritDoc
58
     */
59 23
    public function convertToDatabaseValue($value, AbstractPlatform $platform): string
60
    {
61 23
        if (!in_array($value, static::$enum::cases(), true)) {
62 8
            $message = sprintf(
63 8
                "Invalid '%s' value '%s'",
64 8
                static::$name,
65 8
                is_string($value) ? $value : gettype($value),
66 8
            );
67
68 8
            throw new InvalidArgumentException($message);
69
        }
70
71 15
        return (string)parent::convertToDatabaseValue($value->value, $platform);
72
    }
73
74
    /**
75
     * @inheritDoc
76
     */
77 294
    public function convertToPHPValue($value, AbstractPlatform $platform): DatabaseEnumInterface
78
    {
79 294
        $value = (string)parent::convertToPHPValue($value, $platform);
80 294
        $enum = static::$enum::tryFrom($value);
81
82 294
        if ($enum !== null) {
83 287
            return $enum;
84
        }
85
86 7
        throw ConversionException::conversionFailedFormat(
87 7
            gettype($value),
88 7
            static::$name,
89 7
            'One of: "' . implode('", "', static::getValues()) . '"',
90 7
        );
91
    }
92
93
    /**
94
     * Parent method is deprecated, so remove this after it has been removed.
95
     *
96
     * @codeCoverageIgnore
97
     */
98
    public function getName(): string
99
    {
100
        return '';
101
    }
102
}
103