1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Table Index definition |
7
|
|
|
* |
8
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
9
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
10
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
11
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
12
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
13
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
14
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
15
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
16
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
17
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
18
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
19
|
|
|
* |
20
|
|
|
* @since 0.3.0 |
21
|
|
|
* @author Glynn Quelch <[email protected]> |
22
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
23
|
|
|
* @package PinkCrab\Table_Builder |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
namespace PinkCrab\Table_Builder; |
27
|
|
|
|
28
|
|
|
class Index { |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Index name |
32
|
|
|
* |
33
|
|
|
* @since 0.1.0 |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $key_name; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Column referenced |
40
|
|
|
* |
41
|
|
|
* @since 0.1.0 |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected $column; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Unique index |
48
|
|
|
* |
49
|
|
|
* @since 0.1.0 |
50
|
|
|
* @var bool |
51
|
|
|
*/ |
52
|
|
|
protected $unique = false; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Allow full text index. |
56
|
|
|
* |
57
|
|
|
* @since 0.2.0 |
58
|
|
|
* @var bool |
59
|
|
|
*/ |
60
|
|
|
protected $full_text = false; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* is primary key |
64
|
|
|
* |
65
|
|
|
* @since 0.3.0 |
66
|
|
|
* @var bool |
67
|
|
|
*/ |
68
|
|
|
protected $primary = false; |
69
|
|
|
|
70
|
|
|
public function __construct( string $column, ?string $key_name = null ) { |
71
|
|
|
$this->key_name = $key_name ?? 'ix_' . $column; |
72
|
|
|
$this->column = $column; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Set is primary key |
77
|
|
|
* |
78
|
|
|
* @param bool $primary is primary key |
79
|
|
|
* @return self |
80
|
|
|
*/ |
81
|
|
|
public function primary( bool $primary = true ): self { |
82
|
|
|
$this->primary = $primary; |
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Are the key unique |
88
|
|
|
* |
89
|
|
|
* @since 0.1.0 |
90
|
|
|
* @param boolean $unique |
91
|
|
|
* @return self |
92
|
|
|
*/ |
93
|
|
|
public function unique( bool $unique = true ): self { |
94
|
|
|
$this->unique = $unique; |
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Are the key unique |
100
|
|
|
* |
101
|
|
|
* @since 0.1.0 |
102
|
|
|
* @param boolean $full_text |
103
|
|
|
* @return self |
104
|
|
|
*/ |
105
|
|
|
public function full_text( bool $full_text = true ): self { |
106
|
|
|
$this->full_text = $full_text; |
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Get index name |
112
|
|
|
* |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
public function get_key_name(): string { |
116
|
|
|
return $this->key_name; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Exports the index as a stdClass |
121
|
|
|
* |
122
|
|
|
* @return object |
123
|
|
|
*/ |
124
|
|
|
public function export() { |
125
|
|
|
return (object) array( |
126
|
|
|
'key_name' => $this->key_name, |
127
|
|
|
'column' => $this->column, |
128
|
|
|
'primary' => $this->primary, |
129
|
|
|
'unique' => $this->unique, |
130
|
|
|
'full_text' => $this->full_text, |
131
|
|
|
); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Get column referenced |
137
|
|
|
* |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
|
|
public function get_column(): string { |
141
|
|
|
return $this->column; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Checks if using a using HASH |
146
|
|
|
* |
147
|
|
|
* @return bool |
148
|
|
|
*/ |
149
|
|
|
public function is_primary(): bool { |
150
|
|
|
return $this->primary; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Checks if using a unique index |
155
|
|
|
* |
156
|
|
|
* @return bool |
157
|
|
|
*/ |
158
|
|
|
public function is_unique(): bool { |
159
|
|
|
return $this->unique; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Checks if using afull text index. |
164
|
|
|
* |
165
|
|
|
* @return bool |
166
|
|
|
*/ |
167
|
|
|
public function is_full_text(): bool { |
168
|
|
|
return $this->full_text; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Retuns the index type. |
173
|
|
|
* |
174
|
|
|
* @return string |
175
|
|
|
*/ |
176
|
|
|
public function get_type(): string { |
177
|
|
|
if ( $this->is_primary() ) { |
178
|
|
|
return 'primary'; |
179
|
|
|
} elseif ( $this->is_unique() ) { |
180
|
|
|
return 'unique'; |
181
|
|
|
} elseif ( $this->is_full_text() ) { |
182
|
|
|
return 'fulltext'; |
183
|
|
|
} else { |
184
|
|
|
return ''; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|