Column::getValidOptions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 18
c 0
b 0
f 0
dl 0
loc 20
ccs 0
cts 0
cp 0
rs 9.6666
cc 1
nc 1
nop 0
crap 2
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 Phinx\Db\Adapter\AdapterInterface;
11
use RuntimeException;
12
13
/**
14
 * This object is based loosely on: http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/Table.html.
15
 */
16
class Column
17
{
18
    public const BIGINTEGER = AdapterInterface::PHINX_TYPE_BIG_INTEGER;
19
    public const SMALLINTEGER = AdapterInterface::PHINX_TYPE_SMALL_INTEGER;
20
    public const TINYINTEGER = AdapterInterface::PHINX_TYPE_TINY_INTEGER;
21
    public const BINARY = AdapterInterface::PHINX_TYPE_BINARY;
22
    public const BOOLEAN = AdapterInterface::PHINX_TYPE_BOOLEAN;
23
    public const CHAR = AdapterInterface::PHINX_TYPE_CHAR;
24
    public const DATE = AdapterInterface::PHINX_TYPE_DATE;
25
    public const DATETIME = AdapterInterface::PHINX_TYPE_DATETIME;
26
    public const DECIMAL = AdapterInterface::PHINX_TYPE_DECIMAL;
27
    public const FLOAT = AdapterInterface::PHINX_TYPE_FLOAT;
28
    public const INTEGER = AdapterInterface::PHINX_TYPE_INTEGER;
29
    public const STRING = AdapterInterface::PHINX_TYPE_STRING;
30
    public const TEXT = AdapterInterface::PHINX_TYPE_TEXT;
31
    public const TIME = AdapterInterface::PHINX_TYPE_TIME;
32
    public const TIMESTAMP = AdapterInterface::PHINX_TYPE_TIMESTAMP;
33
    public const UUID = AdapterInterface::PHINX_TYPE_UUID;
34
    public const BINARYUUID = AdapterInterface::PHINX_TYPE_BINARYUUID;
35
    /** MySQL-only column type */
36
    public const MEDIUMINTEGER = AdapterInterface::PHINX_TYPE_MEDIUM_INTEGER;
37
    /** MySQL-only column type */
38
    public const ENUM = AdapterInterface::PHINX_TYPE_ENUM;
39
    /** MySQL-only column type */
40
    public const SET = AdapterInterface::PHINX_TYPE_STRING;
41
    /** MySQL-only column type */
42
    public const BLOB = AdapterInterface::PHINX_TYPE_BLOB;
43
    /** MySQL-only column type */
44
    public const YEAR = AdapterInterface::PHINX_TYPE_YEAR;
45
    /** MySQL/Postgres-only column type */
46
    public const JSON = AdapterInterface::PHINX_TYPE_JSON;
47
    /** Postgres-only column type */
48
    public const JSONB = AdapterInterface::PHINX_TYPE_JSONB;
49
    /** Postgres-only column type */
50
    public const CIDR = AdapterInterface::PHINX_TYPE_CIDR;
51
    /** Postgres-only column type */
52
    public const INET = AdapterInterface::PHINX_TYPE_INET;
53
    /** Postgres-only column type */
54
    public const MACADDR = AdapterInterface::PHINX_TYPE_MACADDR;
55
    /** Postgres-only column type */
56
    public const INTERVAL = AdapterInterface::PHINX_TYPE_INTERVAL;
57
58
    /**
59
     * @var string
60
     */
61
    protected $name;
62
63
    /**
64
     * @var string|\Phinx\Util\Literal
65
     */
66
    protected $type;
67
68
    /**
69
     * @var int
70
     */
71
    protected $limit;
72
73
    /**
74
     * @var bool
75
     */
76
    protected $null = false;
77
78
    /**
79
     * @var mixed|null
80
     */
81
    protected $default;
82
83
    /**
84
     * @var bool
85
     */
86
    protected $identity = false;
87
88
    /**
89
     * @var int
90
     */
91
    protected $seed;
92
93
    /**
94
     * @var int
95
     */
96
    protected $increment;
97
98
    /**
99
     * @var int
100
     */
101
    protected $scale;
102
103
    /**
104
     * @var string
105
     */
106
    protected $after;
107
108
    /**
109
     * @var string
110
     */
111
    protected $update;
112
113
    /**
114
     * @var string
115
     */
116
    protected $comment;
117
118
    /**
119
     * @var bool
120
     */
121
    protected $signed = true;
122
123
    /**
124
     * @var bool
125
     */
126
    protected $timezone = false;
127
128
    /**
129
     * @var array
130 215
     */
131
    protected $properties = [];
132 215
133 215
    /**
134
     * @var string
135
     */
136
    protected $collation;
137
138
    /**
139
     * @var string
140
     */
141 212
    protected $encoding;
142
143 212
    /**
144
     * @var int|null
145
     */
146
    protected $srid;
147
148
    /**
149
     * @var array
150
     */
151
    protected $values;
152 214
153
    /**
154 214
     * Sets the column name.
155 214
     *
156
     * @param string $name Name
157
     * @return $this
158
     */
159
    public function setName($name)
160
    {
161
        $this->name = $name;
162
163 212
        return $this;
164
    }
165 212
166
    /**
167
     * Gets the column name.
168
     *
169
     * @return string|null
170
     */
171
    public function getName()
172
    {
173
        return $this->name;
174 192
    }
175
176 192
    /**
177 192
     * Sets the column type.
178
     *
179
     * @param string|\Phinx\Util\Literal $type Column type
180
     * @return $this
181
     */
182
    public function setType($type)
183
    {
184
        $this->type = $type;
185 192
186
        return $this;
187 192
    }
188
189
    /**
190
     * Gets the column type.
191
     *
192
     * @return string|\Phinx\Util\Literal
193
     */
194
    public function getType()
195
    {
196 208
        return $this->type;
197
    }
198 208
199 208
    /**
200
     * Sets the column limit.
201
     *
202
     * @param int $limit Limit
203
     * @return $this
204
     */
205
    public function setLimit($limit)
206
    {
207 215
        $this->limit = $limit;
208
209 215
        return $this;
210
    }
211
212
    /**
213
     * Gets the column limit.
214
     *
215
     * @return int
216
     */
217 214
    public function getLimit()
218
    {
219 214
        return $this->limit;
220
    }
221
222
    /**
223
     * Sets whether the column allows nulls.
224
     *
225
     * @param bool $null Null
226
     * @return $this
227
     */
228 207
    public function setNull($null)
229
    {
230 207
        $this->null = (bool)$null;
231 207
232
        return $this;
233
    }
234
235
    /**
236
     * Gets whether the column allows nulls.
237
     *
238
     * @return bool
239 215
     */
240
    public function getNull()
241 215
    {
242
        return $this->null;
243
    }
244
245
    /**
246
     * Does the column allow nulls?
247
     *
248
     * @return bool
249
     */
250 158
    public function isNull()
251
    {
252 158
        return $this->getNull();
253 158
    }
254
255
    /**
256
     * Sets the default column value.
257
     *
258
     * @param mixed $default Default
259
     * @return $this
260
     */
261 199
    public function setDefault($default)
262
    {
263 199
        $this->default = $default;
264
265
        return $this;
266
    }
267
268
    /**
269
     * Gets the default column value.
270
     *
271 198
     * @return mixed
272
     */
273 198
    public function getDefault()
274
    {
275
        return $this->default;
276
    }
277
278
    /**
279
     * Sets whether or not the column is an identity column.
280
     *
281
     * @param bool $identity Identity
282 1
     * @return $this
283
     */
284 1
    public function setIdentity($identity)
285 1
    {
286
        $this->identity = $identity;
287
288
        return $this;
289
    }
290
291
    /**
292
     * Gets whether or not the column is an identity column.
293 20
     *
294
     * @return bool
295 20
     */
296
    public function getIdentity()
297
    {
298
        return $this->identity;
299
    }
300
301
    /**
302
     * Is the column an identity column?
303
     *
304 15
     * @return bool
305
     */
306 15
    public function isIdentity()
307 15
    {
308
        return $this->getIdentity();
309
    }
310
311
    /**
312
     * Sets the name of the column to add this column after.
313
     *
314
     * @param string $after After
315 145
     * @return $this
316
     */
317 145
    public function setAfter($after)
318
    {
319
        $this->after = $after;
320
321
        return $this;
322
    }
323
324
    /**
325
     * Returns the name of the column to add this column after.
326 9
     *
327
     * @return string
328 9
     */
329 9
    public function getAfter()
330
    {
331
        return $this->after;
332
    }
333
334
    /**
335
     * Sets the 'ON UPDATE' mysql column function.
336
     *
337 131
     * @param string $update On Update function
338
     * @return $this
339 131
     */
340
    public function setUpdate($update)
341
    {
342
        $this->update = $update;
343
344
        return $this;
345
    }
346
347
    /**
348 9
     * Returns the value of the ON UPDATE column function.
349
     *
350 9
     * @return string
351 9
     */
352
    public function getUpdate()
353
    {
354
        return $this->update;
355
    }
356
357
    /**
358
     * Sets the number precision for decimal or float column.
359 2
     *
360
     * For example `DECIMAL(5,2)`, 5 is the precision and 2 is the scale,
361 2
     * and the column could store value from -999.99 to 999.99.
362
     *
363
     * @param int $precision Number precision
364
     * @return $this
365
     */
366
    public function setPrecision($precision)
367
    {
368
        $this->setLimit($precision);
369
370 9
        return $this;
371
    }
372 9
373 9
    /**
374
     * Gets the number precision for decimal or float column.
375
     *
376
     * For example `DECIMAL(5,2)`, 5 is the precision and 2 is the scale,
377
     * and the column could store value from -999.99 to 999.99.
378
     *
379
     * @return int
380
     */
381 198
    public function getPrecision()
382
    {
383 198
        return $this->limit;
384
    }
385
386
    /**
387
     * Gets the column identity seed.
388
     *
389
     * @return int
390
     */
391
    public function getSeed()
392 68
    {
393
        return $this->seed;
394 68
    }
395 68
396
    /**
397
     * Gets the column identity increment.
398
     *
399
     * @return int
400
     */
401
    public function getIncrement()
402
    {
403 89
        return $this->increment;
404
    }
405 89
406
    /**
407
     * Sets the column identity seed.
408
     *
409
     * @param int $seed Number seed
410
     * @return $this
411
     */
412
    public function setSeed($seed)
413 89
    {
414
        $this->seed = $seed;
415 89
416
        return $this;
417
    }
418
419
    /**
420
     * Sets the column identity increment.
421
     *
422
     * @param int $increment Number increment
423
     * @return $this
424
     */
425 1
    public function setIncrement($increment)
426
    {
427 1
        $this->increment = $increment;
428 1
429
        return $this;
430
    }
431
432
    /**
433
     * Sets the number scale for decimal or float column.
434
     *
435
     * For example `DECIMAL(5,2)`, 5 is the precision and 2 is the scale,
436 68
     * and the column could store value from -999.99 to 999.99.
437
     *
438 68
     * @param int $scale Number scale
439
     * @return $this
440
     */
441
    public function setScale($scale)
442
    {
443
        $this->scale = $scale;
444
445
        return $this;
446 68
    }
447
448 68
    /**
449
     * Gets the number scale for decimal or float column.
450
     *
451
     * For example `DECIMAL(5,2)`, 5 is the precision and 2 is the scale,
452
     * and the column could store value from -999.99 to 999.99.
453
     *
454
     * @return int
455
     */
456
    public function getScale()
457
    {
458
        return $this->scale;
459
    }
460
461
    /**
462
     * Sets the number precision and scale for decimal or float column.
463
     *
464
     * For example `DECIMAL(5,2)`, 5 is the precision and 2 is the scale,
465
     * and the column could store value from -999.99 to 999.99.
466
     *
467
     * @param int $precision Number precision
468
     * @param int $scale Number scale
469
     * @return $this
470
     */
471
    public function setPrecisionAndScale($precision, $scale)
472
    {
473
        $this->setLimit($precision);
474
        $this->scale = $scale;
475
476
        return $this;
477
    }
478
479
    /**
480
     * Sets the column comment.
481 9
     *
482
     * @param string $comment Comment
483 9
     * @return $this
484 2
     */
485 2
    public function setComment($comment)
486 9
    {
487 9
        $this->comment = $comment;
488
489
        return $this;
490
    }
491
492
    /**
493
     * Gets the column comment.
494
     *
495 131
     * @return string
496
     */
497 131
    public function getComment()
498
    {
499
        return $this->comment;
500
    }
501
502
    /**
503
     * Sets whether field should be signed.
504
     *
505
     * @param bool $signed Signed
506
     * @return $this
507
     */
508 1
    public function setSigned($signed)
509
    {
510
        $this->signed = (bool)$signed;
511 1
512 1
        return $this;
513 1
    }
514 1
515 1
    /**
516
     * Gets whether field should be signed.
517
     *
518
     * @return bool
519 1
     */
520
    public function getSigned()
521 1
    {
522
        return $this->signed;
523
    }
524
525
    /**
526
     * Should the column be signed?
527
     *
528
     * @return bool
529 89
     */
530
    public function isSigned()
531 89
    {
532
        return $this->getSigned();
533
    }
534
535
    /**
536
     * Sets whether the field should have a timezone identifier.
537
     * Used for date/time columns only!
538
     *
539
     * @param bool $timezone Timezone
540
     * @return $this
541
     */
542
    public function setTimezone($timezone)
543
    {
544
        $this->timezone = (bool)$timezone;
545
546
        return $this;
547
    }
548
549
    /**
550
     * Gets whether field has a timezone identifier.
551
     *
552
     * @return bool
553
     */
554
    public function getTimezone()
555
    {
556
        return $this->timezone;
557
    }
558
559
    /**
560
     * Should the column have a timezone?
561
     *
562
     * @return bool
563 89
     */
564
    public function isTimezone()
565 89
    {
566
        return $this->getTimezone();
567
    }
568
569
    /**
570
     * Sets field properties.
571
     *
572
     * @param array $properties Properties
573 209
     * @return $this
574
     */
575
    public function setProperties($properties)
576 209
    {
577 209
        $this->properties = $properties;
578 209
579 209
        return $this;
580 209
    }
581 209
582 209
    /**
583 209
     * Gets field properties
584 209
     *
585 209
     * @return array
586 209
     */
587 209
    public function getProperties()
588 209
    {
589 209
        return $this->properties;
590 209
    }
591 209
592
    /**
593
     * Sets field values.
594
     *
595
     * @param string[]|string $values Value(s)
596
     * @return $this
597
     */
598
    public function setValues($values)
599 209
    {
600
        if (!is_array($values)) {
601
            $values = preg_split('/,\s*/', $values);
602 209
        }
603 209
        $this->values = $values;
604
605
        return $this;
606
    }
607
608
    /**
609
     * Gets field values
610
     *
611
     * @return array
612 209
     */
613
    public function getValues()
614 209
    {
615 209
        return $this->values;
616
    }
617 209
618 208
    /**
619
     * Sets the column collation.
620
     *
621
     * @param string $collation Collation
622
     * @return $this
623 208
     */
624 1
    public function setCollation($collation)
625
    {
626
        $this->collation = $collation;
627 207
628 207
        return $this;
629 208
    }
630 208
631
    /**
632
     * Gets the column collation.
633
     *
634
     * @return string
635
     */
636
    public function getCollation()
637
    {
638
        return $this->collation;
639
    }
640
641
    /**
642
     * Sets the column character set.
643
     *
644
     * @param string $encoding Encoding
645
     * @return $this
646
     */
647
    public function setEncoding($encoding)
648
    {
649
        $this->encoding = $encoding;
650
651
        return $this;
652
    }
653
654
    /**
655
     * Gets the column character set.
656
     *
657
     * @return string
658
     */
659
    public function getEncoding()
660
    {
661
        return $this->encoding;
662
    }
663
664
    /**
665
     * Sets the column SRID.
666
     *
667
     * @param int $srid SRID
668
     * @return \Phinx\Db\Table\Column
669
     */
670
    public function setSrid($srid)
671
    {
672
        $this->srid = $srid;
673
674
        return $this;
675
    }
676
677
    /**
678
     * Gets the column SRID.
679
     *
680
     * @return int|null
681
     */
682
    public function getSrid()
683
    {
684
        return $this->srid;
685
    }
686
687
    /**
688
     * Gets all allowed options. Each option must have a corresponding `setFoo` method.
689
     *
690
     * @return array
691
     */
692
    protected function getValidOptions()
693
    {
694
        return [
695
            'limit',
696
            'default',
697
            'null',
698
            'identity',
699
            'scale',
700
            'after',
701
            'update',
702
            'comment',
703
            'signed',
704
            'timezone',
705
            'properties',
706
            'values',
707
            'collation',
708
            'encoding',
709
            'srid',
710
            'seed',
711
            'increment',
712
        ];
713
    }
714
715
    /**
716
     * Gets all aliased options. Each alias must reference a valid option.
717
     *
718
     * @return array
719
     */
720
    protected function getAliasedOptions()
721
    {
722
        return [
723
            'length' => 'limit',
724
            'precision' => 'limit',
725
        ];
726
    }
727
728
    /**
729
     * Utility method that maps an array of column options to this objects methods.
730
     *
731
     * @param array $options Options
732
     * @throws \RuntimeException
733
     * @return $this
734
     */
735
    public function setOptions($options)
736
    {
737
        $validOptions = $this->getValidOptions();
738
        $aliasOptions = $this->getAliasedOptions();
739
740
        foreach ($options as $option => $value) {
741
            if (isset($aliasOptions[$option])) {
742
                // proxy alias -> option
743
                $option = $aliasOptions[$option];
744
            }
745
746
            if (!in_array($option, $validOptions, true)) {
747
                throw new RuntimeException(sprintf('"%s" is not a valid column option.', $option));
748
            }
749
750
            $method = 'set' . ucfirst($option);
751
            $this->$method($value);
752
        }
753
754
        return $this;
755
    }
756
}
757