Completed
Push — master ( 05902f...9e6322 )
by mark
01:41 queued 11s
created

Column::isNull()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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