Completed
Pull Request — 3.0.x (#3980)
by Guilherme
65:33
created

UniqueConstraint::_addColumn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 1
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   $indexName
40
     * @param string[] $columns
41
     * @param string[] $flags
42
     * @param mixed[]  $options
43
     */
44
    public function __construct($indexName, array $columns, array $flags = [], array $options = [])
45
    {
46
        $this->_setName($indexName);
47
48
        $this->options = $options;
49
50
        foreach ($columns as $column) {
51
            $this->_addColumn($column);
52
        }
53
54
        foreach ($flags as $flag) {
55
            $this->addFlag($flag);
56
        }
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function getColumns()
63
    {
64
        return array_keys($this->columns);
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function getQuotedColumns(AbstractPlatform $platform)
71
    {
72
        $columns = [];
73
74
        foreach ($this->columns as $column) {
75
            $columns[] = $column->getQuotedName($platform);
76
        }
77
78
        return $columns;
79
    }
80
81
    /**
82
     * @return string[]
83
     */
84
    public function getUnquotedColumns()
85
    {
86
        return array_map([$this, 'trimQuotes'], $this->getColumns());
87
    }
88
89
    /**
90
     * Returns platform specific flags for unique constraint.
91
     *
92
     * @return string[]
93
     */
94
    public function getFlags()
95
    {
96
        return array_keys($this->flags);
97
    }
98
99
    /**
100
     * Adds flag for a unique constraint that translates to platform specific handling.
101
     *
102
     * @param string $flag
103
     *
104
     * @return self
105
     *
106
     * @example $uniqueConstraint->addFlag('CLUSTERED')
107
     */
108
    public function addFlag($flag)
109
    {
110
        $this->flags[strtolower($flag)] = true;
111
112
        return $this;
113
    }
114
115
    /**
116
     * Does this unique constraint have a specific flag?
117
     *
118
     * @param string $flag
119
     *
120
     * @return bool
121
     */
122
    public function hasFlag($flag)
123
    {
124
        return isset($this->flags[strtolower($flag)]);
125
    }
126
127
    /**
128
     * Removes a flag.
129
     *
130
     * @param string $flag
131
     *
132
     * @return void
133
     */
134
    public function removeFlag($flag)
135
    {
136
        unset($this->flags[strtolower($flag)]);
137
    }
138
139
    /**
140
     * @param string $name
141
     *
142
     * @return bool
143
     */
144
    public function hasOption($name)
145
    {
146
        return isset($this->options[strtolower($name)]);
147
    }
148
149
    /**
150
     * @param string $name
151
     *
152
     * @return mixed
153
     */
154
    public function getOption($name)
155
    {
156
        return $this->options[strtolower($name)];
157
    }
158
159
    /**
160
     * @return mixed[]
161
     */
162
    public function getOptions()
163
    {
164
        return $this->options;
165
    }
166
167
    /**
168
     * @return void
169
     */
170
    protected function _addColumn(string $column)
171
    {
172
        $this->columns[$column] = new Identifier($column);
173
    }
174
}
175