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\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\RelationMap; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Contains information about specific entity definition. |
20
|
|
|
*/ |
21
|
|
|
final class Entity |
22
|
|
|
{ |
23
|
|
|
/** @var string */ |
24
|
|
|
private $role; |
25
|
|
|
|
26
|
|
|
/** @var string|null */ |
27
|
|
|
private $class; |
28
|
|
|
|
29
|
|
|
/** @var string|null */ |
30
|
|
|
private $mapper; |
31
|
|
|
|
32
|
|
|
/** @var string|null */ |
33
|
|
|
private $source; |
34
|
|
|
|
35
|
|
|
/** @var string|null */ |
36
|
|
|
private $constrain; |
37
|
|
|
|
38
|
|
|
/** @var string|null */ |
39
|
|
|
private $repository; |
40
|
|
|
|
41
|
|
|
/** @var FieldMap */ |
42
|
|
|
private $fields; |
43
|
|
|
|
44
|
|
|
/** @var RelationMap */ |
45
|
|
|
private $relations; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Entity constructor. |
49
|
|
|
*/ |
50
|
|
|
public function __construct() |
51
|
|
|
{ |
52
|
|
|
$this->fields = new FieldMap(); |
53
|
|
|
$this->relations = new RelationMap(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $role |
58
|
|
|
* @return Entity |
59
|
|
|
*/ |
60
|
|
|
public function setRole(string $role): self |
61
|
|
|
{ |
62
|
|
|
$this->role = $role; |
63
|
|
|
|
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
public function getRole(): string |
71
|
|
|
{ |
72
|
|
|
return $this->role; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/*** |
76
|
|
|
* @param string $class |
77
|
|
|
* @return Entity |
78
|
|
|
*/ |
79
|
|
|
public function setClass(string $class): self |
80
|
|
|
{ |
81
|
|
|
$this->class = $class; |
82
|
|
|
|
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return string|null |
88
|
|
|
*/ |
89
|
|
|
public function getClass(): ?string |
90
|
|
|
{ |
91
|
|
|
return $this->class; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string|null $mapper |
96
|
|
|
* @return Entity |
97
|
|
|
*/ |
98
|
|
|
public function setMapper(?string $mapper): self |
99
|
|
|
{ |
100
|
|
|
$this->mapper = $mapper; |
101
|
|
|
|
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
|
|
public function getMapper(): string |
109
|
|
|
{ |
110
|
|
|
return $this->mapper ?? Mapper::class; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param string|null $source |
115
|
|
|
* @return Entity |
116
|
|
|
*/ |
117
|
|
|
public function setSource(?string $source): self |
118
|
|
|
{ |
119
|
|
|
$this->source = $source; |
120
|
|
|
|
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
|
|
public function getSource(): string |
128
|
|
|
{ |
129
|
|
|
return $this->source ?? Source::class; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param string|null $constrain |
134
|
|
|
* @return Entity |
135
|
|
|
*/ |
136
|
|
|
public function setConstrain(?string $constrain): self |
137
|
|
|
{ |
138
|
|
|
$this->constrain = $constrain; |
139
|
|
|
|
140
|
|
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return string|null |
145
|
|
|
*/ |
146
|
|
|
public function getConstrain(): ?string |
147
|
|
|
{ |
148
|
|
|
return $this->constrain; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param string|null $repository |
153
|
|
|
* @return Entity |
154
|
|
|
*/ |
155
|
|
|
public function setRepository(?string $repository): self |
156
|
|
|
{ |
157
|
|
|
$this->repository = $repository; |
158
|
|
|
|
159
|
|
|
return $this; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @return string |
164
|
|
|
*/ |
165
|
|
|
public function getRepository(): string |
166
|
|
|
{ |
167
|
|
|
return $this->repository ?? Repository::class; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @return FieldMap |
172
|
|
|
*/ |
173
|
|
|
public function getFields(): FieldMap |
174
|
|
|
{ |
175
|
|
|
return $this->fields; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @return RelationMap |
180
|
|
|
*/ |
181
|
|
|
public function getRelations(): RelationMap |
182
|
|
|
{ |
183
|
|
|
return $this->relations; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Merge entity relations and fields. |
188
|
|
|
* |
189
|
|
|
* @param Entity $entity |
190
|
|
|
*/ |
191
|
|
|
public function merge(Entity $entity) |
192
|
|
|
{ |
193
|
|
|
foreach ($entity->getRelations() as $name => $relation) { |
194
|
|
|
if (!$this->relations->has($name)) { |
195
|
|
|
$this->relations->set($name, $relation); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
foreach ($entity->getFields() as $name => $field) { |
200
|
|
|
if (!$this->fields->has($name)) { |
201
|
|
|
$this->fields->set($name, $field); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
} |