AbstractSetType::requiresSQLCommentHint()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Okapon\DoctrineSetTypeBundle\DBAL\Types;
4
5
use Doctrine\DBAL\Platforms\AbstractPlatform;
6
use Doctrine\DBAL\Platforms\MySqlPlatform;
7
use Doctrine\DBAL\Types\Type;
8
9
/**
10
 * AbstractSetType
11
 *
12
 * Provides MySQL Set type for Doctrine in Symfony applications
13
 *
14
 * @author Yuichi Okada <[email protected]>
15
 */
16
abstract class AbstractSetType extends Type
17
{
18
    /**
19
     * @var string $name Name of this type
20
     */
21
    protected $name = '';
22
23
    /**
24
     * @var array $choices Map of available SET type, key and label
25
     * @static
26
     */
27
    protected static $choices = [];
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 6
    public function convertToDatabaseValue($value, AbstractPlatform $platform)
33
    {
34 6
        if (!is_array($value) || count($value) <= 0) {
35 3
            return null;
36
        }
37
38 3
        $diff = array_diff($value, $this->getValues());
39 3
        if (count($diff) > 0) {
40 1
            throw new \InvalidArgumentException(sprintf(
41 1
                    'Invalid value "%s". It is not defined in "%s::$choices"',
42 1
                    implode(',', $diff),
43 1
                    get_class($this)
44 1
                )
45 1
            );
46
        }
47
48 2
        return implode(',', $value);
49
    }
50
51
    /**
52
     * @param string|null $value
53
     * @param AbstractPlatform $platform
54
     * @return array
55
     */
56 4
    public function convertToPHPValue($value, AbstractPlatform $platform)
57
    {
58 4
        if ($value === null || $value === '') {
59 1
            return [];
60
        }
61 3
        if (strpos($value, ',') === false) {
62 2
            return [$value];
63
        }
64
65 1
        return explode(',', $value);
66
    }
67
68
    /**
69
     * @param array $fieldDeclaration
70
     * @param AbstractPlatform $platform
71
     * @return string
72
     */
73
    public function getSqlDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
74
    {
75 2
        $values = implode(', ', array_map(function ($value) {
76 2
                    return "'{$value}'";
77 2
                },
78 2
                $this->getValues()
79 2
            )
80 2
        );
81
82 2
        if (!$platform instanceof MySqlPlatform) {
83 1
            return $platform->getClobTypeDeclarationSQL($fieldDeclaration);
84
        }
85
86 1
        return sprintf('SET(%s)', $values);
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92 1
    public function requiresSQLCommentHint(AbstractPlatform $platform)
93
    {
94 1
        return true;
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100 1
    public function getName()
101
    {
102 1
        return $this->name ?: (new \ReflectionClass(get_class($this)))->getShortName();
103
    }
104
105
    /**
106
     * Get choices Label for the Set form field type
107
     *
108
     * @return array Values for the SET field
109
     */
110 11
    public static function getChoices()
111
    {
112 11
        return static::$choices;
113
    }
114
115
    /**
116
     * Get values for the SET field
117
     *
118
     * @return array Values for the SET field
119
     */
120 10
    public static function getValues()
121
    {
122 10
        return array_keys(static::getChoices());
123
    }
124
}
125