Completed
Push — master ( 738857...981736 )
by Roma
01:08
created

SetType   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 68
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
getValue() 0 1 ?
B convertToDatabaseValue() 0 24 5
A convertToPHPValue() 0 9 2
A getSQLDeclaration() 0 8 1
A requiresSQLCommentHint() 0 3 1
1
<?php
2
3
namespace Doctrine\DBAL\Types;
4
5
6
use InvalidArgumentException;
7
use Doctrine\DBAL\Platforms\AbstractPlatform;
8
9
abstract class SetType extends Type {
10
11
    /**
12
     * @return string[]
13
     */
14
    abstract protected function getValue();
15
16
17
    /**
18
     * {@inheritdoc}
19
     */
20
    public function convertToDatabaseValue($value, AbstractPlatform $platform) {
21
22
        if (empty($value)) {
23
24
            return null;
25
        }
26
27
        if (!is_array($value)) {
28
29
            throw new InvalidArgumentException('Error "' . $this->getName() . '" type, "' . $value . '" must be array.');
30
        }
31
32
        foreach ($value as $item) {
33
34
            if (in_array($item, $this->getValue())) {
35
36
                continue;
37
            }
38
39
            throw new InvalidArgumentException('Invalid "' . $this->getName() . '" type, "' . $value . '" not allowed.');
40
        }
41
42
        return implode(',', $value);
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function convertToPHPValue($value, AbstractPlatform $platform) {
49
50
        if (empty($value)) {
51
52
            return null;
53
        }
54
55
        return explode(',', $value);
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) {
62
63
        $allow = array_map(function ($type) {
64
            return '\'' . $type . '\'';
65
        }, $this->getValue());
66
67
        return 'SET ( ' . implode(',', $allow) . ' )';
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function requiresSQLCommentHint(AbstractPlatform $platform) {
74
        return true;
75
    }
76
}
77