Completed
Pull Request — master (#1311)
by
unknown
02:31
created

Column::setStored()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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