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