Completed
Push — master ( 814988...075c11 )
by mark
15s queued 11s
created

Column::getIncrement()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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
    /**
20
     * @var string
21
     */
22
    protected $name;
23
24
    /**
25
     * @var string|\Phinx\Util\Literal
26
     */
27
    protected $type;
28
29
    /**
30
     * @var int
31
     */
32
    protected $limit;
33
34
    /**
35
     * @var bool
36
     */
37
    protected $null = false;
38
39
    /**
40
     * @var mixed|null
41
     */
42
    protected $default;
43
44
    /**
45
     * @var bool
46
     */
47
    protected $identity = false;
48
49
    /**
50
     * @var int
51
     */
52
    protected $seed;
53
54
    /**
55
     * @var int
56
     */
57
    protected $increment;
58
59
    /**
60
     * @var int
61
     */
62
    protected $scale;
63
64
    /**
65
     * @var string
66
     */
67
    protected $after;
68
69
    /**
70
     * @var string
71
     */
72
    protected $update;
73
74
    /**
75
     * @var string
76
     */
77
    protected $comment;
78
79
    /**
80
     * @var bool
81
     */
82
    protected $signed = true;
83
84
    /**
85
     * @var bool
86
     */
87
    protected $timezone = false;
88
89
    /**
90
     * @var array
91
     */
92
    protected $properties = [];
93
94
    /**
95
     * @var string
96
     */
97
    protected $collation;
98
99
    /**
100
     * @var string
101
     */
102
    protected $encoding;
103
104
    /**
105
     * @var int|null
106
     */
107
    protected $srid;
108
109
    /**
110
     * @var array
111
     */
112
    protected $values;
113
114
    /**
115
     * Sets the column name.
116
     *
117
     * @param string $name
118
     *
119
     * @return $this
120
     */
121
    public function setName($name)
122
    {
123
        $this->name = $name;
124
125
        return $this;
126
    }
127
128
    /**
129
     * Gets the column name.
130 215
     *
131
     * @return string|null
132 215
     */
133 215
    public function getName()
134
    {
135
        return $this->name;
136
    }
137
138
    /**
139
     * Sets the column type.
140
     *
141 212
     * @param string|\Phinx\Util\Literal $type Column type
142
     *
143 212
     * @return $this
144
     */
145
    public function setType($type)
146
    {
147
        $this->type = $type;
148
149
        return $this;
150
    }
151
152 214
    /**
153
     * Gets the column type.
154 214
     *
155 214
     * @return string|\Phinx\Util\Literal
156
     */
157
    public function getType()
158
    {
159
        return $this->type;
160
    }
161
162
    /**
163 212
     * Sets the column limit.
164
     *
165 212
     * @param int $limit
166
     *
167
     * @return $this
168
     */
169
    public function setLimit($limit)
170
    {
171
        $this->limit = $limit;
172
173
        return $this;
174 192
    }
175
176 192
    /**
177 192
     * Gets the column limit.
178
     *
179
     * @return int
180
     */
181
    public function getLimit()
182
    {
183
        return $this->limit;
184
    }
185 192
186
    /**
187 192
     * Sets whether the column allows nulls.
188
     *
189
     * @param bool $null
190
     *
191
     * @return $this
192
     */
193
    public function setNull($null)
194
    {
195
        $this->null = (bool)$null;
196 208
197
        return $this;
198 208
    }
199 208
200
    /**
201
     * Gets whether the column allows nulls.
202
     *
203
     * @return bool
204
     */
205
    public function getNull()
206
    {
207 215
        return $this->null;
208
    }
209 215
210
    /**
211
     * Does the column allow nulls?
212
     *
213
     * @return bool
214
     */
215
    public function isNull()
216
    {
217 214
        return $this->getNull();
218
    }
219 214
220
    /**
221
     * Sets the default column value.
222
     *
223
     * @param mixed $default
224
     *
225
     * @return $this
226
     */
227
    public function setDefault($default)
228 207
    {
229
        $this->default = $default;
230 207
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 bool $identity
248
     *
249
     * @return $this
250 158
     */
251
    public function setIdentity($identity)
252 158
    {
253 158
        $this->identity = $identity;
254
255
        return $this;
256
    }
257
258
    /**
259
     * Gets whether or not the column is an identity column.
260
     *
261 199
     * @return bool
262
     */
263 199
    public function getIdentity()
264
    {
265
        return $this->identity;
266
    }
267
268
    /**
269
     * Is the column an identity column?
270
     *
271 198
     * @return bool
272
     */
273 198
    public function isIdentity()
274
    {
275
        return $this->getIdentity();
276
    }
277
278
    /**
279
     * Sets the name of the column to add this column after.
280
     *
281
     * @param string $after After
282 1
     *
283
     * @return $this
284 1
     */
285 1
    public function setAfter($after)
286
    {
287
        $this->after = $after;
288
289
        return $this;
290
    }
291
292
    /**
293 20
     * Returns the name of the column to add this column after.
294
     *
295 20
     * @return string
296
     */
297
    public function getAfter()
298
    {
299
        return $this->after;
300
    }
301
302
    /**
303
     * Sets the 'ON UPDATE' mysql column function.
304 15
     *
305
     * @param string $update On Update function
306 15
     *
307 15
     * @return $this
308
     */
309
    public function setUpdate($update)
310
    {
311
        $this->update = $update;
312
313
        return $this;
314
    }
315 145
316
    /**
317 145
     * Returns the value of the ON UPDATE column function.
318
     *
319
     * @return string
320
     */
321
    public function getUpdate()
322
    {
323
        return $this->update;
324
    }
325
326 9
    /**
327
     * Sets the number precision for decimal or float column.
328 9
     *
329 9
     * For example `DECIMAL(5,2)`, 5 is the precision and 2 is the scale,
330
     * and the column could store value from -999.99 to 999.99.
331
     *
332
     * @param int $precision Number precision
333
     *
334
     * @return $this
335
     */
336
    public function setPrecision($precision)
337 131
    {
338
        $this->setLimit($precision);
339 131
340
        return $this;
341
    }
342
343
    /**
344
     * Gets the number precision for decimal or float column.
345
     *
346
     * For example `DECIMAL(5,2)`, 5 is the precision and 2 is the scale,
347
     * and the column could store value from -999.99 to 999.99.
348 9
     *
349
     * @return int
350 9
     */
351 9
    public function getPrecision()
352
    {
353
        return $this->limit;
354
    }
355
356
    /**
357
     * Gets the column identity seed.
358
     *
359 2
     * @return int
360
     */
361 2
    public function getSeed()
362
    {
363
        return $this->seed;
364
    }
365
366
    /**
367
     * Gets the column identity increment.
368
     *
369
     * @return int
370 9
     */
371
    public function getIncrement()
372 9
    {
373 9
        return $this->increment;
374
    }
375
376
    /**
377
     * Sets the column identity seed.
378
     *
379
     * @param int $seed Number seed
380
     *
381 198
     * @return $this
382
     */
383 198
    public function setSeed($seed)
384
    {
385
        $this->seed = $seed;
386
387
        return $this;
388
    }
389
390
    /**
391
     * Sets the column identity increment.
392 68
     *
393
     * @param int $increment Number increment
394 68
     *
395 68
     * @return $this
396
     */
397
    public function setIncrement($increment)
398
    {
399
        $this->increment = $increment;
400
401
        return $this;
402
    }
403 89
404
    /**
405 89
     * Sets the number scale for decimal or float column.
406
     *
407
     * For example `DECIMAL(5,2)`, 5 is the precision and 2 is the scale,
408
     * and the column could store value from -999.99 to 999.99.
409
     *
410
     * @param int $scale Number scale
411
     *
412
     * @return $this
413 89
     */
414
    public function setScale($scale)
415 89
    {
416
        $this->scale = $scale;
417
418
        return $this;
419
    }
420
421
    /**
422
     * Gets the number scale for decimal or float column.
423
     *
424
     * For example `DECIMAL(5,2)`, 5 is the precision and 2 is the scale,
425 1
     * and the column could store value from -999.99 to 999.99.
426
     *
427 1
     * @return int
428 1
     */
429
    public function getScale()
430
    {
431
        return $this->scale;
432
    }
433
434
    /**
435
     * Sets the number precision and scale for decimal or float column.
436 68
     *
437
     * For example `DECIMAL(5,2)`, 5 is the precision and 2 is the scale,
438 68
     * and the column could store value from -999.99 to 999.99.
439
     *
440
     * @param int $precision Number precision
441
     * @param int $scale Number scale
442
     *
443
     * @return $this
444
     */
445
    public function setPrecisionAndScale($precision, $scale)
446 68
    {
447
        $this->setLimit($precision);
448 68
        $this->scale = $scale;
449
450
        return $this;
451
    }
452
453
    /**
454
     * Sets the column comment.
455
     *
456
     * @param string $comment
457
     *
458
     * @return $this
459
     */
460
    public function setComment($comment)
461
    {
462
        $this->comment = $comment;
463
464
        return $this;
465
    }
466
467
    /**
468
     * Gets the column comment.
469
     *
470
     * @return string
471
     */
472
    public function getComment()
473
    {
474
        return $this->comment;
475
    }
476
477
    /**
478
     * Sets whether field should be signed.
479
     *
480
     * @param bool $signed
481 9
     *
482
     * @return $this
483 9
     */
484 2
    public function setSigned($signed)
485 2
    {
486 9
        $this->signed = (bool)$signed;
487 9
488
        return $this;
489
    }
490
491
    /**
492
     * Gets whether field should be signed.
493
     *
494
     * @return bool
495 131
     */
496
    public function getSigned()
497 131
    {
498
        return $this->signed;
499
    }
500
501
    /**
502
     * Should the column be signed?
503
     *
504
     * @return bool
505
     */
506
    public function isSigned()
507
    {
508 1
        return $this->getSigned();
509
    }
510
511 1
    /**
512 1
     * Sets whether the field should have a timezone identifier.
513 1
     * Used for date/time columns only!
514 1
     *
515 1
     * @param bool $timezone
516
     *
517
     * @return $this
518
     */
519 1
    public function setTimezone($timezone)
520
    {
521 1
        $this->timezone = (bool)$timezone;
522
523
        return $this;
524
    }
525
526
    /**
527
     * Gets whether field has a timezone identifier.
528
     *
529 89
     * @return bool
530
     */
531 89
    public function getTimezone()
532
    {
533
        return $this->timezone;
534
    }
535
536
    /**
537
     * Should the column have a timezone?
538
     *
539
     * @return bool
540
     */
541
    public function isTimezone()
542
    {
543
        return $this->getTimezone();
544
    }
545
546
    /**
547
     * Sets field properties.
548
     *
549
     * @param array $properties
550
     *
551
     * @return $this
552
     */
553
    public function setProperties($properties)
554
    {
555
        $this->properties = $properties;
556
557
        return $this;
558
    }
559
560
    /**
561
     * Gets field properties
562
     *
563 89
     * @return array
564
     */
565 89
    public function getProperties()
566
    {
567
        return $this->properties;
568
    }
569
570
    /**
571
     * Sets field values.
572
     *
573 209
     * @param array|string $values
574
     *
575
     * @return $this
576 209
     */
577 209
    public function setValues($values)
578 209
    {
579 209
        if (!is_array($values)) {
580 209
            $values = preg_split('/,\s*/', $values);
581 209
        }
582 209
        $this->values = $values;
583 209
584 209
        return $this;
585 209
    }
586 209
587 209
    /**
588 209
     * Gets field values
589 209
     *
590 209
     * @return array
591 209
     */
592
    public function getValues()
593
    {
594
        return $this->values;
595
    }
596
597
    /**
598
     * Sets the column collation.
599 209
     *
600
     * @param string $collation
601
     *
602 209
     * @throws \UnexpectedValueException If collation not allowed for type
603 209
     *
604
     * @return $this
605
     */
606 View Code Duplication
    public function setCollation($collation)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
607
    {
608
        $allowedTypes = [
609
            AdapterInterface::PHINX_TYPE_CHAR,
610
            AdapterInterface::PHINX_TYPE_STRING,
611
            AdapterInterface::PHINX_TYPE_TEXT,
612 209
        ];
613
        if (!in_array($this->getType(), $allowedTypes)) {
614 209
            throw new UnexpectedValueException('Collation may be set only for types: ' . implode(', ', $allowedTypes));
615 209
        }
616
617 209
        $this->collation = $collation;
618 208
619
        return $this;
620
    }
621
622
    /**
623 208
     * Gets the column collation.
624 1
     *
625
     * @return string
626
     */
627 207
    public function getCollation()
628 207
    {
629 208
        return $this->collation;
630 208
    }
631
632
    /**
633
     * Sets the column character set.
634
     *
635
     * @param string $encoding
636
     *
637
     * @throws \UnexpectedValueException If character set not allowed for type
638
     *
639
     * @return $this
640
     */
641 View Code Duplication
    public function setEncoding($encoding)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
642
    {
643
        $allowedTypes = [
644
            AdapterInterface::PHINX_TYPE_CHAR,
645
            AdapterInterface::PHINX_TYPE_STRING,
646
            AdapterInterface::PHINX_TYPE_TEXT,
647
        ];
648
        if (!in_array($this->getType(), $allowedTypes)) {
649
            throw new UnexpectedValueException('Character set may be set only for types: ' . implode(', ', $allowedTypes));
650
        }
651
652
        $this->encoding = $encoding;
653
654
        return $this;
655
    }
656
657
    /**
658
     * Gets the column character set.
659
     *
660
     * @return string
661
     */
662
    public function getEncoding()
663
    {
664
        return $this->encoding;
665
    }
666
667
    /**
668
     * Sets the column SRID.
669
     *
670
     * @param int $srid SRID
671
     * @return \Phinx\Db\Table\Column
672
     */
673
    public function setSrid($srid)
674
    {
675
        $this->srid = $srid;
676
677
        return $this;
678
    }
679
680
    /**
681
     * Gets the column SRID.
682
     *
683
     * @return int|null
684
     */
685
    public function getSrid()
686
    {
687
        return $this->srid;
688
    }
689
690
    /**
691
     * Gets all allowed options. Each option must have a corresponding `setFoo` method.
692
     *
693
     * @return array
694
     */
695
    protected function getValidOptions()
696
    {
697
        return [
698
            'limit',
699
            'default',
700
            'null',
701
            'identity',
702
            'scale',
703
            'after',
704
            'update',
705
            'comment',
706
            'signed',
707
            'timezone',
708
            'properties',
709
            'values',
710
            'collation',
711
            'encoding',
712
            'srid',
713
            'seed',
714
            'increment',
715
        ];
716
    }
717
718
    /**
719
     * Gets all aliased options. Each alias must reference a valid option.
720
     *
721
     * @return array
722
     */
723
    protected function getAliasedOptions()
724
    {
725
        return [
726
            'length' => 'limit',
727
            'precision' => 'limit',
728
        ];
729
    }
730
731
    /**
732
     * Utility method that maps an array of column options to this objects methods.
733
     *
734
     * @param array $options Options
735
     *
736
     * @throws \RuntimeException
737
     *
738
     * @return $this
739
     */
740
    public function setOptions($options)
741
    {
742
        $validOptions = $this->getValidOptions();
743
        $aliasOptions = $this->getAliasedOptions();
744
745
        foreach ($options as $option => $value) {
746
            if (isset($aliasOptions[$option])) {
747
                // proxy alias -> option
748
                $option = $aliasOptions[$option];
749
            }
750
751
            if (!in_array($option, $validOptions, true)) {
752
                throw new RuntimeException(sprintf('"%s" is not a valid column option.', $option));
753
            }
754
755
            $method = 'set' . ucfirst($option);
756
            $this->$method($value);
757
        }
758
759
        return $this;
760
    }
761
}
762