Completed
Pull Request — master (#1387)
by
unknown
02:21
created

Column::getAliasedOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 2
cts 2
cp 1
rs 9.4285
cc 1
eloc 3
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 $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
     * 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 column precision for decimal.
330
     *
331
     * @param int $precision
332
     * @return \Phinx\Db\Table\Column
333
     */
334
    public function setPrecision($precision)
335
    {
336
        $this->precision = $precision;
337 131
338
        return $this;
339 131
    }
340
341
    /**
342
     * Gets the column precision for decimal.
343
     *
344
     * @return int
345
     */
346
    public function getPrecision()
347
    {
348 9
        return $this->precision;
349
    }
350 9
351 9
    /**
352
     * Sets the column scale for decimal.
353
     *
354
     * @param int $scale
355
     * @return \Phinx\Db\Table\Column
356
     */
357
    public function setScale($scale)
358
    {
359 2
        $this->scale = $scale;
360
361 2
        return $this;
362
    }
363
364
    /**
365
     * Gets the column scale for decimal.
366
     *
367
     * @return int
368
     */
369
    public function getScale()
370 9
    {
371
        return $this->scale;
372 9
    }
373 9
374
    /**
375
     * Sets the column precision and scale for decimal.
376
     *
377
     * @param int $precision
378
     * @param int $scale
379
     * @return \Phinx\Db\Table\Column
380
     */
381 198
    public function setPrecisionAndScale($precision, $scale)
382
    {
383 198
        $this->precision = $precision;
384
        $this->scale = $scale;
385
386
        return $this;
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 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
     * Gets all allowed options. Each option must have a corresponding `setFoo` method.
600
     *
601
     * @return array
602 209
     */
603 209
    protected function getValidOptions()
604
    {
605
        return [
606
            'limit',
607
            'default',
608
            'null',
609
            'identity',
610
            'precision',
611
            'scale',
612 209
            'after',
613
            'update',
614 209
            'comment',
615 209
            'signed',
616
            'timezone',
617 209
            'properties',
618 208
            'values',
619
            'collation',
620
            'encoding',
621
        ];
622
    }
623 208
624 1
    /**
625
     * Gets all aliased options. Each alias must reference a valid option.
626
     *
627 207
     * @return array
628 207
     */
629 208
    protected function getAliasedOptions()
630 208
    {
631
        return [
632
            'length' => 'limit',
633
        ];
634
    }
635
636
    /**
637
     * Utility method that maps an array of column options to this objects methods.
638
     *
639
     * @param array $options Options
640
     * @return \Phinx\Db\Table\Column
641
     */
642
    public function setOptions($options)
643
    {
644
        $validOptions = $this->getValidOptions();
645
        $aliasOptions = $this->getAliasedOptions();
646
647
        foreach ($options as $option => $value) {
648
            if (isset($aliasOptions[$option])) {
649
                // proxy alias -> option
650
                $option = $aliasOptions[$option];
651
            }
652
653
            if (!in_array($option, $validOptions, true)) {
654
                throw new \RuntimeException(sprintf('"%s" is not a valid column option.', $option));
655
            }
656
657
            $method = 'set' . ucfirst($option);
658
            $this->$method($value);
659
        }
660
661
        return $this;
662
    }
663
}
664