1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Cycle ORM Schema Builder. |
5
|
|
|
* |
6
|
|
|
* @license MIT |
7
|
|
|
* @author Anton Titov (Wolfy-J) |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Cycle\Schema\Definition; |
13
|
|
|
|
14
|
|
|
use Cycle\Schema\Definition\Map\FieldMap; |
15
|
|
|
use Cycle\Schema\Definition\Map\OptionMap; |
16
|
|
|
use Cycle\Schema\Definition\Map\RelationMap; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Contains information about specific entity definition. |
20
|
|
|
*/ |
21
|
|
|
final class Entity |
22
|
|
|
{ |
23
|
|
|
/** @var OptionMap */ |
24
|
|
|
private $options; |
25
|
|
|
|
26
|
|
|
/** @var string */ |
27
|
|
|
private $role; |
28
|
|
|
|
29
|
|
|
/** @var string|null */ |
30
|
|
|
private $class; |
31
|
|
|
|
32
|
|
|
/** @var string|null */ |
33
|
|
|
private $mapper; |
34
|
|
|
|
35
|
|
|
/** @var string|null */ |
36
|
|
|
private $source; |
37
|
|
|
|
38
|
|
|
/** @var string|null */ |
39
|
|
|
private $constrain; |
40
|
|
|
|
41
|
|
|
/** @var string|null */ |
42
|
|
|
private $repository; |
43
|
|
|
|
44
|
|
|
/** @var FieldMap */ |
45
|
|
|
private $fields; |
46
|
|
|
|
47
|
|
|
/** @var RelationMap */ |
48
|
|
|
private $relations; |
49
|
|
|
|
50
|
|
|
/** @var array */ |
51
|
|
|
private $schema = []; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Entity constructor. |
55
|
|
|
*/ |
56
|
|
|
public function __construct() |
57
|
|
|
{ |
58
|
|
|
$this->options = new OptionMap(); |
59
|
|
|
$this->fields = new FieldMap(); |
60
|
|
|
$this->relations = new RelationMap(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Full entity copy. |
65
|
|
|
*/ |
66
|
|
|
public function __clone() |
67
|
|
|
{ |
68
|
|
|
$this->options = clone $this->options; |
69
|
|
|
$this->fields = clone $this->fields; |
70
|
|
|
$this->relations = clone $this->relations; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return OptionMap |
75
|
|
|
*/ |
76
|
|
|
public function getOptions(): OptionMap |
77
|
|
|
{ |
78
|
|
|
return $this->options; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $role |
83
|
|
|
* @return Entity |
84
|
|
|
*/ |
85
|
|
|
public function setRole(string $role): self |
86
|
|
|
{ |
87
|
|
|
$this->role = $role; |
88
|
|
|
|
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return string|null |
94
|
|
|
*/ |
95
|
|
|
public function getRole(): ?string |
96
|
|
|
{ |
97
|
|
|
return $this->role; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/*** |
101
|
|
|
* @param string $class |
102
|
|
|
* @return Entity |
103
|
|
|
*/ |
104
|
|
|
public function setClass(string $class): self |
105
|
|
|
{ |
106
|
|
|
$this->class = $class; |
107
|
|
|
|
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return string|null |
113
|
|
|
*/ |
114
|
|
|
public function getClass(): ?string |
115
|
|
|
{ |
116
|
|
|
return $this->class; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param string|null $mapper |
121
|
|
|
* @return Entity |
122
|
|
|
*/ |
123
|
|
|
public function setMapper(?string $mapper): self |
124
|
|
|
{ |
125
|
|
|
$this->mapper = $mapper; |
126
|
|
|
|
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
|
|
public function getMapper(): ?string |
134
|
|
|
{ |
135
|
|
|
return $this->normalizeClass($this->mapper); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param string|null $source |
140
|
|
|
* @return Entity |
141
|
|
|
*/ |
142
|
|
|
public function setSource(?string $source): self |
143
|
|
|
{ |
144
|
|
|
$this->source = $source; |
145
|
|
|
|
146
|
|
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
|
|
public function getSource(): ?string |
153
|
|
|
{ |
154
|
|
|
return $this->normalizeClass($this->source); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param string|null $constrain |
159
|
|
|
* @return Entity |
160
|
|
|
*/ |
161
|
|
|
public function setConstrain(?string $constrain): self |
162
|
|
|
{ |
163
|
|
|
$this->constrain = $constrain; |
164
|
|
|
|
165
|
|
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @return string|null |
170
|
|
|
*/ |
171
|
|
|
public function getConstrain(): ?string |
172
|
|
|
{ |
173
|
|
|
return $this->normalizeClass($this->constrain); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param string|null $repository |
178
|
|
|
* @return Entity |
179
|
|
|
*/ |
180
|
|
|
public function setRepository(?string $repository): self |
181
|
|
|
{ |
182
|
|
|
$this->repository = $repository; |
183
|
|
|
|
184
|
|
|
return $this; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @return string |
189
|
|
|
*/ |
190
|
|
|
public function getRepository(): ?string |
191
|
|
|
{ |
192
|
|
|
return $this->normalizeClass($this->repository); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @return FieldMap |
197
|
|
|
*/ |
198
|
|
|
public function getFields(): FieldMap |
199
|
|
|
{ |
200
|
|
|
return $this->fields; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @return RelationMap |
205
|
|
|
*/ |
206
|
|
|
public function getRelations(): RelationMap |
207
|
|
|
{ |
208
|
|
|
return $this->relations; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @param array $schema |
213
|
|
|
* @return Entity |
214
|
|
|
*/ |
215
|
|
|
public function setSchema(array $schema): Entity |
216
|
|
|
{ |
217
|
|
|
$this->schema = $schema; |
218
|
|
|
|
219
|
|
|
return $this; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @return array |
224
|
|
|
*/ |
225
|
|
|
public function getSchema(): array |
226
|
|
|
{ |
227
|
|
|
return $this->schema; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Merge entity relations and fields. |
232
|
|
|
* |
233
|
|
|
* @param Entity $entity |
234
|
|
|
*/ |
235
|
|
|
public function merge(Entity $entity): void |
236
|
|
|
{ |
237
|
|
|
foreach ($entity->getRelations() as $name => $relation) { |
238
|
|
|
if (!$this->relations->has($name)) { |
239
|
|
|
$this->relations->set($name, $relation); |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
foreach ($entity->getFields() as $name => $field) { |
244
|
|
|
if (!$this->fields->has($name)) { |
245
|
|
|
$this->fields->set($name, $field); |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Check if entity has primary key |
252
|
|
|
* |
253
|
|
|
* @return bool |
254
|
|
|
*/ |
255
|
|
|
public function hasPrimaryKey(): bool |
256
|
|
|
{ |
257
|
|
|
foreach ($this->getFields() as $name => $field) { |
258
|
|
|
if ($field->isPrimary()) { |
259
|
|
|
return true; |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
return false; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Get entity primary keys |
268
|
|
|
* |
269
|
|
|
* @return array |
270
|
|
|
*/ |
271
|
|
|
public function getPrimaryKeys(): array |
272
|
|
|
{ |
273
|
|
|
$keys = []; |
274
|
|
|
|
275
|
|
|
foreach ($this->getFields() as $name => $field) { |
276
|
|
|
if ($field->isPrimary()) { |
277
|
|
|
$keys[] = $name; |
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
return $keys; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* @param string|null $class |
286
|
|
|
* @return string|null |
287
|
|
|
*/ |
288
|
|
|
private function normalizeClass(string $class = null): ?string |
289
|
|
|
{ |
290
|
|
|
if ($class === null) { |
291
|
|
|
return null; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
return ltrim($class, '\\'); |
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
|