Completed
Pull Request — master (#19)
by Alejandro
05:54
created

PhpEnumType::convertToPHPValue()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 13
cts 13
cp 1
rs 8.6845
c 0
b 0
f 0
cc 4
eloc 13
nc 5
nop 2
crap 4
1
<?php
2
declare(strict_types=1);
3
4
namespace Acelaya\Doctrine\Type;
5
6
use Acelaya\Doctrine\Exception\InvalidArgumentException;
7
use Doctrine\DBAL\DBALException;
8
use Doctrine\DBAL\Platforms\AbstractPlatform;
9
use Doctrine\DBAL\Types\Type;
10
use MyCLabs\Enum\Enum;
11
12
class PhpEnumType extends Type
13
{
14
    /**
15
     * @var string
16
     */
17
    private $name;
18
    /**
19
     * @var string
20
     */
21
    protected $enumClass = Enum::class;
22
23
    /**
24
     * Gets the name of this type.
25
     *
26
     * @return string
27
     */
28 2
    public function getName()
29
    {
30 2
        return $this->name ?: 'enum';
31
    }
32
33
    /**
34
     * Gets the SQL declaration snippet for a field of this type.
35
     *
36
     * @param array $fieldDeclaration The field declaration.
37
     * @param AbstractPlatform $platform The currently used database platform.
38
     *
39
     * @return string
40
     */
41 1
    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
42
    {
43 1
        return $platform->getVarcharTypeDeclarationSQL([]);
44
    }
45
46
    /**
47
     * @param string|null $value
48
     * @param AbstractPlatform $platform
49
     * @return mixed
50
     * @throws InvalidArgumentException
51
     */
52 4
    public function convertToPHPValue($value, AbstractPlatform $platform)
53
    {
54 4
        if ($value === null) {
55 1
            return null;
56
        }
57
58
        // If the enumeration provides a casting method, apply it
59 3
        if (\method_exists($this->enumClass, 'castValueIn')) {
60 1
            $value = \call_user_func([$this->enumClass, 'castValueIn'], $value);
61
        }
62
63
        // Check if the value is valid for this enumeration
64 3
        $isValid = \call_user_func([$this->enumClass, 'isValid'], $value);
65 3
        if (! $isValid) {
66 1
            throw new InvalidArgumentException(\sprintf(
67 1
                'The value "%s" is not valid for the enum "%s". Expected one of ["%s"]',
68 1
                $value,
69 1
                $this->enumClass,
70 1
                \implode('", "', \call_user_func([$this->enumClass, 'toArray']))
71
            ));
72
        }
73
74 2
        return new $this->enumClass($value);
75
    }
76
77 8
    public function convertToDatabaseValue($value, AbstractPlatform $platform)
78
    {
79 8
        if ($value === null) {
80 1
            return null;
81
        }
82
83
        // If the enumeration provides a casting method, apply it
84 7
        if (\method_exists($this->enumClass, 'castValueOut')) {
85 1
            return \call_user_func([$this->enumClass, 'castValueOut'], $value);
86
        }
87
88
        // Otherwise, cast to string
89 6
        return (string) $value;
90
    }
91
92
    /**
93
     * @param string $typeNameOrEnumClass
94
     * @param string|null $enumClass
95
     * @throws InvalidArgumentException
96
     * @throws DBALException
97
     */
98 18
    public static function registerEnumType($typeNameOrEnumClass, $enumClass = null)
99
    {
100 18
        $typeName = $typeNameOrEnumClass;
101 18
        $enumClass = $enumClass ?: $typeNameOrEnumClass;
102
103 18
        if (! \is_subclass_of($enumClass, Enum::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \MyCLabs\Enum\Enum::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
104 1
            throw new InvalidArgumentException(\sprintf(
105 1
                'Provided enum class "%s" is not valid. Enums must extend "%s"',
106 1
                $enumClass,
107 1
                Enum::class
108
            ));
109
        }
110
111
        // Register and customize the type
112 17
        self::addType($typeName, static::class);
113
        /** @var PhpEnumType $type */
114 17
        $type = self::getType($typeName);
115 17
        $type->name = $typeName;
116 17
        $type->enumClass = $enumClass;
117 17
    }
118
119
    /**
120
     * @param array $types
121
     * @throws InvalidArgumentException
122
     * @throws DBALException
123
     */
124 2
    public static function registerEnumTypes(array $types)
125
    {
126 2
        foreach ($types as $typeName => $enumClass) {
127 2
            $typeName = \is_string($typeName) ? $typeName : $enumClass;
128 2
            static::registerEnumType($typeName, $enumClass);
129
        }
130 2
    }
131
132
    /**
133
     * @param AbstractPlatform $platform
134
     * @return boolean
135
     */
136 1
    public function requiresSQLCommentHint(AbstractPlatform $platform)
137
    {
138 1
        return true;
139
    }
140
}
141