Passed
Push — master ( c6b93a...2fd976 )
by Anton
01:45
created

Field::getType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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