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

Column::setNull()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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 $seed;
73
74
    /**
75
     * @var integer
76
     */
77
    protected $increment;
78
79
80
    /**
81
     * @var integer
82
     */
83
    protected $scale;
84
85
    /**
86
     * @var string
87
     */
88
    protected $after;
89
90
    /**
91
     * @var string
92
     */
93
    protected $update;
94
95
    /**
96
     * @var string
97
     */
98
    protected $comment;
99
100
    /**
101
     * @var boolean
102
     */
103
    protected $signed = true;
104
105
    /**
106
     * @var boolean
107
     */
108
    protected $timezone = false;
109
110
    /**
111
     * @var array
112
     */
113
    protected $properties = [];
114
115
    /**
116
     * @var string
117
     */
118
    protected $collation;
119
120
    /**
121
     * @var string
122
     */
123
    protected $encoding;
124
125
    /**
126
     * @var array
127
     */
128
    protected $values;
129
130 215
    /**
131
     * Sets the column name.
132 215
     *
133 215
     * @param string $name
134
     * @return \Phinx\Db\Table\Column
135
     */
136
    public function setName($name)
137
    {
138
        $this->name = $name;
139
140
        return $this;
141 212
    }
142
143 212
    /**
144
     * Gets the column name.
145
     *
146
     * @return string|null
147
     */
148
    public function getName()
149
    {
150
        return $this->name;
151
    }
152 214
153
    /**
154 214
     * Sets the column type.
155 214
     *
156
     * @param string|\Phinx\Util\Literal $type Column type
157
     * @return \Phinx\Db\Table\Column
158
     */
159
    public function setType($type)
160
    {
161
        $this->type = $type;
162
163 212
        return $this;
164
    }
165 212
166
    /**
167
     * Gets the column type.
168
     *
169
     * @return string|\Phinx\Util\Literal
170
     */
171
    public function getType()
172
    {
173
        return $this->type;
174 192
    }
175
176 192
    /**
177 192
     * Sets the column limit.
178
     *
179
     * @param int $limit
180
     * @return \Phinx\Db\Table\Column
181
     */
182
    public function setLimit($limit)
183
    {
184
        $this->limit = $limit;
185 192
186
        return $this;
187 192
    }
188
189
    /**
190
     * Gets the column limit.
191
     *
192
     * @return int
193
     */
194
    public function getLimit()
195
    {
196 208
        return $this->limit;
197
    }
198 208
199 208
    /**
200
     * Sets whether the column allows nulls.
201
     *
202
     * @param bool $null
203
     * @return \Phinx\Db\Table\Column
204
     */
205
    public function setNull($null)
206
    {
207 215
        $this->null = (bool)$null;
208
209 215
        return $this;
210
    }
211
212
    /**
213
     * Gets whether the column allows nulls.
214
     *
215
     * @return bool
216
     */
217 214
    public function getNull()
218
    {
219 214
        return $this->null;
220
    }
221
222
    /**
223
     * Does the column allow nulls?
224
     *
225
     * @return bool
226
     */
227
    public function isNull()
228 207
    {
229
        return $this->getNull();
230 207
    }
231 207
232
    /**
233
     * Sets the default column value.
234
     *
235
     * @param mixed $default
236
     * @return \Phinx\Db\Table\Column
237
     */
238
    public function setDefault($default)
239 215
    {
240
        $this->default = $default;
241 215
242
        return $this;
243
    }
244
245
    /**
246
     * Gets the default column value.
247
     *
248
     * @return mixed
249
     */
250 158
    public function getDefault()
251
    {
252 158
        return $this->default;
253 158
    }
254
255
    /**
256
     * Sets whether or not the column is an identity column.
257
     *
258
     * @param bool $identity
259
     * @return \Phinx\Db\Table\Column
260
     */
261 199
    public function setIdentity($identity)
262
    {
263 199
        $this->identity = $identity;
264
265
        return $this;
266
    }
267
268
    /**
269
     * Gets whether or not the column is an identity column.
270
     *
271 198
     * @return bool
272
     */
273 198
    public function getIdentity()
274
    {
275
        return $this->identity;
276
    }
277
278
    /**
279
     * Is the column an identity column?
280
     *
281
     * @return bool
282 1
     */
283
    public function isIdentity()
284 1
    {
285 1
        return $this->getIdentity();
286
    }
287
288
    /**
289
     * Sets the name of the column to add this column after.
290
     *
291
     * @param string $after After
292
     * @return \Phinx\Db\Table\Column
293 20
     */
294
    public function setAfter($after)
295 20
    {
296
        $this->after = $after;
297
298
        return $this;
299
    }
300
301
    /**
302
     * Returns the name of the column to add this column after.
303
     *
304 15
     * @return string
305
     */
306 15
    public function getAfter()
307 15
    {
308
        return $this->after;
309
    }
310
311
    /**
312
     * Sets the 'ON UPDATE' mysql column function.
313
     *
314
     * @param  string $update On Update function
315 145
     * @return \Phinx\Db\Table\Column
316
     */
317 145
    public function setUpdate($update)
318
    {
319
        $this->update = $update;
320
321
        return $this;
322
    }
323
324
    /**
325
     * Returns the value of the ON UPDATE column function.
326 9
     *
327
     * @return string
328 9
     */
329 9
    public function getUpdate()
330
    {
331
        return $this->update;
332
    }
333
334
    /**
335
     * Sets the number precision for decimal or float column.
336
     *
337 131
     * For example `DECIMAL(5,2)`, 5 is the precision and 2 is the scale,
338
     * and the column could store value from -999.99 to 999.99.
339 131
     *
340
     * @param int $precision Number precision
341
     * @return \Phinx\Db\Table\Column
342
     */
343
    public function setPrecision($precision)
344
    {
345
        $this->setLimit($precision);
346
347
        return $this;
348 9
    }
349
350 9
    /**
351 9
     * Gets the number precision for decimal or float column.
352
     *
353
     * For example `DECIMAL(5,2)`, 5 is the precision and 2 is the scale,
354
     * and the column could store value from -999.99 to 999.99.
355
     *
356
     * @return int
357
     */
358
    public function getPrecision()
359 2
    {
360
        return $this->limit;
361 2
    }
362
363
      /**
364
     * Gets the column identity seed.
365
     *
366
     * @return int
367
     */
368
    public function getSeed()
369
    {
370 9
        return $this->seed;
371
    }
372 9
373 9
  /**
374
     * Gets the column identity increment.
375
     *
376
     * @return int
377
     */
378
    public function getIncrement()
379
    {
380
        return $this->increment;
381 198
    }
382
383 198
  /**
384
     * Sets the column identity seed.
385
     *
386
     * @param int $seed
387
     * @return \Phinx\Db\Table\Column
388
     */
389
    public function setSeed($seed)
390
    {
391
        $this->seed = $seed;
392 68
        return $this;
393
    }
394 68
395 68
  /**
396
     * Sets the column identity increment.
397
     *
398
     * @param int $increment
399
     * @return \Phinx\Db\Table\Column
400
     */
401
    public function setIncrement($increment)
402
    {
403 89
        $this->increment = $increment;
404
405 89
        return $this;
406
    }
407
408
    /**
409
     * Sets the number scale for decimal or float column.
410
     *
411
     * For example `DECIMAL(5,2)`, 5 is the precision and 2 is the scale,
412
     * and the column could store value from -999.99 to 999.99.
413 89
     *
414
     * @param int $scale Number scale
415 89
     * @return \Phinx\Db\Table\Column
416
     */
417
    public function setScale($scale)
418
    {
419
        $this->scale = $scale;
420
421
        return $this;
422
    }
423
424
    /**
425 1
     * Gets the number scale for decimal or float column.
426
     *
427 1
     * For example `DECIMAL(5,2)`, 5 is the precision and 2 is the scale,
428 1
     * and the column could store value from -999.99 to 999.99.
429
     *
430
     * @return int
431
     */
432
    public function getScale()
433
    {
434
        return $this->scale;
435
    }
436 68
437
    /**
438 68
     * Sets the number precision and scale for decimal or float column.
439
     *
440
     * For example `DECIMAL(5,2)`, 5 is the precision and 2 is the scale,
441
     * and the column could store value from -999.99 to 999.99.
442
     *
443
     * @param int $precision Number precision
444
     * @param int $scale Number scale
445
     * @return \Phinx\Db\Table\Column
446 68
     */
447
    public function setPrecisionAndScale($precision, $scale)
448 68
    {
449
        $this->setLimit($precision);
450
        $this->scale = $scale;
451
452
        return $this;
453
    }
454
455
    /**
456
     * Sets the column comment.
457
     *
458
     * @param string $comment
459
     * @return \Phinx\Db\Table\Column
460
     */
461
    public function setComment($comment)
462
    {
463
        $this->comment = $comment;
464
465
        return $this;
466
    }
467
468
    /**
469
     * Gets the column comment.
470
     *
471
     * @return string
472
     */
473
    public function getComment()
474
    {
475
        return $this->comment;
476
    }
477
478
    /**
479
     * Sets whether field should be signed.
480
     *
481 9
     * @param bool $signed
482
     * @return \Phinx\Db\Table\Column
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
     * @return \Phinx\Db\Table\Column
517
     */
518
    public function setTimezone($timezone)
519 1
    {
520
        $this->timezone = (bool)$timezone;
521 1
522
        return $this;
523
    }
524
525
    /**
526
     * Gets whether field has a timezone identifier.
527
     *
528
     * @return bool
529 89
     */
530
    public function getTimezone()
531 89
    {
532
        return $this->timezone;
533
    }
534
535
    /**
536
     * Should the column have a timezone?
537
     *
538
     * @return bool
539
     */
540
    public function isTimezone()
541
    {
542
        return $this->getTimezone();
543
    }
544
545
    /**
546
     * Sets field properties.
547
     *
548
     * @param array $properties
549
     *
550
     * @return \Phinx\Db\Table\Column
551
     */
552
    public function setProperties($properties)
553
    {
554
        $this->properties = $properties;
555
556
        return $this;
557
    }
558
559
    /**
560
     * Gets field properties
561
     *
562
     * @return array
563 89
     */
564
    public function getProperties()
565 89
    {
566
        return $this->properties;
567
    }
568
569
    /**
570
     * Sets field values.
571
     *
572
     * @param array|string $values
573 209
     *
574
     * @return \Phinx\Db\Table\Column
575
     */
576 209
    public function setValues($values)
577 209
    {
578 209
        if (!is_array($values)) {
579 209
            $values = preg_split('/,\s*/', $values);
580 209
        }
581 209
        $this->values = $values;
582 209
583 209
        return $this;
584 209
    }
585 209
586 209
    /**
587 209
     * Gets field values
588 209
     *
589 209
     * @return array
590 209
     */
591 209
    public function getValues()
592
    {
593
        return $this->values;
594
    }
595
596
    /**
597
     * Sets the column collation.
598
     *
599 209
     * @param string $collation
600
     *
601
     * @throws \UnexpectedValueException If collation not allowed for type
602 209
     * @return $this
603 209
     */
604 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...
605
    {
606
        $allowedTypes = [
607
            AdapterInterface::PHINX_TYPE_CHAR,
608
            AdapterInterface::PHINX_TYPE_STRING,
609
            AdapterInterface::PHINX_TYPE_TEXT,
610
        ];
611
        if (!in_array($this->getType(), $allowedTypes)) {
612 209
            throw new \UnexpectedValueException('Collation may be set only for types: ' . implode(', ', $allowedTypes));
613
        }
614 209
615 209
        $this->collation = $collation;
616
617 209
        return $this;
618 208
    }
619
620
    /**
621
     * Gets the column collation.
622
     *
623 208
     * @return string
624 1
     */
625
    public function getCollation()
626
    {
627 207
        return $this->collation;
628 207
    }
629 208
630 208
    /**
631
     * Sets the column character set.
632
     *
633
     * @param string $encoding
634
     *
635
     * @throws \UnexpectedValueException If character set not allowed for type
636
     * @return $this
637
     */
638 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...
639
    {
640
        $allowedTypes = [
641
            AdapterInterface::PHINX_TYPE_CHAR,
642
            AdapterInterface::PHINX_TYPE_STRING,
643
            AdapterInterface::PHINX_TYPE_TEXT,
644
        ];
645
        if (!in_array($this->getType(), $allowedTypes)) {
646
            throw new \UnexpectedValueException('Character set may be set only for types: ' . implode(', ', $allowedTypes));
647
        }
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
     * Gets all allowed options. Each option must have a corresponding `setFoo` method.
666
     *
667
     * @return array
668
     */
669
    protected function getValidOptions()
670
    {
671
        return [
672
            'limit',
673
            'default',
674
            'null',
675
            'identity',
676
            'scale',
677
            'after',
678
            'update',
679
            'comment',
680
            'signed',
681
            'timezone',
682
            'properties',
683
            'values',
684
            'collation',
685
	    'encoding',
686
	    /* James Duncan Added seed and increment for MS SQL identity columns */
687
	    'seed',
688
	    'increment',
689
        ];
690
    }
691
692
    /**
693
     * Gets all aliased options. Each alias must reference a valid option.
694
     *
695
     * @return array
696
     */
697
    protected function getAliasedOptions()
698
    {
699
        return [
700
            'length' => 'limit',
701
            'precision' => 'limit',
702
        ];
703
    }
704
705
    /**
706
     * Utility method that maps an array of column options to this objects methods.
707
     *
708
     * @param array $options Options
709
     * @return \Phinx\Db\Table\Column
710
     */
711
    public function setOptions($options)
712
    {
713
        $validOptions = $this->getValidOptions();
714
        $aliasOptions = $this->getAliasedOptions();
715
716
        foreach ($options as $option => $value) {
717
            if (isset($aliasOptions[$option])) {
718
                // proxy alias -> option
719
                $option = $aliasOptions[$option];
720
            }
721
722
            if (!in_array($option, $validOptions, true)) {
723
                throw new \RuntimeException(sprintf('"%s" is not a valid column option.', $option));
724
            }
725
726
            $method = 'set' . ucfirst($option);
727
            $this->$method($value);
728
        }
729
730
        return $this;
731
    }
732
}
733