Passed
Push — master ( ad038c...02f8e8 )
by Anton
01:54
created

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