Passed
Push — phpstan-tests ( 6a2b78...974220 )
by Michael
61:35 queued 23s
created

UniqueConstraint   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 168
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 16
eloc 26
dl 0
loc 168
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A _addColumn() 0 7 2
A hasFlag() 0 3 1
A getOptions() 0 3 1
A getColumns() 0 3 1
A getUnquotedColumns() 0 3 1
A getQuotedColumns() 0 9 2
A removeFlag() 0 3 1
A getOption() 0 3 1
A addFlag() 0 5 1
A __construct() 0 12 3
A hasOption() 0 3 1
A getFlags() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Schema;
6
7
use Doctrine\DBAL\Platforms\AbstractPlatform;
8
use InvalidArgumentException;
9
use function array_keys;
10
use function array_map;
11
use function is_string;
12
use function strtolower;
13
14
/**
15
 * Class for a unique constraint.
16
 */
17
class UniqueConstraint extends AbstractAsset implements Constraint
18
{
19
    /**
20
     * Asset identifier instances of the column names the unique constraint is associated with.
21
     * array($columnName => Identifier)
22
     *
23
     * @var Identifier[]
24
     */
25
    protected $columns = [];
26
27
    /**
28
     * Platform specific flags
29
     * array($flagName => true)
30
     *
31
     * @var true[]
32
     */
33
    protected $flags = [];
34
35
    /**
36
     * Platform specific options
37
     *
38
     * @var mixed[]
39
     */
40
    private $options = [];
41
42
    /**
43
     * @param string   $indexName
44
     * @param string[] $columns
45
     * @param string[] $flags
46
     * @param mixed[]  $options
47
     */
48
    public function __construct($indexName, array $columns, array $flags = [], array $options = [])
49
    {
50
        $this->_setName($indexName);
51
52
        $this->options = $options;
53
54
        foreach ($columns as $column) {
55
            $this->_addColumn($column);
56
        }
57
58
        foreach ($flags as $flag) {
59
            $this->addFlag($flag);
60
        }
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function getColumns()
67
    {
68
        return array_keys($this->columns);
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function getQuotedColumns(AbstractPlatform $platform)
75
    {
76
        $columns = [];
77
78
        foreach ($this->columns as $column) {
79
            $columns[] = $column->getQuotedName($platform);
80
        }
81
82
        return $columns;
83
    }
84
85
    /**
86
     * @return string[]
87
     */
88
    public function getUnquotedColumns()
89
    {
90
        return array_map([$this, 'trimQuotes'], $this->getColumns());
91
    }
92
93
    /**
94
     * Returns platform specific flags for unique constraint.
95
     *
96
     * @return string[]
97
     */
98
    public function getFlags()
99
    {
100
        return array_keys($this->flags);
101
    }
102
103
    /**
104
     * Adds flag for a unique constraint that translates to platform specific handling.
105
     *
106
     * @param string $flag
107
     *
108
     * @return self
109
     *
110
     * @example $uniqueConstraint->addFlag('CLUSTERED')
111
     */
112
    public function addFlag($flag)
113
    {
114
        $this->flags[strtolower($flag)] = true;
115
116
        return $this;
117
    }
118
119
    /**
120
     * Does this unique constraint have a specific flag?
121
     *
122
     * @param string $flag
123
     *
124
     * @return bool
125
     */
126
    public function hasFlag($flag)
127
    {
128
        return isset($this->flags[strtolower($flag)]);
129
    }
130
131
    /**
132
     * Removes a flag.
133
     *
134
     * @param string $flag
135
     *
136
     * @return void
137
     */
138
    public function removeFlag($flag)
139
    {
140
        unset($this->flags[strtolower($flag)]);
141
    }
142
143
    /**
144
     * @param string $name
145
     *
146
     * @return bool
147
     */
148
    public function hasOption($name)
149
    {
150
        return isset($this->options[strtolower($name)]);
151
    }
152
153
    /**
154
     * @param string $name
155
     *
156
     * @return mixed
157
     */
158
    public function getOption($name)
159
    {
160
        return $this->options[strtolower($name)];
161
    }
162
163
    /**
164
     * @return mixed[]
165
     */
166
    public function getOptions()
167
    {
168
        return $this->options;
169
    }
170
171
    /**
172
     * @param string $column
173
     *
174
     * @return void
175
     *
176
     * @throws InvalidArgumentException
177
     */
178
    protected function _addColumn($column)
179
    {
180
        if (! is_string($column)) {
0 ignored issues
show
introduced by
The condition is_string($column) is always true.
Loading history...
181
            throw new InvalidArgumentException('Expecting a string as Index Column');
182
        }
183
184
        $this->columns[$column] = new Identifier($column);
185
    }
186
}
187