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
|
|
|
* Sets the index columns. |
56
|
|
|
* |
57
|
|
|
* @param string[] $columns Columns |
58
|
|
|
* |
59
|
|
|
* @return $this |
60
|
|
|
*/ |
61
|
|
|
public function setColumns($columns) |
62
|
|
|
{ |
63
|
|
|
$this->columns = $columns; |
64
|
|
|
|
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Gets the index columns. |
70
|
|
|
* |
71
|
|
|
* @return string[] |
72
|
|
|
*/ |
73
|
|
|
public function getColumns() |
74
|
30 |
|
{ |
75
|
|
|
return $this->columns; |
76
|
30 |
|
} |
77
|
30 |
|
|
78
|
|
|
/** |
79
|
|
|
* Sets the index type. |
80
|
|
|
* |
81
|
|
|
* @param string $type Type |
82
|
|
|
* |
83
|
|
|
* @return $this |
84
|
|
|
*/ |
85
|
30 |
|
public function setType($type) |
86
|
|
|
{ |
87
|
30 |
|
$this->type = $type; |
88
|
|
|
|
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Gets the index type. |
94
|
|
|
* |
95
|
|
|
* @return string |
96
|
12 |
|
*/ |
97
|
|
|
public function getType() |
98
|
12 |
|
{ |
99
|
12 |
|
return $this->type; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Sets the index name. |
104
|
|
|
* |
105
|
|
|
* @param string $name Name |
106
|
|
|
* |
107
|
32 |
|
* @return $this |
108
|
|
|
*/ |
109
|
32 |
|
public function setName($name) |
110
|
|
|
{ |
111
|
|
|
$this->name = $name; |
112
|
|
|
|
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Gets the index name. |
118
|
10 |
|
* |
119
|
|
|
* @return string|null |
120
|
10 |
|
*/ |
121
|
10 |
|
public function getName() |
122
|
|
|
{ |
123
|
|
|
return $this->name; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Sets the index limit. |
128
|
|
|
* |
129
|
30 |
|
* @param int|array $limit limit value or array of limit value |
130
|
|
|
* |
131
|
30 |
|
* @return $this |
132
|
|
|
*/ |
133
|
|
|
public function setLimit($limit) |
134
|
|
|
{ |
135
|
|
|
$this->limit = $limit; |
136
|
|
|
|
137
|
|
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
1 |
|
/** |
141
|
|
|
* Gets the index limit. |
142
|
1 |
|
* |
143
|
1 |
|
* @return int|array |
144
|
|
|
*/ |
145
|
|
|
public function getLimit() |
146
|
|
|
{ |
147
|
|
|
return $this->limit; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
15 |
|
* Sets the index columns sort order. |
152
|
|
|
* |
153
|
15 |
|
* @param string[] $order column name sort order key value pair |
154
|
|
|
* @return $this |
155
|
|
|
*/ |
156
|
|
|
public function setOrder($order) |
157
|
|
|
{ |
158
|
|
|
$this->order = $order; |
159
|
|
|
|
160
|
|
|
return $this; |
161
|
|
|
} |
162
|
|
|
|
163
|
29 |
|
/** |
164
|
|
|
* Gets the index columns sort order. |
165
|
|
|
* |
166
|
29 |
|
* @return string[] |
167
|
29 |
|
*/ |
168
|
20 |
|
public function getOrder() |
169
|
1 |
|
{ |
170
|
|
|
return $this->order; |
171
|
|
|
} |
172
|
|
|
|
173
|
19 |
|
/** |
174
|
9 |
|
* Utility method that maps an array of index options to this objects methods. |
175
|
9 |
|
* |
176
|
9 |
|
* @param array $options Options |
177
|
9 |
|
* |
178
|
|
|
* @throws \RuntimeException |
179
|
|
|
* |
180
|
12 |
|
* @return $this |
181
|
12 |
|
*/ |
182
|
28 |
|
public function setOptions($options) |
183
|
28 |
|
{ |
184
|
|
|
// Valid Options |
185
|
|
|
$validOptions = ['type', 'unique', 'name', 'limit', 'order']; |
186
|
|
|
foreach ($options as $option => $value) { |
187
|
|
|
if (!in_array($option, $validOptions, true)) { |
188
|
|
|
throw new RuntimeException(sprintf('"%s" is not a valid index option.', $option)); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
// handle $options['unique'] |
192
|
|
|
if (strcasecmp($option, self::UNIQUE) === 0) { |
193
|
|
|
if ((bool)$value) { |
194
|
|
|
$this->setType(self::UNIQUE); |
195
|
|
|
} |
196
|
|
|
continue; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
$method = 'set' . ucfirst($option); |
200
|
|
|
$this->$method($value); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
return $this; |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|