Completed
Pull Request — master (#1641)
by
unknown
02:00
created

Column::isSigned()   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 $seed;
73
74
	/**
75
	 * @var integer
76
	 */
77
	protected $increment;
78
79
	/**
80
	 * @var integer
81
	 */
82
	protected $scale;
83
84
	/**
85
	 * @var string
86
	 */
87
	protected $after;
88
89
	/**
90
	 * @var string
91
	 */
92
	protected $update;
93
94
	/**
95
	 * @var string
96
	 */
97
	protected $comment;
98
99
	/**
100
	 * @var boolean
101
	 */
102
	protected $signed = true;
103
104
	/**
105
	 * @var boolean
106
	 */
107
	protected $timezone = false;
108
109
	/**
110
	 * @var array
111
	 */
112
	protected $properties = [];
113
114
	/**
115
	 * @var string
116
	 */
117
	protected $collation;
118
119
	/**
120
	 * @var string
121
	 */
122
	protected $encoding;
123
124
	/**
125
	 * @var array
126
	 */
127
	protected $values;
128
129
	/**
130 215
	 * Sets the column name.
131
	 *
132 215
	 * @param string $name
133 215
	 * @return \Phinx\Db\Table\Column
134
	 */
135
	public function setName($name)
136
	{
137
		$this->name = $name;
138
139
		return $this;
140
	}
141 212
142
	/**
143 212
	 * Gets the column name.
144
	 *
145
	 * @return string|null
146
	 */
147
	public function getName()
148
	{
149
		return $this->name;
150
	}
151
152 214
	/**
153
	 * Sets the column type.
154 214
	 *
155 214
	 * @param string|\Phinx\Util\Literal $type Column type
156
	 * @return \Phinx\Db\Table\Column
157
	 */
158
	public function setType($type)
159
	{
160
		$this->type = $type;
161
162
		return $this;
163 212
	}
164
165 212
	/**
166
	 * Gets the column type.
167
	 *
168
	 * @return string|\Phinx\Util\Literal
169
	 */
170
	public function getType()
171
	{
172
		return $this->type;
173
	}
174 192
175
	/**
176 192
	 * Sets the column limit.
177 192
	 *
178
	 * @param int $limit
179
	 * @return \Phinx\Db\Table\Column
180
	 */
181
	public function setLimit($limit)
182
	{
183
		$this->limit = $limit;
184
185 192
		return $this;
186
	}
187 192
188
	/**
189
	 * Gets the column limit.
190
	 *
191
	 * @return int
192
	 */
193
	public function getLimit()
194
	{
195
		return $this->limit;
196 208
	}
197
198 208
	/**
199 208
	 * Sets whether the column allows nulls.
200
	 *
201
	 * @param bool $null
202
	 * @return \Phinx\Db\Table\Column
203
	 */
204
	public function setNull($null)
205
	{
206
		$this->null = (bool)$null;
207 215
208
		return $this;
209 215
	}
210
211
	/**
212
	 * Gets whether the column allows nulls.
213
	 *
214
	 * @return bool
215
	 */
216
	public function getNull()
217 214
	{
218
		return $this->null;
219 214
	}
220
221
	/**
222
	 * Does the column allow nulls?
223
	 *
224
	 * @return bool
225
	 */
226
	public function isNull()
227
	{
228 207
		return $this->getNull();
229
	}
230 207
231 207
	/**
232
	 * Sets the default column value.
233
	 *
234
	 * @param mixed $default
235
	 * @return \Phinx\Db\Table\Column
236
	 */
237
	public function setDefault($default)
238
	{
239 215
		$this->default = $default;
240
241 215
		return $this;
242
	}
243
244
	/**
245
	 * Gets the default column value.
246
	 *
247
	 * @return mixed
248
	 */
249
	public function getDefault()
250 158
	{
251
		return $this->default;
252 158
	}
253 158
254
	/**
255
	 * Sets whether or not the column is an identity column.
256
	 *
257
	 * @param bool $identity
258
	 * @return \Phinx\Db\Table\Column
259
	 */
260
	public function setIdentity($identity)
261 199
	{
262
		$this->identity = $identity;
263 199
264
		return $this;
265
	}
266
267
	/**
268
	 * Gets whether or not the column is an identity column.
269
	 *
270
	 * @return bool
271 198
	 */
272
	public function getIdentity()
273 198
	{
274
		return $this->identity;
275
	}
276
277
	/**
278
	 * Is the column an identity column?
279
	 *
280
	 * @return bool
281
	 */
282 1
	public function isIdentity()
283
	{
284 1
		return $this->getIdentity();
285 1
	}
286
287
	/**
288
	 * Sets the name of the column to add this column after.
289
	 *
290
	 * @param string $after After
291
	 * @return \Phinx\Db\Table\Column
292
	 */
293 20
	public function setAfter($after)
294
	{
295 20
		$this->after = $after;
296
297
		return $this;
298
	}
299
300
	/**
301
	 * Returns the name of the column to add this column after.
302
	 *
303
	 * @return string
304 15
	 */
305
	public function getAfter()
306 15
	{
307 15
		return $this->after;
308
	}
309
310
	/**
311
	 * Sets the 'ON UPDATE' mysql column function.
312
	 *
313
	 * @param  string $update On Update function
314
	 * @return \Phinx\Db\Table\Column
315 145
	 */
316
	public function setUpdate($update)
317 145
	{
318
		$this->update = $update;
319
320
		return $this;
321
	}
322
323
	/**
324
	 * Returns the value of the ON UPDATE column function.
325
	 *
326 9
	 * @return string
327
	 */
328 9
	public function getUpdate()
329 9
	{
330
		return $this->update;
331
	}
332
333
	/**
334
	 * Sets the number precision for decimal or float column.
335
	 *
336
	 * For example `DECIMAL(5,2)`, 5 is the precision and 2 is the scale,
337 131
	 * and the column could store value from -999.99 to 999.99.
338
	 *
339 131
	 * @param int $precision Number precision
340
	 * @return \Phinx\Db\Table\Column
341
	 */
342
	public function setPrecision($precision)
343
	{
344
		$this->setLimit($precision);
345
346
		return $this;
347
	}
348 9
349
	/**
350 9
	 * Gets the number precision for decimal or float column.
351 9
	 *
352
	 * For example `DECIMAL(5,2)`, 5 is the precision and 2 is the scale,
353
	 * and the column could store value from -999.99 to 999.99.
354
	 *
355
	 * @return int
356
	 */
357
	public function getPrecision()
358
	{
359 2
		return $this->limit;
360
	}
361 2
362
	/**
363
	 * Gets the column identity seed.
364
	 *
365
	 * @return int
366
	 */
367
	public function getSeed()
368
	{
369
		return $this->seed;
370 9
	}
371
372 9
	/**
373 9
	 * Gets the column identity increment.
374
	 *
375
	 * @return int
376
	 */
377
	public function getIncrement()
378
	{
379
		return $this->increment;
380
	}
381 198
382
	/**
383 198
	 * Sets the column identity seed.
384
	 *
385
	 * @param int $seed Number seed
386
	 * @return \Phinx\Db\Table\Column
387
	 */
388
	public function setSeed($seed)
389
	{
390
		$this->seed = $seed;
391
392 68
		return $this;
393
	}
394 68
395 68
	/**
396
	 * Sets the column identity increment.
397
	 *
398
	 * @param int $increment Number 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
			'seed',
687
			'increment',
688
		];
689
	}
690
691
	/**
692
	 * Gets all aliased options. Each alias must reference a valid option.
693
	 *
694
	 * @return array
695
	 */
696
	protected function getAliasedOptions()
697
	{
698
		return [
699
			'length' => 'limit',
700
			'precision' => 'limit',
701
		];
702
	}
703
704
	/**
705
	 * Utility method that maps an array of column options to this objects methods.
706
	 *
707
	 * @param array $options Options
708
	 * @return \Phinx\Db\Table\Column
709
	 */
710
	public function setOptions($options)
711
	{
712
		$validOptions = $this->getValidOptions();
713
		$aliasOptions = $this->getAliasedOptions();
714
715
		foreach ($options as $option => $value) {
716
			if (isset($aliasOptions[$option])) {
717
				// proxy alias -> option
718
				$option = $aliasOptions[$option];
719
			}
720
721
			if (!in_array($option, $validOptions, true)) {
722
				throw new \RuntimeException(sprintf('"%s" is not a valid column option.', $option));
723
			}
724
725
			$method = 'set' . ucfirst($option);
726
			$this->$method($value);
727
		}
728
729
		return $this;
730
	}
731
}
732