1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Schema 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
|
|
|
use PinkCrab\Table_Builder\Column_Types; |
29
|
|
|
|
30
|
|
|
class Column { |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Gives access to various wrappers for |
34
|
|
|
* types |
35
|
|
|
* |
36
|
|
|
* @method varchar( ?int $length = null ) |
37
|
|
|
* @method text( ?int $length = null ) |
38
|
|
|
* @method int( ?int $length = null ) |
39
|
|
|
* @method float( ?int $length = null ) |
40
|
|
|
* @method double( ?int $length = null ) |
41
|
|
|
* @method datetime( ?string $default = null ) |
42
|
|
|
* @method timestamp( ?string $default = null ) |
43
|
|
|
*/ |
44
|
|
|
use Column_Types; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Column name |
48
|
|
|
* |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
protected $name; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* The column type |
55
|
|
|
* |
56
|
|
|
* @var string|null |
57
|
|
|
*/ |
58
|
|
|
protected $type = null; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* The column length |
62
|
|
|
* |
63
|
|
|
* @var int|null |
64
|
|
|
*/ |
65
|
|
|
protected $length = null; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* The column precision |
69
|
|
|
* Used for floating point values |
70
|
|
|
* |
71
|
|
|
* @var int|null |
72
|
|
|
*/ |
73
|
|
|
protected $precision = null; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Denotes if the column is nullable |
77
|
|
|
* |
78
|
|
|
* @var bool|null |
79
|
|
|
*/ |
80
|
|
|
protected $nullable = null; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* The columns default value |
84
|
|
|
* |
85
|
|
|
* @var string|null |
86
|
|
|
*/ |
87
|
|
|
protected $default = null; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* If the column has the auto incrememnt flag. |
91
|
|
|
* |
92
|
|
|
* @var bool|null |
93
|
|
|
*/ |
94
|
|
|
protected $auto_increment = null; |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Is the columns value unsigned |
98
|
|
|
* |
99
|
|
|
* @var bool|null |
100
|
|
|
*/ |
101
|
|
|
protected $unsigned = null; |
102
|
|
|
|
103
|
|
|
public function __construct( string $name ) { |
104
|
|
|
$this->name = $name; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Sets the columns type |
109
|
|
|
* |
110
|
|
|
* @param string $type |
111
|
|
|
* @return self |
112
|
|
|
*/ |
113
|
|
|
public function type( string $type ): self { |
114
|
|
|
$this->type = $type; |
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Sets the column length |
120
|
|
|
* |
121
|
|
|
* @param integer $length |
122
|
|
|
* @return self |
123
|
|
|
*/ |
124
|
|
|
public function length( int $length ): self { |
125
|
|
|
$this->length = $length; |
126
|
|
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Sets the column precision |
131
|
|
|
* |
132
|
|
|
* Only used for floating point numbers |
133
|
|
|
* |
134
|
|
|
* @param integer $precision |
135
|
|
|
* @return self |
136
|
|
|
*/ |
137
|
|
|
public function precision( int $precision ): self { |
138
|
|
|
$this->precision = $precision; |
139
|
|
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Denotes if the column is nullable |
144
|
|
|
* |
145
|
|
|
* @param boolean $nullable |
146
|
|
|
* @return self |
147
|
|
|
*/ |
148
|
|
|
public function nullable( bool $nullable = true ): self { |
149
|
|
|
$this->nullable = $nullable; |
150
|
|
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Sets the default value |
155
|
|
|
* |
156
|
|
|
* @param mixed $default |
157
|
|
|
* @return self |
158
|
|
|
*/ |
159
|
|
|
public function default( $default ): self { |
160
|
|
|
$this->default = $default; |
161
|
|
|
return $this; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Sets the default value |
166
|
|
|
* |
167
|
|
|
* @param boolean $auto_increment |
168
|
|
|
* @return self |
169
|
|
|
*/ |
170
|
|
|
public function auto_increment( bool $auto_increment = true ): self { |
171
|
|
|
$this->auto_increment = $auto_increment; |
172
|
|
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Denotes if the column is unsigned. |
177
|
|
|
* |
178
|
|
|
* @param boolean $unsigned |
179
|
|
|
* @return self |
180
|
|
|
*/ |
181
|
|
|
public function unsigned( bool $unsigned = true ): self { |
182
|
|
|
$this->unsigned = $unsigned; |
183
|
|
|
return $this; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Returns the column details as a stdClass |
188
|
|
|
* |
189
|
|
|
* @return object{ |
190
|
|
|
* name:string, |
191
|
|
|
* type:string, |
192
|
|
|
* length:int|null, |
193
|
|
|
* precision:int|null, |
194
|
|
|
* nullable:bool, |
195
|
|
|
* default:string|int|float, |
196
|
|
|
* unsigned:bool, |
197
|
|
|
* auto_increment:bool, |
198
|
|
|
*} |
199
|
|
|
*/ |
200
|
|
|
public function export() { |
201
|
|
|
return (object) array( |
202
|
|
|
'name' => $this->name, |
203
|
|
|
'type' => $this->type, |
204
|
|
|
'length' => $this->length, |
205
|
|
|
'precision' => $this->precision, |
206
|
|
|
'nullable' => $this->nullable ?? false, |
207
|
|
|
'default' => $this->default, |
208
|
|
|
'unsigned' => $this->unsigned ?? false, |
209
|
|
|
'auto_increment' => $this->auto_increment ?? false, |
210
|
|
|
); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Get column name |
215
|
|
|
* |
216
|
|
|
* @return string |
217
|
|
|
*/ |
218
|
|
|
public function get_name(): string { |
219
|
|
|
return $this->name; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Get the column type |
224
|
|
|
* |
225
|
|
|
* @return string|null |
226
|
|
|
*/ |
227
|
|
|
public function get_type(): ?string { |
228
|
|
|
return $this->type; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Get the column length |
233
|
|
|
* |
234
|
|
|
* @return int|null |
235
|
|
|
*/ |
236
|
|
|
public function get_length(): ?int { |
237
|
|
|
return $this->length; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Get used for floating point values |
242
|
|
|
* |
243
|
|
|
* @return int|null |
244
|
|
|
*/ |
245
|
|
|
public function get_precision(): ?int { |
246
|
|
|
return $this->precision; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Get denotes if the column is nullable |
251
|
|
|
* Returns false if column not set. |
252
|
|
|
* |
253
|
|
|
* @return bool |
254
|
|
|
*/ |
255
|
|
|
public function is_nullable(): bool { |
256
|
|
|
return $this->nullable ?? false; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Get the columns default value |
261
|
|
|
* |
262
|
|
|
* @return mixed|null |
263
|
|
|
*/ |
264
|
|
|
public function get_default() { |
265
|
|
|
return $this->default; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Get if the column has the auto increment flag. |
270
|
|
|
* False if not set. |
271
|
|
|
* |
272
|
|
|
* @return bool |
273
|
|
|
*/ |
274
|
|
|
public function is_auto_increment(): bool { |
275
|
|
|
return $this->auto_increment ?? false; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Get is the columns value unsigned |
280
|
|
|
* False if not set. |
281
|
|
|
* |
282
|
|
|
* @return bool |
283
|
|
|
*/ |
284
|
|
|
public function is_unsigned(): bool { |
285
|
|
|
return $this->unsigned ?? false; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
} |
289
|
|
|
|