PhpEnumType   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 8
Bugs 0 Features 0
Metric Value
eloc 40
c 8
b 0
f 0
dl 0
loc 119
rs 10
wmc 17
ccs 45
cts 45
cp 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A registerEnumType() 0 19 3
A requiresSQLCommentHint() 0 3 1
A getName() 0 3 2
A convertToPHPValue() 0 29 4
A getSQLDeclaration() 0 3 1
A registerEnumTypes() 0 5 3
A convertToDatabaseValue() 0 15 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Acelaya\Doctrine\Type;
6
7
use Acelaya\Doctrine\Exception\InvalidArgumentException;
8
use Doctrine\DBAL\DBALException;
9
use Doctrine\DBAL\Platforms\AbstractPlatform;
10
use Doctrine\DBAL\Types\Type;
11
use MyCLabs\Enum\Enum;
12
13
use function implode;
14
use function is_string;
15
use function is_subclass_of;
16
use function method_exists;
17
use function sprintf;
18
19
class PhpEnumType extends Type
20
{
21
    /** @var string */
22
    private $name;
23
    /** @var string */
24
    protected $enumClass = Enum::class;
25
26 2
    public function getName(): string
27
    {
28 2
        return $this->name ?: 'enum';
29
    }
30
31
    /**
32
     * @param mixed[] $fieldDeclaration
33
     * @return string
34
     */
35 1
    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
36
    {
37 1
        return $platform->getVarcharTypeDeclarationSQL($fieldDeclaration);
38
    }
39
40
    /**
41
     * @param string|null $value
42
     * @return mixed
43
     * @throws InvalidArgumentException
44
     */
45 4
    public function convertToPHPValue($value, AbstractPlatform $platform)
46
    {
47 4
        if ($value === null) {
48 1
            return null;
49
        }
50
51
        // If the enumeration provides a casting method, apply it
52 3
        if (method_exists($this->enumClass, 'castValueIn')) {
53
            /** @var callable $castValueIn */
54 1
            $castValueIn = [$this->enumClass, 'castValueIn'];
55 1
            $value = $castValueIn($value);
56
        }
57
58
        // Check if the value is valid for this enumeration
59
        /** @var callable $isValidCallable */
60 3
        $isValidCallable = [$this->enumClass, 'isValid'];
61 3
        $isValid = $isValidCallable($value);
62 3
        if (! $isValid) {
63
            /** @var callable $toArray */
64 1
            $toArray = [$this->enumClass, 'toArray'];
65 1
            throw new InvalidArgumentException(sprintf(
66 1
                'The value "%s" is not valid for the enum "%s". Expected one of ["%s"]',
67 1
                $value,
68 1
                $this->enumClass,
69 1
                implode('", "', $toArray())
70
            ));
71
        }
72
73 2
        return new $this->enumClass($value);
74
    }
75
76 8
    public function convertToDatabaseValue($value, AbstractPlatform $platform)
77
    {
78 8
        if ($value === null) {
79 1
            return null;
80
        }
81
82
        // If the enumeration provides a casting method, apply it
83 7
        if (method_exists($this->enumClass, 'castValueOut')) {
84
            /** @var callable $castValueOut */
85 1
            $castValueOut = [$this->enumClass, 'castValueOut'];
86 1
            return $castValueOut($value);
87
        }
88
89
        // Otherwise, cast to string
90 6
        return (string) $value;
91
    }
92
93
    /**
94
     * @throws InvalidArgumentException
95
     * @throws DBALException
96
     */
97 18
    public static function registerEnumType(string $typeNameOrEnumClass, ?string $enumClass = null): void
98
    {
99 18
        $typeName = $typeNameOrEnumClass;
100 18
        $enumClass = $enumClass ?: $typeNameOrEnumClass;
101
102 18
        if (! is_subclass_of($enumClass, Enum::class)) {
103 1
            throw new InvalidArgumentException(sprintf(
104 1
                'Provided enum class "%s" is not valid. Enums must extend "%s"',
105 1
                $enumClass,
106 1
                Enum::class
107
            ));
108
        }
109
110
        // Register and customize the type
111 17
        self::addType($typeName, static::class);
112
        /** @var PhpEnumType $type */
113 17
        $type = self::getType($typeName);
114 17
        $type->name = $typeName;
115 17
        $type->enumClass = $enumClass;
116
    }
117
118
    /**
119
     * @param array<string|int, string> $types
120
     * @throws InvalidArgumentException
121
     * @throws DBALException
122
     */
123 2
    public static function registerEnumTypes(array $types): void
124
    {
125 2
        foreach ($types as $typeName => $enumClass) {
126 2
            $typeName = is_string($typeName) ? $typeName : $enumClass;
127 2
            static::registerEnumType($typeName, $enumClass);
128
        }
129
    }
130
131
    /**
132
     * @param AbstractPlatform $platform
133
     * @return boolean
134
     */
135 1
    public function requiresSQLCommentHint(AbstractPlatform $platform): bool
136
    {
137 1
        return true;
138
    }
139
}
140