Passed
Push — master ( 5ad27f...037772 )
by David
53s
created

FluidColumnOptions::default()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
4
namespace TheCodingMachine\FluidSchema;
5
6
use Doctrine\DBAL\Schema\Column;
7
use Doctrine\DBAL\Types\Type;
8
9
class FluidColumnOptions
10
{
11
    /**
12
     * @var FluidTable
13
     */
14
    private $fluidTable;
15
    /**
16
     * @var Column
17
     */
18
    private $column;
19
20
    /**
21
     * FluidColumn constructor.
22
     * @param FluidTable $fluidTable
23
     * @param Column $column
24
     */
25
    public function __construct(FluidTable $fluidTable, Column $column)
26
    {
27
        $this->fluidTable = $fluidTable;
28
        $this->column = $column;
29
    }
30
31
    /**
32
     * Makes the column not nullable.
33
     * @return FluidColumnOptions
34
     */
35
    public function notNull(): FluidColumnOptions
36
    {
37
        $this->column->setNotnull(true);
38
        return $this;
39
    }
40
41
    /**
42
     * Makes the column nullable.
43
     * @return FluidColumnOptions
44
     */
45
    public function null(): FluidColumnOptions
46
    {
47
        $this->column->setNotnull(false);
48
        return $this;
49
    }
50
51
    /**
52
     * Automatically add a unique constraint for the column.
53
     *
54
     * @return FluidColumnOptions
55
     */
56
    public function unique(): FluidColumnOptions
57
    {
58
        $this->column->setCustomSchemaOption('unique', true);
59
        return $this;
60
    }
61
62
    /**
63
     * Automatically add an index for the column.
64
     *
65
     * @return FluidColumnOptions
66
     */
67
    public function index(): FluidColumnOptions
68
    {
69
        $this->fluidTable->index([$this->column->getName()]);
70
        return $this;
71
    }
72
    public function comment(string $comment): FluidColumnOptions
73
    {
74
        $this->column->setComment($comment);
75
        return $this;
76
    }
77
78
    public function autoIncrement(): FluidColumnOptions
79
    {
80
        $this->column->setAutoincrement(true);
81
        return $this;
82
    }
83
84
    public function primaryKey(?string $indexName = null): FluidColumnOptions
85
    {
86
        $newIndexName = $indexName ?: false;
87
88
        $this->fluidTable->primaryKey([$this->column->getName()], $newIndexName);
89
        return $this;
90
    }
91
92
    public function default($defaultValue): FluidColumnOptions
0 ignored issues
show
Coding Style introduced by
Possible parse error: non-abstract method defined as abstract
Loading history...
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
93
    {
94
        $this->column->setDefault($defaultValue);
95
        return $this;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $this.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
96
    }
97
98
    public function then(): FluidTable
99
    {
100
        return $this->fluidTable;
101
    }
102
103
    public function column($name): FluidColumn
104
    {
105
        return $this->fluidTable->column($name);
106
    }
107
}
108