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\ORM\Mapper\Mapper; |
13
|
|
|
use Cycle\ORM\Select\Repository; |
14
|
|
|
use Cycle\ORM\Select\Source; |
15
|
|
|
use Cycle\Schema\Definition\Map\FieldMap; |
16
|
|
|
use Cycle\Schema\Definition\Map\OptionMap; |
17
|
|
|
use Cycle\Schema\Definition\Map\RelationMap; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Contains information about specific entity definition. |
21
|
|
|
*/ |
22
|
|
|
final class Entity |
23
|
|
|
{ |
24
|
|
|
/** @var OptionMap */ |
25
|
|
|
private $options; |
26
|
|
|
|
27
|
|
|
/** @var string */ |
28
|
|
|
private $role; |
29
|
|
|
|
30
|
|
|
/** @var string|null */ |
31
|
|
|
private $class; |
32
|
|
|
|
33
|
|
|
/** @var string|null */ |
34
|
|
|
private $mapper; |
35
|
|
|
|
36
|
|
|
/** @var string|null */ |
37
|
|
|
private $source; |
38
|
|
|
|
39
|
|
|
/** @var string|null */ |
40
|
|
|
private $constrain; |
41
|
|
|
|
42
|
|
|
/** @var string|null */ |
43
|
|
|
private $repository; |
44
|
|
|
|
45
|
|
|
/** @var FieldMap */ |
46
|
|
|
private $fields; |
47
|
|
|
|
48
|
|
|
/** @var RelationMap */ |
49
|
|
|
private $relations; |
50
|
|
|
|
51
|
|
|
/** @var array */ |
52
|
|
|
private $schema = []; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Entity constructor. |
56
|
|
|
*/ |
57
|
|
|
public function __construct() |
58
|
|
|
{ |
59
|
|
|
$this->options = new OptionMap(); |
60
|
|
|
$this->fields = new FieldMap(); |
61
|
|
|
$this->relations = new RelationMap(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return OptionMap |
66
|
|
|
*/ |
67
|
|
|
public function getOptions(): OptionMap |
68
|
|
|
{ |
69
|
|
|
return $this->options; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string $role |
74
|
|
|
* @return Entity |
75
|
|
|
*/ |
76
|
|
|
public function setRole(string $role): self |
77
|
|
|
{ |
78
|
|
|
$this->role = $role; |
79
|
|
|
|
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return string|null |
85
|
|
|
*/ |
86
|
|
|
public function getRole(): ?string |
87
|
|
|
{ |
88
|
|
|
return $this->role; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/*** |
92
|
|
|
* @param string $class |
93
|
|
|
* @return Entity |
94
|
|
|
*/ |
95
|
|
|
public function setClass(string $class): self |
96
|
|
|
{ |
97
|
|
|
$this->class = $class; |
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return string|null |
104
|
|
|
*/ |
105
|
|
|
public function getClass(): ?string |
106
|
|
|
{ |
107
|
|
|
return $this->class; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param string|null $mapper |
112
|
|
|
* @return Entity |
113
|
|
|
*/ |
114
|
|
|
public function setMapper(?string $mapper): self |
115
|
|
|
{ |
116
|
|
|
$this->mapper = $mapper; |
117
|
|
|
|
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
|
|
public function getMapper(): string |
125
|
|
|
{ |
126
|
|
|
return $this->mapper ?? Mapper::class; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param string|null $source |
131
|
|
|
* @return Entity |
132
|
|
|
*/ |
133
|
|
|
public function setSource(?string $source): self |
134
|
|
|
{ |
135
|
|
|
$this->source = $source; |
136
|
|
|
|
137
|
|
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
|
|
public function getSource(): string |
144
|
|
|
{ |
145
|
|
|
return $this->source ?? Source::class; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param string|null $constrain |
150
|
|
|
* @return Entity |
151
|
|
|
*/ |
152
|
|
|
public function setConstrain(?string $constrain): self |
153
|
|
|
{ |
154
|
|
|
$this->constrain = $constrain; |
155
|
|
|
|
156
|
|
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @return string|null |
161
|
|
|
*/ |
162
|
|
|
public function getConstrain(): ?string |
163
|
|
|
{ |
164
|
|
|
return $this->constrain; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param string|null $repository |
169
|
|
|
* @return Entity |
170
|
|
|
*/ |
171
|
|
|
public function setRepository(?string $repository): self |
172
|
|
|
{ |
173
|
|
|
$this->repository = $repository; |
174
|
|
|
|
175
|
|
|
return $this; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @return string |
180
|
|
|
*/ |
181
|
|
|
public function getRepository(): string |
182
|
|
|
{ |
183
|
|
|
return $this->repository ?? Repository::class; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @return FieldMap |
188
|
|
|
*/ |
189
|
|
|
public function getFields(): FieldMap |
190
|
|
|
{ |
191
|
|
|
return $this->fields; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @return RelationMap |
196
|
|
|
*/ |
197
|
|
|
public function getRelations(): RelationMap |
198
|
|
|
{ |
199
|
|
|
return $this->relations; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param array $schema |
204
|
|
|
* @return Entity |
205
|
|
|
*/ |
206
|
|
|
public function setSchema(array $schema): Entity |
207
|
|
|
{ |
208
|
|
|
$this->schema = $schema; |
209
|
|
|
|
210
|
|
|
return $this; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @return array |
215
|
|
|
*/ |
216
|
|
|
public function getSchema(): array |
217
|
|
|
{ |
218
|
|
|
return $this->schema; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Merge entity relations and fields. |
223
|
|
|
* |
224
|
|
|
* @param Entity $entity |
225
|
|
|
*/ |
226
|
|
|
public function merge(Entity $entity) |
227
|
|
|
{ |
228
|
|
|
foreach ($entity->getRelations() as $name => $relation) { |
229
|
|
|
if (!$this->relations->has($name)) { |
230
|
|
|
$this->relations->set($name, $relation); |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
foreach ($entity->getFields() as $name => $field) { |
235
|
|
|
if (!$this->fields->has($name)) { |
236
|
|
|
$this->fields->set($name, $field); |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Full entity copy. |
243
|
|
|
*/ |
244
|
|
|
public function __clone() |
245
|
|
|
{ |
246
|
|
|
$this->options = clone $this->options; |
247
|
|
|
$this->fields = clone $this->fields; |
248
|
|
|
$this->relations = clone $this->relations; |
249
|
|
|
} |
250
|
|
|
} |