Completed
Pull Request — master (#1641)
by
unknown
01:31
created

Column::getIncrement()   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
dl 0
loc 4
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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|\Phinx\Util\Literal
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 $scale;
73
74
    /**
75
     * @var string
76
     */
77
    protected $after;
78
79
    /**
80
     * @var string
81
     */
82
    protected $update;
83
84
    /**
85
     * @var string
86
     */
87
    protected $comment;
88
89
    /**
90
     * @var boolean
91
     */
92
    protected $signed = true;
93
94
    /**
95
     * @var boolean
96
     */
97
    protected $timezone = false;
98
99
    /**
100
     * @var array
101
     */
102
    protected $properties = [];
103
104
    /**
105
     * @var string
106
     */
107
    protected $collation;
108
109
    /**
110
     * @var string
111
     */
112
    protected $encoding;
113
114
    /**
115
     * @var array
116
     */
117
    protected $values;
118
119
    /**
120
     * Sets the column name.
121
     *
122
     * @param string $name
123
     * @return \Phinx\Db\Table\Column
124
     */
125
    public function setName($name)
126
    {
127
        $this->name = $name;
128
129
        return $this;
130 215
    }
131
132 215
    /**
133 215
     * Gets the column name.
134
     *
135
     * @return string|null
136
     */
137
    public function getName()
138
    {
139
        return $this->name;
140
    }
141 212
142
    /**
143 212
     * Sets the column type.
144
     *
145
     * @param string|\Phinx\Util\Literal $type Column type
146
     * @return \Phinx\Db\Table\Column
147
     */
148
    public function setType($type)
149
    {
150
        $this->type = $type;
151
152 214
        return $this;
153
    }
154 214
155 214
    /**
156
     * Gets the column type.
157
     *
158
     * @return string|\Phinx\Util\Literal
159
     */
160
    public function getType()
161
    {
162
        return $this->type;
163 212
    }
164
165 212
    /**
166
     * Sets the column limit.
167
     *
168
     * @param int $limit
169
     * @return \Phinx\Db\Table\Column
170
     */
171
    public function setLimit($limit)
172
    {
173
        $this->limit = $limit;
174 192
175
        return $this;
176 192
    }
177 192
178
    /**
179
     * Gets the column limit.
180
     *
181
     * @return int
182
     */
183
    public function getLimit()
184
    {
185 192
        return $this->limit;
186
    }
187 192
188
    /**
189
     * Sets whether the column allows nulls.
190
     *
191
     * @param bool $null
192
     * @return \Phinx\Db\Table\Column
193
     */
194
    public function setNull($null)
195
    {
196 208
        $this->null = (bool)$null;
197
198 208
        return $this;
199 208
    }
200
201
    /**
202
     * Gets whether the column allows nulls.
203
     *
204
     * @return bool
205
     */
206
    public function getNull()
207 215
    {
208
        return $this->null;
209 215
    }
210
211
    /**
212
     * Does the column allow nulls?
213
     *
214
     * @return bool
215
     */
216
    public function isNull()
217 214
    {
218
        return $this->getNull();
219 214
    }
220
221
    /**
222
     * Sets the default column value.
223
     *
224
     * @param mixed $default
225
     * @return \Phinx\Db\Table\Column
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
     * @return \Phinx\Db\Table\Column
249
     */
250 158
    public function setIdentity($identity)
251
    {
252 158
        $this->identity = $identity;
253 158
254
        return $this;
255
    }
256
257
    /**
258
     * Gets whether or not the column is an identity column.
259
     *
260
     * @return bool
261 199
     */
262
    public function getIdentity()
263 199
    {
264
        return $this->identity;
265
    }
266
267
    /**
268
     * Is the column an identity column?
269
     *
270
     * @return bool
271 198
     */
272
    public function isIdentity()
273 198
    {
274
        return $this->getIdentity();
275
    }
276
277
    /**
278
     * Sets the name of the column to add this column after.
279
     *
280
     * @param string $after After
281
     * @return \Phinx\Db\Table\Column
282 1
     */
283
    public function setAfter($after)
284 1
    {
285 1
        $this->after = $after;
286
287
        return $this;
288
    }
289
290
    /**
291
     * Returns the name of the column to add this column after.
292
     *
293 20
     * @return string
294
     */
295 20
    public function getAfter()
296
    {
297
        return $this->after;
298
    }
299
300
    /**
301
     * Sets the 'ON UPDATE' mysql column function.
302
     *
303
     * @param  string $update On Update function
304 15
     * @return \Phinx\Db\Table\Column
305
     */
306 15
    public function setUpdate($update)
307 15
    {
308
        $this->update = $update;
309
310
        return $this;
311
    }
312
313
    /**
314
     * Returns the value of the ON UPDATE column function.
315 145
     *
316
     * @return string
317 145
     */
318
    public function getUpdate()
319
    {
320
        return $this->update;
321
    }
322
323
    /**
324
     * Sets the number precision for decimal or float column.
325
     *
326 9
     * For example `DECIMAL(5,2)`, 5 is the precision and 2 is the scale,
327
     * and the column could store value from -999.99 to 999.99.
328 9
     *
329 9
     * @param int $precision Number precision
330
     * @return \Phinx\Db\Table\Column
331
     */
332
    public function setPrecision($precision)
333
    {
334
        $this->setLimit($precision);
335
336
        return $this;
337 131
    }
338
339 131
    /**
340
     * Gets the number precision for decimal or float column.
341
     *
342
     * For example `DECIMAL(5,2)`, 5 is the precision and 2 is the scale,
343
     * and the column could store value from -999.99 to 999.99.
344
     *
345
     * @return int
346
     */
347
    public function getPrecision()
348 9
    {
349
        return $this->limit;
350 9
    }
351 9
352
      /**
353
     * Gets the column identity seed.
354
     *
355
     * @return int
356
     */
357
    public function getSeed()
358
    {
359 2
        return $this->seed;
0 ignored issues
show
Bug introduced by
The property seed does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
360
    }
361 2
362
  /**
363
     * Gets the column identity increment.
364
     *
365
     * @return int
366
     */
367
    public function getIncrement()
368
    {
369
        return $this->increment;
0 ignored issues
show
Bug introduced by
The property increment does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
370 9
    }
371
372 9
  /**
373 9
     * Sets the column identity seed.
374
     *
375
     * @param int $seed
376
     * @return \Phinx\Db\Table\Column
377
     */
378
    public function setSeed($seed)
379
    {
380
        $this->seed = $seed;
381 198
        return $this;
382
    }
383 198
384
  /**
385
     * Sets the column identity increment.
386
     *
387
     * @param int $increment
388
     * @return \Phinx\Db\Table\Column
389
     */
390
    public function setIncrement($increment)
391
    {
392 68
        $this->increment = $increment;
393
394 68
        return $this;
395 68
    }
396
397
    /**
398
     * Sets the number scale for decimal or float column.
399
     *
400
     * For example `DECIMAL(5,2)`, 5 is the precision and 2 is the scale,
401
     * and the column could store value from -999.99 to 999.99.
402
     *
403 89
     * @param int $scale Number scale
404
     * @return \Phinx\Db\Table\Column
405 89
     */
406
    public function setScale($scale)
407
    {
408
        $this->scale = $scale;
409
410
        return $this;
411
    }
412
413 89
    /**
414
     * Gets the number scale for decimal or float column.
415 89
     *
416
     * For example `DECIMAL(5,2)`, 5 is the precision and 2 is the scale,
417
     * and the column could store value from -999.99 to 999.99.
418
     *
419
     * @return int
420
     */
421
    public function getScale()
422
    {
423
        return $this->scale;
424
    }
425 1
426
    /**
427 1
     * Sets the number precision and scale for decimal or float column.
428 1
     *
429
     * For example `DECIMAL(5,2)`, 5 is the precision and 2 is the scale,
430
     * and the column could store value from -999.99 to 999.99.
431
     *
432
     * @param int $precision Number precision
433
     * @param int $scale Number scale
434
     * @return \Phinx\Db\Table\Column
435
     */
436 68
    public function setPrecisionAndScale($precision, $scale)
437
    {
438 68
        $this->setLimit($precision);
439
        $this->scale = $scale;
440
441
        return $this;
442
    }
443
444
    /**
445
     * Sets the column comment.
446 68
     *
447
     * @param string $comment
448 68
     * @return \Phinx\Db\Table\Column
449
     */
450
    public function setComment($comment)
451
    {
452
        $this->comment = $comment;
453
454
        return $this;
455
    }
456
457
    /**
458
     * Gets the column comment.
459
     *
460
     * @return string
461
     */
462
    public function getComment()
463
    {
464
        return $this->comment;
465
    }
466
467
    /**
468
     * Sets whether field should be signed.
469
     *
470
     * @param bool $signed
471
     * @return \Phinx\Db\Table\Column
472
     */
473
    public function setSigned($signed)
474
    {
475
        $this->signed = (bool)$signed;
476
477
        return $this;
478
    }
479
480
    /**
481 9
     * Gets whether field should be signed.
482
     *
483 9
     * @return bool
484 2
     */
485 2
    public function getSigned()
486 9
    {
487 9
        return $this->signed;
488
    }
489
490
    /**
491
     * Should the column be signed?
492
     *
493
     * @return bool
494
     */
495 131
    public function isSigned()
496
    {
497 131
        return $this->getSigned();
498
    }
499
500
    /**
501
     * Sets whether the field should have a timezone identifier.
502
     * Used for date/time columns only!
503
     *
504
     * @param bool $timezone
505
     * @return \Phinx\Db\Table\Column
506
     */
507
    public function setTimezone($timezone)
508 1
    {
509
        $this->timezone = (bool)$timezone;
510
511 1
        return $this;
512 1
    }
513 1
514 1
    /**
515 1
     * Gets whether field has a timezone identifier.
516
     *
517
     * @return bool
518
     */
519 1
    public function getTimezone()
520
    {
521 1
        return $this->timezone;
522
    }
523
524
    /**
525
     * Should the column have a timezone?
526
     *
527
     * @return bool
528
     */
529 89
    public function isTimezone()
530
    {
531 89
        return $this->getTimezone();
532
    }
533
534
    /**
535
     * Sets field properties.
536
     *
537
     * @param array $properties
538
     *
539
     * @return \Phinx\Db\Table\Column
540
     */
541
    public function setProperties($properties)
542
    {
543
        $this->properties = $properties;
544
545
        return $this;
546
    }
547
548
    /**
549
     * Gets field properties
550
     *
551
     * @return array
552
     */
553
    public function getProperties()
554
    {
555
        return $this->properties;
556
    }
557
558
    /**
559
     * Sets field values.
560
     *
561
     * @param array|string $values
562
     *
563 89
     * @return \Phinx\Db\Table\Column
564
     */
565 89
    public function setValues($values)
566
    {
567
        if (!is_array($values)) {
568
            $values = preg_split('/,\s*/', $values);
569
        }
570
        $this->values = $values;
571
572
        return $this;
573 209
    }
574
575
    /**
576 209
     * Gets field values
577 209
     *
578 209
     * @return array
579 209
     */
580 209
    public function getValues()
581 209
    {
582 209
        return $this->values;
583 209
    }
584 209
585 209
    /**
586 209
     * Sets the column collation.
587 209
     *
588 209
     * @param string $collation
589 209
     *
590 209
     * @throws \UnexpectedValueException If collation not allowed for type
591 209
     * @return $this
592
     */
593 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...
594
    {
595
        $allowedTypes = [
596
            AdapterInterface::PHINX_TYPE_CHAR,
597
            AdapterInterface::PHINX_TYPE_STRING,
598
            AdapterInterface::PHINX_TYPE_TEXT,
599 209
        ];
600
        if (!in_array($this->getType(), $allowedTypes)) {
601
            throw new \UnexpectedValueException('Collation may be set only for types: ' . implode(', ', $allowedTypes));
602 209
        }
603 209
604
        $this->collation = $collation;
605
606
        return $this;
607
    }
608
609
    /**
610
     * Gets the column collation.
611
     *
612 209
     * @return string
613
     */
614 209
    public function getCollation()
615 209
    {
616
        return $this->collation;
617 209
    }
618 208
619
    /**
620
     * Sets the column character set.
621
     *
622
     * @param string $encoding
623 208
     *
624 1
     * @throws \UnexpectedValueException If character set not allowed for type
625
     * @return $this
626
     */
627 207 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...
628 207
    {
629 208
        $allowedTypes = [
630 208
            AdapterInterface::PHINX_TYPE_CHAR,
631
            AdapterInterface::PHINX_TYPE_STRING,
632
            AdapterInterface::PHINX_TYPE_TEXT,
633
        ];
634
        if (!in_array($this->getType(), $allowedTypes)) {
635
            throw new \UnexpectedValueException('Character set may be set only for types: ' . implode(', ', $allowedTypes));
636
        }
637
638
        $this->encoding = $encoding;
639
640
        return $this;
641
    }
642
643
    /**
644
     * Gets the column character set.
645
     *
646
     * @return string
647
     */
648
    public function getEncoding()
649
    {
650
        return $this->encoding;
651
    }
652
653
    /**
654
     * Gets all allowed options. Each option must have a corresponding `setFoo` method.
655
     *
656
     * @return array
657
     */
658
    protected function getValidOptions()
659
    {
660
        return [
661
            'limit',
662
            'default',
663
            'null',
664
            'identity',
665
            'scale',
666
            'after',
667
            'update',
668
            'comment',
669
            'signed',
670
            'timezone',
671
            'properties',
672
            'values',
673
            'collation',
674
	    'encoding',
675
	    /* James Duncan Added seed and increment for MS SQL identity columns */
676
	    'seed',
677
	    'increment',
678
        ];
679
    }
680
681
    /**
682
     * Gets all aliased options. Each alias must reference a valid option.
683
     *
684
     * @return array
685
     */
686
    protected function getAliasedOptions()
687
    {
688
        return [
689
            'length' => 'limit',
690
            'precision' => 'limit',
691
        ];
692
    }
693
694
    /**
695
     * Utility method that maps an array of column options to this objects methods.
696
     *
697
     * @param array $options Options
698
     * @return \Phinx\Db\Table\Column
699
     */
700
    public function setOptions($options)
701
    {
702
        $validOptions = $this->getValidOptions();
703
        $aliasOptions = $this->getAliasedOptions();
704
705
        foreach ($options as $option => $value) {
706
            if (isset($aliasOptions[$option])) {
707
                // proxy alias -> option
708
                $option = $aliasOptions[$option];
709
            }
710
711
            if (!in_array($option, $validOptions, true)) {
712
                throw new \RuntimeException(sprintf('"%s" is not a valid column option.', $option));
713
            }
714
715
            $method = 'set' . ucfirst($option);
716
            $this->$method($value);
717
        }
718
719
        return $this;
720
    }
721
}
722