Failed Conditions
Pull Request — 3.0.x (#3980)
by Guilherme
06:55
created

UniqueConstraint::hasFlag()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Doctrine\DBAL\Schema;
4
5
use Doctrine\DBAL\Platforms\AbstractPlatform;
6
use function array_keys;
7
use function array_map;
8
use function strtolower;
9
10
/**
11
 * Class for a unique constraint.
12
 */
13
class UniqueConstraint extends AbstractAsset implements Constraint
14
{
15
    /**
16
     * Asset identifier instances of the column names the unique constraint is associated with.
17
     * array($columnName => Identifier)
18
     *
19
     * @var Identifier[]
20
     */
21
    protected $columns = [];
22
23
    /**
24
     * Platform specific flags.
25
     * array($flagName => true)
26
     *
27
     * @var true[]
28
     */
29
    protected $flags = [];
30
31
    /**
32
     * Platform specific options.
33
     *
34
     * @var mixed[]
35
     */
36
    private $options = [];
37
38
    /**
39
     * @param string[] $columns
40
     * @param string[] $flags
41
     * @param mixed[]  $options
42
     */
43 506
    public function __construct(string $name, array $columns, array $flags = [], array $options = [])
44
    {
45 506
        $this->_setName($name);
46
47 506
        $this->options = $options;
48
49 506
        foreach ($columns as $column) {
50 483
            $this->addColumn($column);
51
        }
52
53 506
        foreach ($flags as $flag) {
54 23
            $this->addFlag($flag);
55
        }
56 506
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function getColumns()
62
    {
63
        return array_keys($this->columns);
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 506
    public function getQuotedColumns(AbstractPlatform $platform)
70
    {
71 506
        $columns = [];
72
73 506
        foreach ($this->columns as $column) {
74 483
            $columns[] = $column->getQuotedName($platform);
75
        }
76
77 506
        return $columns;
78
    }
79
80
    /**
81
     * @return string[]
82
     */
83
    public function getUnquotedColumns() : array
84
    {
85
        return array_map([$this, 'trimQuotes'], $this->getColumns());
86
    }
87
88
    /**
89
     * Returns platform specific flags for unique constraint.
90
     *
91
     * @return string[]
92
     */
93 483
    public function getFlags() : array
94
    {
95 483
        return array_keys($this->flags);
96
    }
97
98
    /**
99
     * Adds flag for a unique constraint that translates to platform specific handling.
100
     *
101
     * @example $uniqueConstraint->addFlag('CLUSTERED')
102
     */
103 23
    public function addFlag(string $flag) : UniqueConstraint
104
    {
105 23
        $this->flags[strtolower($flag)] = true;
106
107 23
        return $this;
108
    }
109
110
    /**
111
     * Does this unique constraint have a specific flag?
112
     */
113
    public function hasFlag(string $flag) : bool
114
    {
115
        return isset($this->flags[strtolower($flag)]);
116
    }
117
118
    /**
119
     * Removes a flag.
120
     */
121
    public function removeFlag(string $flag) : void
122
    {
123
        unset($this->flags[strtolower($flag)]);
124
    }
125
126
    /**
127
     * Does this unique constraint have a specific option?
128
     */
129
    public function hasOption(string $name) : bool
130
    {
131
        return isset($this->options[strtolower($name)]);
132
    }
133
134
    /**
135
     * @return mixed
136
     */
137
    public function getOption(string $name)
138
    {
139
        return $this->options[strtolower($name)];
140
    }
141
142
    /**
143
     * @return mixed[]
144
     */
145
    public function getOptions() : array
146
    {
147
        return $this->options;
148
    }
149
150
    /**
151
     * Adds a new column to the unique constraint.
152
     */
153 483
    protected function addColumn(string $column) : void
154
    {
155 483
        $this->columns[$column] = new Identifier($column);
156 483
    }
157
}
158