|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Isolate\UnitOfWork\Entity; |
|
4
|
|
|
|
|
5
|
|
|
use Isolate\UnitOfWork\Command\EditCommandHandler; |
|
6
|
|
|
use Isolate\UnitOfWork\Command\NewCommandHandler; |
|
7
|
|
|
use Isolate\UnitOfWork\Command\RemoveCommandHandler; |
|
8
|
|
|
use Isolate\UnitOfWork\Entity\Definition\IdentificationStrategy; |
|
9
|
|
|
use Isolate\UnitOfWork\Entity\Definition\Identity; |
|
10
|
|
|
use Isolate\UnitOfWork\Entity\Definition\Property; |
|
11
|
|
|
use Isolate\UnitOfWork\Exception\InvalidArgumentException; |
|
12
|
|
|
use Isolate\UnitOfWork\Exception\NotExistingPropertyException; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @api |
|
16
|
|
|
*/ |
|
17
|
|
|
class Definition |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var ClassName |
|
21
|
|
|
*/ |
|
22
|
|
|
private $className; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var Identity |
|
26
|
|
|
*/ |
|
27
|
|
|
private $idDefinition; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var NewCommandHandler|null |
|
31
|
|
|
*/ |
|
32
|
|
|
private $newCommandHandler; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var EditCommandHandler|null |
|
36
|
|
|
*/ |
|
37
|
|
|
private $editCommandHandler; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var RemoveCommandHandler|null |
|
41
|
|
|
*/ |
|
42
|
|
|
private $removeCommandHandler; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var array |
|
46
|
|
|
*/ |
|
47
|
|
|
private $observedProperties; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var IdentificationStrategy |
|
51
|
|
|
*/ |
|
52
|
|
|
private $identificationStrategy; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param ClassName $className |
|
56
|
|
|
* @param Identity $idDefinition |
|
57
|
|
|
* @param IdentificationStrategy $identificationStrategy |
|
58
|
|
|
*/ |
|
59
|
|
|
public function __construct(ClassName $className, Identity $idDefinition, IdentificationStrategy $identificationStrategy = null) |
|
60
|
|
|
{ |
|
61
|
|
|
$this->className = $className; |
|
62
|
|
|
$this->idDefinition = $idDefinition; |
|
63
|
|
|
$this->observedProperties = []; |
|
64
|
|
|
$this->identificationStrategy = $identificationStrategy ?: new IdentificationStrategy\PropertyValue($idDefinition); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param Property[] $properties |
|
69
|
|
|
* @throws InvalidArgumentException |
|
70
|
|
|
* @throws NotExistingPropertyException |
|
71
|
|
|
* |
|
72
|
|
|
* @api |
|
73
|
|
|
*/ |
|
74
|
|
|
public function setObserved(array $properties) |
|
75
|
|
|
{ |
|
76
|
|
|
foreach ($properties as $property) { |
|
77
|
|
|
if (!$property instanceof Property) { |
|
78
|
|
|
throw new InvalidArgumentException("Each observed property needs to be an instance of Property"); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
$this->validatePropertyPaths((string) $this->className, $this->idDefinition, $properties); |
|
82
|
|
|
$this->observedProperties = array_unique($properties); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param Property $property |
|
87
|
|
|
* @throws InvalidArgumentException |
|
88
|
|
|
* @throws NotExistingPropertyException |
|
89
|
|
|
* |
|
90
|
|
|
* @api |
|
91
|
|
|
*/ |
|
92
|
|
|
public function addToObserved(Property $property) |
|
93
|
|
|
{ |
|
94
|
|
|
$this->validatePropertyPaths((string) $this->className, $this->idDefinition, [$property]); |
|
95
|
|
|
$this->observedProperties[] = $property; |
|
96
|
|
|
$this->observedProperties = array_unique($this->observedProperties); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @return Property[]|array |
|
101
|
|
|
* |
|
102
|
|
|
* @api |
|
103
|
|
|
*/ |
|
104
|
|
|
public function getObservedProperties() |
|
105
|
|
|
{ |
|
106
|
|
|
return $this->observedProperties; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @return ClassName |
|
111
|
|
|
* |
|
112
|
|
|
* @api |
|
113
|
|
|
*/ |
|
114
|
|
|
public function getClassName() |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->className; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @return Identity |
|
121
|
|
|
* |
|
122
|
|
|
* @api |
|
123
|
|
|
*/ |
|
124
|
|
|
public function getIdDefinition() |
|
125
|
|
|
{ |
|
126
|
|
|
return $this->idDefinition; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param $entity |
|
131
|
|
|
* @return bool |
|
132
|
|
|
* |
|
133
|
|
|
* @api |
|
134
|
|
|
*/ |
|
135
|
|
|
public function fitsFor($entity) |
|
136
|
|
|
{ |
|
137
|
|
|
return $this->className->isClassOf($entity); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param NewCommandHandler $commandHandler |
|
142
|
|
|
* |
|
143
|
|
|
* @api |
|
144
|
|
|
*/ |
|
145
|
|
|
public function setNewCommandHandler(NewCommandHandler $commandHandler) |
|
146
|
|
|
{ |
|
147
|
|
|
$this->newCommandHandler = $commandHandler; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @return bool |
|
152
|
|
|
* |
|
153
|
|
|
* @api |
|
154
|
|
|
*/ |
|
155
|
|
|
public function hasNewCommandHandler() |
|
156
|
|
|
{ |
|
157
|
|
|
return isset($this->newCommandHandler); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @return NewCommandHandler|null |
|
162
|
|
|
* |
|
163
|
|
|
* @api |
|
164
|
|
|
*/ |
|
165
|
|
|
public function getNewCommandHandler() |
|
166
|
|
|
{ |
|
167
|
|
|
return $this->newCommandHandler; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @param EditCommandHandler $commandHandler |
|
172
|
|
|
* |
|
173
|
|
|
* @api |
|
174
|
|
|
*/ |
|
175
|
|
|
public function setEditCommandHandler(EditCommandHandler $commandHandler) |
|
176
|
|
|
{ |
|
177
|
|
|
$this->editCommandHandler = $commandHandler; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* @return bool |
|
182
|
|
|
* |
|
183
|
|
|
* @api |
|
184
|
|
|
*/ |
|
185
|
|
|
public function hasEditCommandHandler() |
|
186
|
|
|
{ |
|
187
|
|
|
return isset($this->editCommandHandler); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* @return EditCommandHandler|null |
|
192
|
|
|
* |
|
193
|
|
|
* @api |
|
194
|
|
|
*/ |
|
195
|
|
|
public function getEditCommandHandler() |
|
196
|
|
|
{ |
|
197
|
|
|
return $this->editCommandHandler; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* @param RemoveCommandHandler $commandHandler |
|
202
|
|
|
* |
|
203
|
|
|
* @api |
|
204
|
|
|
*/ |
|
205
|
|
|
public function setRemoveCommandHandler(RemoveCommandHandler $commandHandler) |
|
206
|
|
|
{ |
|
207
|
|
|
$this->removeCommandHandler = $commandHandler; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* @return bool |
|
212
|
|
|
* |
|
213
|
|
|
* @api |
|
214
|
|
|
*/ |
|
215
|
|
|
public function hasRemoveCommandHandler() |
|
216
|
|
|
{ |
|
217
|
|
|
return isset($this->removeCommandHandler); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* @return RemoveCommandHandler|null |
|
222
|
|
|
* |
|
223
|
|
|
* @api |
|
224
|
|
|
*/ |
|
225
|
|
|
public function getRemoveCommandHandler() |
|
226
|
|
|
{ |
|
227
|
|
|
return $this->removeCommandHandler; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* @return IdentificationStrategy|IdentificationStrategy |
|
232
|
|
|
* |
|
233
|
|
|
* @api |
|
234
|
|
|
*/ |
|
235
|
|
|
public function getIdentityStrategy() |
|
236
|
|
|
{ |
|
237
|
|
|
return $this->identificationStrategy; |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* @param $className |
|
242
|
|
|
* @param Identity $idDefinition |
|
243
|
|
|
* @param array $observedProperties |
|
244
|
|
|
* @throws InvalidArgumentException |
|
245
|
|
|
* @throws NotExistingPropertyException |
|
246
|
|
|
*/ |
|
247
|
|
|
private function validatePropertyPaths($className, Identity $idDefinition, array $observedProperties) |
|
248
|
|
|
{ |
|
249
|
|
|
$reflection = new \ReflectionClass($className); |
|
250
|
|
|
foreach ($observedProperties as $property) { |
|
251
|
|
|
$propertyName = $property->getName(); |
|
252
|
|
|
if ($idDefinition->isEqual($propertyName)) { |
|
253
|
|
|
throw new InvalidArgumentException("Id definition property path can't be between observer properties."); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
if (!$reflection->hasProperty($propertyName)) { |
|
257
|
|
|
throw new NotExistingPropertyException(sprintf( |
|
258
|
|
|
"Property \"%s\" does not exists in \"%s\" class.", |
|
259
|
|
|
$propertyName, |
|
260
|
|
|
$className |
|
261
|
|
|
)); |
|
262
|
|
|
} |
|
263
|
|
|
} |
|
264
|
|
|
} |
|
265
|
|
|
} |
|
266
|
|
|
|