Passed
Pull Request — master (#1939)
by
unknown
02:54
created

Index::setInclude()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
c 0
b 0
f 0
ccs 0
cts 0
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 * MIT License
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
namespace Phinx\Db\Table;
9
10
use RuntimeException;
11
12
class Index
13
{
14
    /**
15
     * @var string
16
     */
17
    public const UNIQUE = 'unique';
18
19
    /**
20
     * @var string
21
     */
22
    public const INDEX = 'index';
23
24
    /**
25
     * @var string
26
     */
27
    public const FULLTEXT = 'fulltext';
28
29
    /**
30
     * @var string[]
31
     */
32
    protected $columns;
33
34
    /**
35
     * @var string
36
     */
37
    protected $type = self::INDEX;
38
39
    /**
40
     * @var string|null
41
     */
42
    protected $name;
43
44
    /**
45
     * @var int|array|null
46
     */
47
    protected $limit;
48
49
    /**
50
     * @var string[]
51
     */
52
    protected $order;
53
54
    /**
55
     * @var string[]
56
     */
57
    protected $includedColumns;
58
59
    /**
60
     * Sets the index columns.
61
     *
62
     * @param string[] $columns Columns
63
     *
64
     * @return $this
65
     */
66
    public function setColumns($columns)
67
    {
68
        $this->columns = $columns;
69
70
        return $this;
71
    }
72
73
    /**
74 30
     * Gets the index columns.
75
     *
76 30
     * @return string[]
77 30
     */
78
    public function getColumns()
79
    {
80
        return $this->columns;
81
    }
82
83
    /**
84
     * Sets the index type.
85 30
     *
86
     * @param string $type Type
87 30
     *
88
     * @return $this
89
     */
90
    public function setType($type)
91
    {
92
        $this->type = $type;
93
94
        return $this;
95
    }
96 12
97
    /**
98 12
     * Gets the index type.
99 12
     *
100
     * @return string
101
     */
102
    public function getType()
103
    {
104
        return $this->type;
105
    }
106
107 32
    /**
108
     * Sets the index name.
109 32
     *
110
     * @param string $name Name
111
     *
112
     * @return $this
113
     */
114
    public function setName($name)
115
    {
116
        $this->name = $name;
117
118 10
        return $this;
119
    }
120 10
121 10
    /**
122
     * Gets the index name.
123
     *
124
     * @return string|null
125
     */
126
    public function getName()
127
    {
128
        return $this->name;
129 30
    }
130
131 30
    /**
132
     * Sets the index limit.
133
     *
134
     * @param int|array $limit limit value or array of limit value
135
     *
136
     * @return $this
137
     */
138
    public function setLimit($limit)
139
    {
140 1
        $this->limit = $limit;
141
142 1
        return $this;
143 1
    }
144
145
    /**
146
     * Gets the index limit.
147
     *
148
     * @return int|array
149
     */
150
    public function getLimit()
151 15
    {
152
        return $this->limit;
153 15
    }
154
155
    /**
156
     * Sets the index columns sort order.
157
     *
158
     * @param string[] $order column name sort order key value pair
159
     * @return $this
160
     */
161
    public function setOrder($order)
162
    {
163 29
        $this->order = $order;
164
165
        return $this;
166 29
    }
167 29
168 20
    /**
169 1
     * Gets the index columns sort order.
170
     *
171
     * @return string[]
172
     */
173 19
    public function getOrder()
174 9
    {
175 9
        return $this->order;
176 9
    }
177 9
178
    /**
179
     * Sets the index included columns.
180 12
     *
181 12
     * @param string[] $includedColumns Columns
182 28
     * @return $this
183 28
     */
184
    public function setInclude($includedColumns)
185
    {
186
        $this->includedColumns = $includedColumns;
187
188
        return $this;
189
    }
190
191
    /**
192
     * Gets the index included columns.
193
     *
194
     * @return string[]
195
     */
196
    public function getInclude()
197
    {
198
        return $this->includedColumns;
199
    }
200
201
    /**
202
     * Utility method that maps an array of index options to this objects methods.
203
     *
204
     * @param array $options Options
205
     *
206
     * @throws \RuntimeException
207
     *
208
     * @return $this
209
     */
210
    public function setOptions($options)
211
    {
212
        // Valid Options
213
        $validOptions = ['type', 'unique', 'name', 'limit', 'order', 'include'];
214
        foreach ($options as $option => $value) {
215
            if (!in_array($option, $validOptions, true)) {
216
                throw new RuntimeException(sprintf('"%s" is not a valid index option.', $option));
217
            }
218
219
            // handle $options['unique']
220
            if (strcasecmp($option, self::UNIQUE) === 0) {
221
                if ((bool)$value) {
222
                    $this->setType(self::UNIQUE);
223
                }
224
                continue;
225
            }
226
227
            $method = 'set' . ucfirst($option);
228
            $this->$method($value);
229
        }
230
231
        return $this;
232
    }
233
}
234