Completed
Push — master ( 4df908...713b25 )
by Anton
01:54
created

Field::setColumn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
namespace Cycle\Schema\Definition;
11
12
use Cycle\Schema\Exception\FieldException;
13
14
/**
15
 * Field declaration, it's type and mapping to column.
16
 */
17
final class Field
18
{
19
    /** @var string */
20
    private $column;
21
22
    /** @var string */
23
    private $type;
24
25
    /** @var mixed */
26
    private $default;
27
28
    /** @var bool */
29
    private $nullable = false;
30
31
    /** @var array|string */
32
    private $typecast;
33
34
    /** @var bool */
35
    private $referenced = false;
36
37
    /**
38
     * @return string
39
     */
40
    public function getType(): string
41
    {
42
        if (empty($this->column)) {
43
            throw new FieldException("Field type must be set");
44
        }
45
46
        return $this->type;
47
    }
48
49
    /**
50
     * @param string $type
51
     * @return Field
52
     */
53
    public function setType(string $type): Field
54
    {
55
        $this->type = $type;
56
57
        return $this;
58
    }
59
60
    /**
61
     * @return mixed
62
     */
63
    public function getDefault()
64
    {
65
        return $this->default;
66
    }
67
68
    /**
69
     * @param mixed $default
70
     * @return Field
71
     */
72
    public function setDefault($default)
73
    {
74
        $this->default = $default;
75
76
        return $this;
77
    }
78
79
    /**
80
     * @return bool
81
     */
82
    public function isNullable(): bool
83
    {
84
        return $this->nullable;
85
    }
86
87
    /**
88
     * @param bool $nullable
89
     * @return Field
90
     */
91
    public function setNullable(bool $nullable): Field
92
    {
93
        $this->nullable = $nullable;
94
95
        return $this;
96
    }
97
98
99
    /**
100
     * @param string $column
101
     * @return Field
102
     */
103
    public function setColumn(string $column): Field
104
    {
105
        $this->column = $column;
106
107
        return $this;
108
    }
109
110
    /**
111
     * @return string
112
     *
113
     * @throws FieldException
114
     */
115
    public function getColumn(): string
116
    {
117
        if (empty($this->column)) {
118
            throw new FieldException("Column mapping must be set");
119
        }
120
121
        return $this->column;
122
    }
123
124
    /**
125
     * @param array|string $typecast
126
     * @return Field
127
     */
128
    public function setTypecast($typecast)
129
    {
130
        $this->typecast = $typecast;
131
132
        return $this;
133
    }
134
135
    /**
136
     * @return bool
137
     */
138
    public function hasTypecast(): bool
139
    {
140
        return $this->typecast !== null;
141
    }
142
143
    /**
144
     * @return array|string
145
     */
146
    public function getTypecast()
147
    {
148
        return $this->typecast;
149
    }
150
151
    /**
152
     * @param bool $indexed
153
     * @return Field
154
     */
155
    public function setReferenced(bool $indexed): Field
156
    {
157
        $this->referenced = $indexed;
158
159
        return $this;
160
    }
161
162
    /**
163
     * @return bool
164
     */
165
    public function isReferenced(): bool
166
    {
167
        return $this->referenced;
168
    }
169
}