1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Cycle ORM Schema Builder. |
4
|
|
|
* |
5
|
|
|
* @license MIT |
6
|
|
|
* @author Anton Titov (Wolfy-J) |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
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 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 OptionMap |
48
|
|
|
*/ |
49
|
|
|
public function getOptions(): OptionMap |
50
|
|
|
{ |
51
|
|
|
return $this->options; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
|
|
public function getType(): string |
58
|
|
|
{ |
59
|
|
|
if (empty($this->column)) { |
60
|
|
|
throw new FieldException("Field type must be set"); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $this->type; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $type |
68
|
|
|
* @return Field |
69
|
|
|
*/ |
70
|
|
|
public function setType(string $type): Field |
71
|
|
|
{ |
72
|
|
|
$this->type = $type; |
73
|
|
|
|
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param bool $primary |
79
|
|
|
* @return Field |
80
|
|
|
*/ |
81
|
|
|
public function setPrimary(bool $primary): Field |
82
|
|
|
{ |
83
|
|
|
$this->primary = $primary; |
84
|
|
|
|
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return bool |
90
|
|
|
*/ |
91
|
|
|
public function isPrimary(): bool |
92
|
|
|
{ |
93
|
|
|
return $this->primary || in_array($this->type, ['primary', 'bigPrimary']); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string $column |
98
|
|
|
* @return Field |
99
|
|
|
*/ |
100
|
|
|
public function setColumn(string $column): Field |
101
|
|
|
{ |
102
|
|
|
$this->column = $column; |
103
|
|
|
|
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return string |
109
|
|
|
* |
110
|
|
|
* @throws FieldException |
111
|
|
|
*/ |
112
|
|
|
public function getColumn(): string |
113
|
|
|
{ |
114
|
|
|
if (empty($this->column)) { |
115
|
|
|
throw new FieldException("Column mapping must be set"); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $this->column; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param array|string $typecast |
123
|
|
|
* @return Field |
124
|
|
|
*/ |
125
|
|
|
public function setTypecast($typecast) |
126
|
|
|
{ |
127
|
|
|
$this->typecast = $typecast; |
128
|
|
|
|
129
|
|
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return bool |
134
|
|
|
*/ |
135
|
|
|
public function hasTypecast(): bool |
136
|
|
|
{ |
137
|
|
|
return $this->typecast !== null; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return array|string |
142
|
|
|
*/ |
143
|
|
|
public function getTypecast() |
144
|
|
|
{ |
145
|
|
|
return $this->typecast; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param bool $indexed |
150
|
|
|
* @return Field |
151
|
|
|
*/ |
152
|
|
|
public function setReferenced(bool $indexed): Field |
153
|
|
|
{ |
154
|
|
|
$this->referenced = $indexed; |
155
|
|
|
|
156
|
|
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @return bool |
161
|
|
|
*/ |
162
|
|
|
public function isReferenced(): bool |
163
|
|
|
{ |
164
|
|
|
return $this->referenced; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Cloning. |
169
|
|
|
*/ |
170
|
|
|
public function __clone() |
171
|
|
|
{ |
172
|
|
|
$this->options = clone $this->options; |
173
|
|
|
} |
174
|
|
|
} |