Failed Conditions
Push — master ( 01c22b...e42c1f )
by Marco
79:13 queued 10s
created

UniqueConstraint::addFlag()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
dl 0
loc 5
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Schema;
6
7
use Doctrine\DBAL\Platforms\AbstractPlatform;
8
use function array_keys;
9
use function array_map;
10
use function strtolower;
11
12
/**
13
 * Class for a unique constraint.
14
 */
15
class UniqueConstraint extends AbstractAsset implements Constraint
16
{
17
    /**
18
     * Asset identifier instances of the column names the unique constraint is associated with.
19
     *
20
     * @var array<string, Identifier>
21
     */
22
    protected $columns = [];
23
24
    /**
25
     * Platform specific flags
26
     *
27
     * @var array<string, true>
28
     */
29
    protected $flags = [];
30
31
    /**
32
     * Platform specific options
33
     *
34
     * @var array<string, mixed>
35
     */
36
    private $options = [];
37
38
    /**
39
     * @param array<string>        $columns
40
     * @param array<string>        $flags
41
     * @param array<string, mixed> $options
42
     */
43
    public function __construct(string $indexName, array $columns, array $flags = [], array $options = [])
44
    {
45
        $this->_setName($indexName);
46
47
        $this->options = $options;
48
49
        foreach ($columns as $column) {
50
            $this->_addColumn($column);
51
        }
52
53
        foreach ($flags as $flag) {
54
            $this->addFlag($flag);
55
        }
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function getColumns() : array
62
    {
63
        return array_keys($this->columns);
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function getQuotedColumns(AbstractPlatform $platform) : array
70
    {
71
        $columns = [];
72
73
        foreach ($this->columns as $column) {
74
            $columns[] = $column->getQuotedName($platform);
75
        }
76
77
        return $columns;
78
    }
79
80
    /**
81
     * @return array<int, 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 array<int, string>
92
     */
93
    public function getFlags() : array
94
    {
95
        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
    public function addFlag(string $flag) : self
104
    {
105
        $this->flags[strtolower($flag)] = true;
106
107
        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
    public function hasOption(string $name) : bool
127
    {
128
        return isset($this->options[strtolower($name)]);
129
    }
130
131
    /**
132
     * @return mixed
133
     */
134
    public function getOption(string $name)
135
    {
136
        return $this->options[strtolower($name)];
137
    }
138
139
    /**
140
     * @return array<string, mixed>
141
     */
142
    public function getOptions() : array
143
    {
144
        return $this->options;
145
    }
146
147
    protected function _addColumn(string $column) : void
148
    {
149
        $this->columns[$column] = new Identifier($column);
150
    }
151
}
152