Completed
Pull Request — master (#1558)
by
unknown
01:49
created

Column::setAfter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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