|
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 \stdClass |
|
190
|
|
|
* @return-shape object{ |
|
191
|
|
|
* name:string, |
|
192
|
|
|
* type:string, |
|
193
|
|
|
* length:int|null, |
|
194
|
|
|
* precision:int|null, |
|
195
|
|
|
* nullable:bool, |
|
196
|
|
|
* default:string|int|float, |
|
197
|
|
|
* unsigned:bool, |
|
198
|
|
|
* auto_increment:bool, |
|
199
|
|
|
*} |
|
200
|
|
|
*/ |
|
201
|
|
|
public function export(): \stdClass { |
|
202
|
|
|
return (object) array( |
|
203
|
|
|
'name' => $this->name, |
|
204
|
|
|
'type' => $this->type, |
|
205
|
|
|
'length' => $this->length, |
|
206
|
|
|
'precision' => $this->precision, |
|
207
|
|
|
'nullable' => $this->nullable ?? false, |
|
208
|
|
|
'default' => $this->default, |
|
209
|
|
|
'unsigned' => $this->unsigned ?? false, |
|
210
|
|
|
'auto_increment' => $this->auto_increment ?? false, |
|
211
|
|
|
); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* Get column name |
|
216
|
|
|
* |
|
217
|
|
|
* @return string |
|
218
|
|
|
*/ |
|
219
|
|
|
public function get_name(): string { |
|
220
|
|
|
return $this->name; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* Get the column type |
|
225
|
|
|
* |
|
226
|
|
|
* @return string|null |
|
227
|
|
|
*/ |
|
228
|
|
|
public function get_type(): ?string { |
|
229
|
|
|
return $this->type; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Get the column length |
|
234
|
|
|
* |
|
235
|
|
|
* @return int|null |
|
236
|
|
|
*/ |
|
237
|
|
|
public function get_length(): ?int { |
|
238
|
|
|
return $this->length; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* Get used for floating point values |
|
243
|
|
|
* |
|
244
|
|
|
* @return int|null |
|
245
|
|
|
*/ |
|
246
|
|
|
public function get_precision(): ?int { |
|
247
|
|
|
return $this->precision; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Get denotes if the column is nullable |
|
252
|
|
|
* Returns false if column not set. |
|
253
|
|
|
* |
|
254
|
|
|
* @return bool |
|
255
|
|
|
*/ |
|
256
|
|
|
public function is_nullable(): bool { |
|
257
|
|
|
return $this->nullable ?? false; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* Get the columns default value |
|
262
|
|
|
* |
|
263
|
|
|
* @return mixed|null |
|
264
|
|
|
*/ |
|
265
|
|
|
public function get_default() { |
|
266
|
|
|
return $this->default; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* Get if the column has the auto increment flag. |
|
271
|
|
|
* False if not set. |
|
272
|
|
|
* |
|
273
|
|
|
* @return bool |
|
274
|
|
|
*/ |
|
275
|
|
|
public function is_auto_increment(): bool { |
|
276
|
|
|
return $this->auto_increment ?? false; |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
/** |
|
280
|
|
|
* Get is the columns value unsigned |
|
281
|
|
|
* False if not set. |
|
282
|
|
|
* |
|
283
|
|
|
* @return bool |
|
284
|
|
|
*/ |
|
285
|
|
|
public function is_unsigned(): bool { |
|
286
|
|
|
return $this->unsigned ?? false; |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
} |
|
290
|
|
|
|