1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mapado\RestClientSdk; |
4
|
|
|
|
5
|
|
|
use Mapado\RestClientSdk\Helper\ArrayHelper; |
6
|
|
|
use Mapado\RestClientSdk\Mapping\ClassMetadata; |
7
|
|
|
use Mapado\RestClientSdk\Mapping\Relation; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* UnitOfWork |
11
|
|
|
*/ |
12
|
|
|
class UnitOfWork |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* mapping |
16
|
|
|
* |
17
|
|
|
* @var Mapping |
18
|
|
|
*/ |
19
|
|
|
private $mapping; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* storage for every entity retrieved |
23
|
|
|
* |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
private $storage; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Constructor. |
30
|
|
|
*/ |
31
|
|
|
public function __construct(Mapping $mapping) |
32
|
|
|
{ |
33
|
1 |
|
$this->mapping = $mapping; |
34
|
1 |
|
$this->storage = []; |
35
|
1 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* getDirtyData |
39
|
|
|
* |
40
|
|
|
* return the new serialized model with only needed fields to update |
41
|
|
|
* |
42
|
|
|
* @param array $newSerializedModel |
43
|
|
|
* @param array $oldSerializedModel |
44
|
|
|
* @param ClassMetadata $classMetadata |
45
|
|
|
* |
46
|
|
|
* @return array |
47
|
|
|
*/ |
48
|
|
|
public function getDirtyData(array $newSerializedModel, array $oldSerializedModel, ClassMetadata $classMetadata) |
49
|
|
|
{ |
50
|
1 |
|
return $this->getDirtyFields($newSerializedModel, $oldSerializedModel, $classMetadata); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* registerClean |
55
|
|
|
* |
56
|
|
|
* @param string $id |
57
|
|
|
* @param object $entity |
58
|
|
|
* |
59
|
|
|
* @return UnitOfWork |
60
|
|
|
*/ |
61
|
|
|
public function registerClean($id, $entity) |
62
|
|
|
{ |
63
|
1 |
|
if (is_object($entity)) { |
64
|
1 |
|
$entityStored = clone $entity; |
65
|
1 |
|
$this->storage[$id] = $entityStored; |
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* getDirtyEntity |
73
|
|
|
* |
74
|
|
|
* @param string $id |
75
|
|
|
* |
76
|
|
|
* @return mixed |
77
|
|
|
*/ |
78
|
|
|
public function getDirtyEntity($id) |
79
|
|
|
{ |
80
|
1 |
|
if (isset($this->storage[$id])) { |
81
|
1 |
|
return $this->storage[$id]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* clear |
89
|
|
|
* |
90
|
|
|
* @param string $id |
91
|
|
|
* |
92
|
|
|
* @return UnitOfWork |
93
|
|
|
*/ |
94
|
|
|
public function clear($id) |
95
|
|
|
{ |
96
|
|
|
unset($this->storage[$id]); |
97
|
|
|
|
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* getDirtyFields |
103
|
|
|
* |
104
|
|
|
* compares serialize object and returns only modified fields |
105
|
|
|
* |
106
|
|
|
* @param array $newSerializedModel |
107
|
|
|
* @param array $oldSerializedModel |
108
|
|
|
* @param ClassMetadata $classMetadata |
109
|
|
|
* |
110
|
|
|
* @return array |
111
|
|
|
*/ |
112
|
|
|
private function getDirtyFields(array $newSerializedModel, array $oldSerializedModel, ClassMetadata $classMetadata) |
113
|
|
|
{ |
114
|
1 |
|
$dirtyFields = []; |
115
|
|
|
|
116
|
1 |
|
foreach ($newSerializedModel as $key => $value) { |
117
|
1 |
|
if (!array_key_exists($key, $oldSerializedModel)) { |
118
|
|
|
// a new key has been found, add it to the dirtyFields |
119
|
1 |
|
$dirtyFields[$key] = $value; |
120
|
1 |
|
continue; |
121
|
|
|
} |
122
|
|
|
|
123
|
1 |
|
$oldValue = $oldSerializedModel[$key]; |
124
|
|
|
|
125
|
1 |
|
$currentRelation = $classMetadata ? $classMetadata->getRelation($key) : null; |
126
|
|
|
|
127
|
1 |
|
if (!$currentRelation) { |
128
|
1 |
|
if (is_array($value) && !ArrayHelper::arraySame($value, $oldValue ?: []) |
|
|
|
|
129
|
1 |
|
|| $value !== $oldValue |
130
|
|
|
) { |
131
|
1 |
|
$dirtyFields[$key] = $value; |
132
|
|
|
} |
133
|
1 |
|
continue; |
134
|
|
|
} |
135
|
|
|
|
136
|
1 |
|
$currentClassMetadata = $this->mapping->getClassMetadata($currentRelation->getTargetEntity()); |
137
|
|
|
|
138
|
1 |
|
$idSerializedKey = $currentClassMetadata ? $currentClassMetadata->getIdSerializeKey() : null; |
139
|
|
|
|
140
|
1 |
|
if (Relation::MANY_TO_ONE === $currentRelation->getType()) { |
141
|
1 |
|
if ($value !== $oldValue) { |
142
|
1 |
|
if (is_string($value) || is_string($oldValue)) { |
143
|
1 |
|
$dirtyFields[$key] = $value; |
144
|
|
|
} else { |
145
|
1 |
|
$recursiveDiff = $this->getDirtyFields($value, $oldValue, $currentClassMetadata); |
146
|
|
|
|
147
|
1 |
|
if (!empty($recursiveDiff)) { |
148
|
1 |
|
$recursiveDiff[$idSerializedKey] = self::getEntityId($value, $idSerializedKey); |
149
|
1 |
|
$dirtyFields[$key] = $recursiveDiff; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
1 |
|
continue; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
// ONE_TO_MANY relation |
158
|
|
|
|
159
|
1 |
|
if (count($value ?? []) !== count($oldValue ?? [])) { |
160
|
|
|
// get all objects ids of new array |
161
|
1 |
|
$dirtyFields[$key] = $this->addIdentifiers($value, [], $idSerializedKey); |
162
|
|
|
} |
163
|
|
|
|
164
|
1 |
|
if (!empty($value)) { |
165
|
1 |
|
foreach ($value as $relationKey => $relationValue) { |
166
|
1 |
|
$oldRelationValue = $this->findOldRelation($relationValue, $oldValue, $currentClassMetadata); |
167
|
|
|
|
168
|
1 |
|
if ($relationValue !== $oldRelationValue) { |
169
|
1 |
|
if (is_string($relationValue) || is_string($oldRelationValue)) { |
170
|
1 |
|
$dirtyFields[$key][$relationKey] = $relationValue; |
171
|
|
|
} else { |
172
|
1 |
|
$recursiveDiff = $this->getDirtyFields($relationValue, $oldRelationValue, $currentClassMetadata); |
173
|
|
|
|
174
|
1 |
|
if (!empty($recursiveDiff)) { |
175
|
1 |
|
$idSerializedKey = $currentClassMetadata->getIdSerializeKey(); |
176
|
|
|
|
177
|
1 |
|
$entityId = self::getEntityId($relationValue, $idSerializedKey); |
178
|
1 |
|
if (null !== $entityId) { |
179
|
1 |
|
$recursiveDiff[$idSerializedKey] = $entityId; |
180
|
|
|
} |
181
|
1 |
|
$dirtyFields[$key][$relationKey] = $recursiveDiff; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
189
|
1 |
|
return $dirtyFields; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* addIdentifiers |
194
|
|
|
* |
195
|
|
|
* add defined identifiers to given model |
196
|
|
|
* |
197
|
|
|
* @param array $newSerializedModel |
198
|
|
|
* @param array $dirtyFields |
199
|
|
|
* |
200
|
|
|
* @return array |
201
|
|
|
*/ |
202
|
|
|
private function addIdentifiers($newSerializedModel, $dirtyFields, $idSerializedKey = null) |
203
|
|
|
{ |
204
|
1 |
|
foreach ($newSerializedModel as $key => $value) { |
205
|
1 |
|
if ($idSerializedKey && isset($value[$idSerializedKey])) { |
206
|
1 |
|
$dirtyFields[$key][$idSerializedKey] = $value[$idSerializedKey]; |
207
|
1 |
|
} elseif (is_string($value) && is_int($key)) { |
208
|
1 |
|
$dirtyFields[$key] = $value; |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
212
|
1 |
|
return $dirtyFields; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
private function findOldRelation($relationValue, array $oldValue, ClassMetadata $classMetadata) |
216
|
|
|
{ |
217
|
1 |
|
$idSerializedKey = $classMetadata->getIdSerializeKey(); |
218
|
|
|
|
219
|
1 |
|
$relationValueId = self::getEntityId($relationValue, $idSerializedKey); |
220
|
|
|
|
221
|
1 |
|
foreach ($oldValue as $oldRelationValue) { |
222
|
1 |
|
$oldRelationValueId = self::getEntityId($oldRelationValue, $idSerializedKey); |
223
|
|
|
|
224
|
1 |
|
if ($relationValueId === $oldRelationValueId) { |
225
|
1 |
|
return $oldRelationValue; |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|
229
|
1 |
|
return $classMetadata->getDefaultSerializedModel(); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* get entity id from string or array |
234
|
|
|
* |
235
|
|
|
* @param mixed $stringOrEntity |
236
|
|
|
* @param string $idSerializedKey |
237
|
|
|
*/ |
238
|
|
|
private static function getEntityId($stringOrEntity, $idSerializedKey) |
239
|
|
|
{ |
240
|
1 |
|
if (!is_array($stringOrEntity)) { |
241
|
1 |
|
return $stringOrEntity; |
242
|
|
|
} |
243
|
|
|
|
244
|
1 |
|
return $stringOrEntity[$idSerializedKey] |
245
|
1 |
|
?? null; |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|