SetType::getValue()
last analyzed

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
ccs 0
cts 0
cp 0
c 0
b 0
f 0
nc 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * Team: jungle
5
 * User: Roma Baranenko
6
 * Contacts: <[email protected]>
7
 * Date: 05.12.17
8
 * Time: 18:50
9
 */
10
11
namespace Doctrine\DBAL\Types;
12
13
14
use InvalidArgumentException;
15
use Doctrine\DBAL\Platforms\AbstractPlatform;
16
17
/**
18
 * Class SetType
19
 * @package Doctrine\DBAL\Types
20
 * @author Roma Baranenko <[email protected]>
21
 */
22
abstract class SetType extends Type {
23
24
    /**
25
     * @return string[]
26
     */
27
    abstract protected function getValue();
28
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 16
    public function convertToDatabaseValue($value, AbstractPlatform $platform) {
34
35 16
        if (empty($value)) {
36
37 4
            return null;
38 12
        } elseif (!is_array($value)) {
39
40 4
            throw new InvalidArgumentException('Error "' . $this->getName() . '" type, "' . $value . '" must be array.');
41
        }
42
43 8
        foreach ($value as $item) {
44
45 8
            if (in_array($item, $this->getValue())) {
46
47 6
                continue;
48
            }
49
50 4
            throw new InvalidArgumentException('Invalid "' . $this->getName() . '" type, "' . $item . '" not allowed.');
51 2
        }
52
53 4
        return implode(',', $value);
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 8
    public function convertToPHPValue($value, AbstractPlatform $platform) {
60
61 8
        if (empty($value)) {
62
63 4
            return null;
64
        }
65
66 4
        return explode(',', $value);
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) {
73
74 4
        $allow = array_map(function ($type) {
75 4
            return '\'' . $type . '\'';
76 4
        }, $this->getValue());
77
78 4
        return 'SET ( ' . implode(',', $allow) . ' )';
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84 4
    public function requiresSQLCommentHint(AbstractPlatform $platform) {
85 4
        return true;
86
    }
87
}
88