1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Phinx |
4
|
|
|
* |
5
|
|
|
* (The MIT license) |
6
|
|
|
* Copyright (c) 2015 Rob Morgan |
7
|
|
|
* |
8
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
9
|
|
|
* of this software and associated * documentation files (the "Software"), to |
10
|
|
|
* deal in the Software without restriction, including without limitation the |
11
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
12
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is |
13
|
|
|
* furnished to do so, subject to the following conditions: |
14
|
|
|
* |
15
|
|
|
* The above copyright notice and this permission notice shall be included in |
16
|
|
|
* all copies or substantial portions of the Software. |
17
|
|
|
* |
18
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
19
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
20
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
21
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
22
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
23
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
24
|
|
|
* IN THE SOFTWARE. |
25
|
|
|
* |
26
|
|
|
* @package Phinx |
27
|
|
|
* @subpackage Phinx\Db |
28
|
|
|
*/ |
29
|
|
|
namespace Phinx\Db\Table; |
30
|
|
|
|
31
|
|
|
use Phinx\Db\Adapter\AdapterInterface; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* |
35
|
|
|
* This object is based loosely on: http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/Table.html. |
36
|
|
|
*/ |
37
|
|
|
class Column |
38
|
|
|
{ |
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $name; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
protected $type; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var integer |
51
|
|
|
*/ |
52
|
|
|
protected $limit = null; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var boolean |
56
|
|
|
*/ |
57
|
|
|
protected $null = false; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var mixed |
61
|
|
|
*/ |
62
|
|
|
protected $default = null; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var boolean |
66
|
|
|
*/ |
67
|
|
|
protected $identity = false; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @var integer |
71
|
|
|
*/ |
72
|
|
|
protected $precision; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @var integer |
76
|
|
|
*/ |
77
|
|
|
protected $scale; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @var string |
81
|
|
|
*/ |
82
|
|
|
protected $after; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @var string |
86
|
|
|
*/ |
87
|
|
|
protected $update; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @var string |
91
|
|
|
*/ |
92
|
|
|
protected $comment; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @var boolean |
96
|
|
|
*/ |
97
|
|
|
protected $signed = true; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @var boolean |
101
|
|
|
*/ |
102
|
|
|
protected $timezone = false; |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @var array |
106
|
|
|
*/ |
107
|
|
|
protected $properties = []; |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @var string |
111
|
|
|
*/ |
112
|
|
|
protected $collation; |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @var string |
116
|
|
|
*/ |
117
|
|
|
protected $encoding; |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @var array |
121
|
|
|
*/ |
122
|
|
|
protected $values; |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Sets the column name. |
126
|
|
|
* |
127
|
|
|
* @param string $name |
128
|
|
|
* @return Column |
129
|
|
|
*/ |
130
|
215 |
|
public function setName($name) |
131
|
|
|
{ |
132
|
215 |
|
$this->name = $name; |
133
|
215 |
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Gets the column name. |
138
|
|
|
* |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
212 |
|
public function getName() |
142
|
|
|
{ |
143
|
212 |
|
return $this->name; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Sets the column type. |
148
|
|
|
* |
149
|
|
|
* @param string $type |
150
|
|
|
* @return Column |
151
|
|
|
*/ |
152
|
214 |
|
public function setType($type) |
153
|
|
|
{ |
154
|
214 |
|
$this->type = $type; |
155
|
214 |
|
return $this; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Gets the column type. |
160
|
|
|
* |
161
|
|
|
* @return string |
162
|
|
|
*/ |
163
|
212 |
|
public function getType() |
164
|
|
|
{ |
165
|
212 |
|
return $this->type; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Sets the column limit. |
170
|
|
|
* |
171
|
|
|
* @param integer $limit |
172
|
|
|
* @return Column |
173
|
|
|
*/ |
174
|
192 |
|
public function setLimit($limit) |
175
|
|
|
{ |
176
|
192 |
|
$this->limit = $limit; |
177
|
192 |
|
return $this; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Gets the column limit. |
182
|
|
|
* |
183
|
|
|
* @return integer |
184
|
|
|
*/ |
185
|
192 |
|
public function getLimit() |
186
|
|
|
{ |
187
|
192 |
|
return $this->limit; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Sets whether the column allows nulls. |
192
|
|
|
* |
193
|
|
|
* @param boolean $null |
194
|
|
|
* @return Column |
195
|
|
|
*/ |
196
|
208 |
|
public function setNull($null) |
197
|
|
|
{ |
198
|
208 |
|
$this->null = (bool) $null; |
199
|
208 |
|
return $this; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Gets whether the column allows nulls. |
204
|
|
|
* |
205
|
|
|
* @return boolean |
206
|
|
|
*/ |
207
|
215 |
|
public function getNull() |
208
|
|
|
{ |
209
|
215 |
|
return $this->null; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Does the column allow nulls? |
214
|
|
|
* |
215
|
|
|
* @return boolean |
216
|
|
|
*/ |
217
|
214 |
|
public function isNull() |
218
|
|
|
{ |
219
|
214 |
|
return $this->getNull(); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Sets the default column value. |
224
|
|
|
* |
225
|
|
|
* @param mixed $default |
226
|
|
|
* @return Column |
227
|
|
|
*/ |
228
|
207 |
|
public function setDefault($default) |
229
|
|
|
{ |
230
|
207 |
|
$this->default = $default; |
231
|
207 |
|
return $this; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Gets the default column value. |
236
|
|
|
* |
237
|
|
|
* @return mixed |
238
|
|
|
*/ |
239
|
215 |
|
public function getDefault() |
240
|
|
|
{ |
241
|
215 |
|
return $this->default; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Sets whether or not the column is an identity column. |
246
|
|
|
* |
247
|
|
|
* @param boolean $identity |
248
|
|
|
* @return Column |
249
|
|
|
*/ |
250
|
158 |
|
public function setIdentity($identity) |
251
|
|
|
{ |
252
|
158 |
|
$this->identity = $identity; |
253
|
158 |
|
return $this; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Gets whether or not the column is an identity column. |
258
|
|
|
* |
259
|
|
|
* @return boolean |
260
|
|
|
*/ |
261
|
199 |
|
public function getIdentity() |
262
|
|
|
{ |
263
|
199 |
|
return $this->identity; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Is the column an identity column? |
268
|
|
|
* |
269
|
|
|
* @return boolean |
270
|
|
|
*/ |
271
|
198 |
|
public function isIdentity() |
272
|
|
|
{ |
273
|
198 |
|
return $this->getIdentity(); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Sets the name of the column to add this column after. |
278
|
|
|
* |
279
|
|
|
* @param string $after After |
280
|
|
|
* @return Column |
281
|
|
|
*/ |
282
|
1 |
|
public function setAfter($after) |
283
|
|
|
{ |
284
|
1 |
|
$this->after = $after; |
285
|
1 |
|
return $this; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Returns the name of the column to add this column after. |
290
|
|
|
* |
291
|
|
|
* @return string |
292
|
|
|
*/ |
293
|
20 |
|
public function getAfter() |
294
|
|
|
{ |
295
|
20 |
|
return $this->after; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Sets the 'ON UPDATE' mysql column function. |
300
|
|
|
* |
301
|
|
|
* @param string $update On Update function |
302
|
|
|
* @return Column |
303
|
|
|
*/ |
304
|
15 |
|
public function setUpdate($update) |
305
|
|
|
{ |
306
|
15 |
|
$this->update = $update; |
307
|
15 |
|
return $this; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Returns the value of the ON UPDATE column function. |
312
|
|
|
* |
313
|
|
|
* @return string |
314
|
|
|
*/ |
315
|
145 |
|
public function getUpdate() |
316
|
|
|
{ |
317
|
145 |
|
return $this->update; |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* Sets the column precision for decimal. |
322
|
|
|
* |
323
|
|
|
* @param integer $precision |
324
|
|
|
* @return Column |
325
|
|
|
*/ |
326
|
9 |
|
public function setPrecision($precision) |
327
|
|
|
{ |
328
|
9 |
|
$this->precision = $precision; |
329
|
9 |
|
return $this; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* Gets the column precision for decimal. |
334
|
|
|
* |
335
|
|
|
* @return integer |
336
|
|
|
*/ |
337
|
131 |
|
public function getPrecision() |
338
|
|
|
{ |
339
|
131 |
|
return $this->precision; |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* Sets the column scale for decimal. |
344
|
|
|
* |
345
|
|
|
* @param integer $scale |
346
|
|
|
* @return Column |
347
|
|
|
*/ |
348
|
9 |
|
public function setScale($scale) |
349
|
|
|
{ |
350
|
9 |
|
$this->scale = $scale; |
351
|
9 |
|
return $this; |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* Gets the column scale for decimal. |
356
|
|
|
* |
357
|
|
|
* @return integer |
358
|
|
|
*/ |
359
|
2 |
|
public function getScale() |
360
|
|
|
{ |
361
|
2 |
|
return $this->scale; |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* Sets the column comment. |
366
|
|
|
* |
367
|
|
|
* @param string $comment |
368
|
|
|
* @return Column |
369
|
|
|
*/ |
370
|
9 |
|
public function setComment($comment) |
371
|
|
|
{ |
372
|
9 |
|
$this->comment = $comment; |
373
|
9 |
|
return $this; |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* Gets the column comment. |
378
|
|
|
* |
379
|
|
|
* @return string |
380
|
|
|
*/ |
381
|
198 |
|
public function getComment() |
382
|
|
|
{ |
383
|
198 |
|
return $this->comment; |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
/** |
387
|
|
|
* Sets whether field should be signed. |
388
|
|
|
* |
389
|
|
|
* @param bool $signed |
390
|
|
|
* @return Column |
391
|
|
|
*/ |
392
|
68 |
|
public function setSigned($signed) |
393
|
|
|
{ |
394
|
68 |
|
$this->signed = (bool) $signed; |
395
|
68 |
|
return $this; |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* Gets whether field should be signed. |
400
|
|
|
* |
401
|
|
|
* @return boolean |
402
|
|
|
*/ |
403
|
89 |
|
public function getSigned() |
404
|
|
|
{ |
405
|
89 |
|
return $this->signed; |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
/** |
409
|
|
|
* Should the column be signed? |
410
|
|
|
* |
411
|
|
|
* @return boolean |
412
|
|
|
*/ |
413
|
89 |
|
public function isSigned() |
414
|
|
|
{ |
415
|
89 |
|
return $this->getSigned(); |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
/** |
419
|
|
|
* Sets whether the field should have a timezone identifier. |
420
|
|
|
* Used for date/time columns only! |
421
|
|
|
* |
422
|
|
|
* @param bool $timezone |
423
|
|
|
* @return Column |
424
|
|
|
*/ |
425
|
1 |
|
public function setTimezone($timezone) |
426
|
|
|
{ |
427
|
1 |
|
$this->timezone = (bool) $timezone; |
428
|
1 |
|
return $this; |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
/** |
432
|
|
|
* Gets whether field has a timezone identifier. |
433
|
|
|
* |
434
|
|
|
* @return boolean |
435
|
|
|
*/ |
436
|
68 |
|
public function getTimezone() |
437
|
|
|
{ |
438
|
68 |
|
return $this->timezone; |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
/** |
442
|
|
|
* Should the column have a timezone? |
443
|
|
|
* |
444
|
|
|
* @return boolean |
445
|
|
|
*/ |
446
|
68 |
|
public function isTimezone() |
447
|
|
|
{ |
448
|
68 |
|
return $this->getTimezone(); |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
/** |
452
|
|
|
* Sets field properties. |
453
|
|
|
* |
454
|
|
|
* @param array $properties |
455
|
|
|
* |
456
|
|
|
* @return Column |
457
|
|
|
*/ |
458
|
|
|
public function setProperties($properties) |
459
|
|
|
{ |
460
|
|
|
$this->properties = $properties; |
461
|
|
|
return $this; |
462
|
|
|
} |
463
|
|
|
|
464
|
|
|
/** |
465
|
|
|
* Gets field properties |
466
|
|
|
* |
467
|
|
|
* @return array |
468
|
|
|
*/ |
469
|
|
|
public function getProperties() |
470
|
|
|
{ |
471
|
|
|
return $this->properties; |
472
|
|
|
} |
473
|
|
|
|
474
|
|
|
/** |
475
|
|
|
* Sets field values. |
476
|
|
|
* |
477
|
|
|
* @param mixed (array|string) $values |
478
|
|
|
* |
479
|
|
|
* @return Column |
480
|
|
|
*/ |
481
|
9 |
|
public function setValues($values) |
482
|
|
|
{ |
483
|
9 |
|
if (!is_array($values)) { |
484
|
2 |
|
$values = preg_split('/,\s*/', $values); |
485
|
2 |
|
} |
486
|
9 |
|
$this->values = $values; |
487
|
9 |
|
return $this; |
488
|
|
|
} |
489
|
|
|
|
490
|
|
|
/** |
491
|
|
|
* Gets field values |
492
|
|
|
* |
493
|
|
|
* @return array |
494
|
|
|
*/ |
495
|
131 |
|
public function getValues() |
496
|
|
|
{ |
497
|
131 |
|
return $this->values; |
498
|
|
|
} |
499
|
|
|
|
500
|
|
|
/** |
501
|
|
|
* Sets the column collation. |
502
|
|
|
* |
503
|
|
|
* @param string $collation |
504
|
|
|
* |
505
|
|
|
* @throws \UnexpectedValueException If collation not allowed for type |
506
|
|
|
* @return $this |
507
|
|
|
*/ |
508
|
1 |
View Code Duplication |
public function setCollation($collation) |
|
|
|
|
509
|
|
|
{ |
510
|
|
|
$allowedTypes = [ |
511
|
1 |
|
AdapterInterface::PHINX_TYPE_CHAR, |
512
|
1 |
|
AdapterInterface::PHINX_TYPE_STRING, |
513
|
1 |
|
AdapterInterface::PHINX_TYPE_TEXT, |
514
|
1 |
|
]; |
515
|
1 |
|
if (!in_array($this->getType(), $allowedTypes)) { |
516
|
|
|
throw new \UnexpectedValueException('Collation may be set only for types: ' . implode(', ', $allowedTypes)); |
517
|
|
|
} |
518
|
|
|
|
519
|
1 |
|
$this->collation = $collation; |
520
|
|
|
|
521
|
1 |
|
return $this; |
522
|
|
|
} |
523
|
|
|
|
524
|
|
|
/** |
525
|
|
|
* Gets the column collation. |
526
|
|
|
* |
527
|
|
|
* @return string |
528
|
|
|
*/ |
529
|
89 |
|
public function getCollation() |
530
|
|
|
{ |
531
|
89 |
|
return $this->collation; |
532
|
|
|
} |
533
|
|
|
|
534
|
|
|
/** |
535
|
|
|
* Sets the column character set. |
536
|
|
|
* |
537
|
|
|
* @param string $encoding |
538
|
|
|
* |
539
|
|
|
* @throws \UnexpectedValueException If character set not allowed for type |
540
|
|
|
* @return $this |
541
|
|
|
*/ |
542
|
|
View Code Duplication |
public function setEncoding($encoding) |
|
|
|
|
543
|
|
|
{ |
544
|
|
|
$allowedTypes = [ |
545
|
|
|
AdapterInterface::PHINX_TYPE_CHAR, |
546
|
|
|
AdapterInterface::PHINX_TYPE_STRING, |
547
|
|
|
AdapterInterface::PHINX_TYPE_TEXT, |
548
|
|
|
]; |
549
|
|
|
if (!in_array($this->getType(), $allowedTypes)) { |
550
|
|
|
throw new \UnexpectedValueException('Character set may be set only for types: ' . implode(', ', $allowedTypes)); |
551
|
|
|
} |
552
|
|
|
|
553
|
|
|
$this->encoding = $encoding; |
554
|
|
|
|
555
|
|
|
return $this; |
556
|
|
|
} |
557
|
|
|
|
558
|
|
|
/** |
559
|
|
|
* Gets the column character set. |
560
|
|
|
* |
561
|
|
|
* @return string |
562
|
|
|
*/ |
563
|
89 |
|
public function getEncoding() |
564
|
|
|
{ |
565
|
89 |
|
return $this->encoding; |
566
|
|
|
} |
567
|
|
|
|
568
|
|
|
/** |
569
|
|
|
* Gets all allowed options. Each option must have a corresponding `setFoo` method. |
570
|
|
|
* |
571
|
|
|
* @return array |
572
|
|
|
*/ |
573
|
209 |
|
protected function getValidOptions() |
574
|
|
|
{ |
575
|
|
|
return [ |
576
|
209 |
|
'limit', |
577
|
209 |
|
'default', |
578
|
209 |
|
'null', |
579
|
209 |
|
'identity', |
580
|
209 |
|
'precision', |
581
|
209 |
|
'scale', |
582
|
209 |
|
'after', |
583
|
209 |
|
'update', |
584
|
209 |
|
'comment', |
585
|
209 |
|
'signed', |
586
|
209 |
|
'timezone', |
587
|
209 |
|
'properties', |
588
|
209 |
|
'values', |
589
|
209 |
|
'collation', |
590
|
209 |
|
'encoding', |
591
|
209 |
|
]; |
592
|
|
|
} |
593
|
|
|
|
594
|
|
|
/** |
595
|
|
|
* Gets all aliased options. Each alias must reference a valid option. |
596
|
|
|
* |
597
|
|
|
* @return array |
598
|
|
|
*/ |
599
|
209 |
|
protected function getAliasedOptions() |
600
|
|
|
{ |
601
|
|
|
return [ |
602
|
209 |
|
'length' => 'limit', |
603
|
209 |
|
]; |
604
|
|
|
} |
605
|
|
|
|
606
|
|
|
/** |
607
|
|
|
* Utility method that maps an array of column options to this objects methods. |
608
|
|
|
* |
609
|
|
|
* @param array $options Options |
610
|
|
|
* @return Column |
611
|
|
|
*/ |
612
|
209 |
|
public function setOptions($options) |
613
|
|
|
{ |
614
|
209 |
|
$validOptions = $this->getValidOptions(); |
615
|
209 |
|
$aliasOptions = $this->getAliasedOptions(); |
616
|
|
|
|
617
|
209 |
|
foreach ($options as $option => $value) { |
618
|
208 |
|
if (isset($aliasOptions[$option])) { |
619
|
|
|
// proxy alias -> option |
620
|
|
|
$option = $aliasOptions[$option]; |
621
|
|
|
} |
622
|
|
|
|
623
|
208 |
|
if (!in_array($option, $validOptions, true)) { |
624
|
1 |
|
throw new \RuntimeException(sprintf('"%s" is not a valid column option.', $option)); |
625
|
|
|
} |
626
|
|
|
|
627
|
207 |
|
$method = 'set' . ucfirst($option); |
628
|
207 |
|
$this->$method($value); |
629
|
208 |
|
} |
630
|
208 |
|
return $this; |
631
|
|
|
} |
632
|
|
|
} |
633
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.