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 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 string |
45
|
|
|
*/ |
46
|
|
|
public function getType(): string |
47
|
|
|
{ |
48
|
|
|
if (empty($this->column)) { |
49
|
|
|
throw new FieldException("Field type must be set"); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $this->type; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $type |
57
|
|
|
* @return Field |
58
|
|
|
*/ |
59
|
|
|
public function setType(string $type): Field |
60
|
|
|
{ |
61
|
|
|
$this->type = $type; |
62
|
|
|
|
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $column |
68
|
|
|
* @return Field |
69
|
|
|
*/ |
70
|
|
|
public function setColumn(string $column): Field |
71
|
|
|
{ |
72
|
|
|
$this->column = $column; |
73
|
|
|
|
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return string |
79
|
|
|
* |
80
|
|
|
* @throws FieldException |
81
|
|
|
*/ |
82
|
|
|
public function getColumn(): string |
83
|
|
|
{ |
84
|
|
|
if (empty($this->column)) { |
85
|
|
|
throw new FieldException("Column mapping must be set"); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $this->column; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param array|string $typecast |
93
|
|
|
* @return Field |
94
|
|
|
*/ |
95
|
|
|
public function setTypecast($typecast) |
96
|
|
|
{ |
97
|
|
|
$this->typecast = $typecast; |
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return bool |
104
|
|
|
*/ |
105
|
|
|
public function hasTypecast(): bool |
106
|
|
|
{ |
107
|
|
|
return $this->typecast !== null; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return array|string |
112
|
|
|
*/ |
113
|
|
|
public function getTypecast() |
114
|
|
|
{ |
115
|
|
|
return $this->typecast; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param bool $indexed |
120
|
|
|
* @return Field |
121
|
|
|
*/ |
122
|
|
|
public function setReferenced(bool $indexed): Field |
123
|
|
|
{ |
124
|
|
|
$this->referenced = $indexed; |
125
|
|
|
|
126
|
|
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return bool |
131
|
|
|
*/ |
132
|
|
|
public function isReferenced(): bool |
133
|
|
|
{ |
134
|
|
|
return $this->referenced; |
135
|
|
|
} |
136
|
|
|
} |