1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mapado\RestClientSdk; |
4
|
|
|
|
5
|
|
|
use Mapado\RestClientSdk\Mapping\ClassMetadata; |
6
|
|
|
use Mapado\RestClientSdk\Mapping; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* UnitOfWork |
10
|
|
|
*/ |
11
|
|
|
class UnitOfWork |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* mapping |
15
|
|
|
* |
16
|
|
|
* @var Mapping |
17
|
|
|
* @access private |
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
|
|
|
$this->mapping = $mapping; |
34
|
|
|
$this->storage = []; |
35
|
|
|
} |
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
|
|
|
* @access public |
46
|
|
|
* @return array |
47
|
|
|
*/ |
48
|
|
|
public function getDirtyData(array $newSerializedModel, array $oldSerializedModel, ClassMetadata $classMetadata) |
49
|
|
|
{ |
50
|
|
|
return $this->getDirtyFields($newSerializedModel, $oldSerializedModel, $classMetadata); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* registerClean |
55
|
|
|
* |
56
|
|
|
* @param string $id |
57
|
|
|
* @param object $entity |
58
|
|
|
* @access public |
59
|
|
|
* @return UnitOfWork |
60
|
|
|
*/ |
61
|
|
|
public function registerClean($id, $entity) |
62
|
|
|
{ |
63
|
|
|
if (is_object($entity)) { |
64
|
|
|
$entityStored = clone $entity; |
65
|
|
|
$this->storage[$id] = $entityStored; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* getDirtyEntity |
73
|
|
|
* |
74
|
|
|
* @param string $id |
75
|
|
|
* @access public |
76
|
|
|
* @return mixed |
77
|
|
|
*/ |
78
|
|
|
public function getDirtyEntity($id) |
79
|
|
|
{ |
80
|
|
|
if (isset($this->storage[$id])) { |
81
|
|
|
return $this->storage[$id]; |
82
|
|
|
} |
83
|
|
|
return null; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* clear |
88
|
|
|
* |
89
|
|
|
* @param string $id |
90
|
|
|
* @access public |
91
|
|
|
* @return UnitOfWork |
92
|
|
|
*/ |
93
|
|
|
public function clear($id) |
94
|
|
|
{ |
95
|
|
|
unset($this->storage[$id]); |
96
|
|
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* getDirtyFields |
101
|
|
|
* |
102
|
|
|
* compares serialize object and returns only modified fields |
103
|
|
|
* |
104
|
|
|
* @param array $newArrayModel |
|
|
|
|
105
|
|
|
* @param array $oldSerializedModel |
106
|
|
|
* @access private |
107
|
|
|
* @return array |
108
|
|
|
*/ |
109
|
|
|
private function getDirtyFields(array $newSerializedModel, array $oldSerializedModel, ClassMetadata $classMetadata = null) |
110
|
|
|
{ |
111
|
|
|
$dirtyFields = []; |
112
|
|
|
|
113
|
|
|
foreach ($newSerializedModel as $key => $value) { |
114
|
|
|
if (array_key_exists($key, $oldSerializedModel)) { |
115
|
|
|
if (is_array($value)) { |
116
|
|
|
$currentClassMetadata = $classMetadata->getRelation($key) ? $this->mapping->getClassMetadata($classMetadata->getRelation($key)->getTargetEntity()) : null; |
|
|
|
|
117
|
|
|
$idSerializedKey = $currentClassMetadata ? $currentClassMetadata->getIdSerializeKey() : null; |
118
|
|
|
$recursiveDiff = $this->getDirtyFields($value, $oldSerializedModel[$key], $currentClassMetadata); |
119
|
|
|
if (count($recursiveDiff)) { |
120
|
|
|
$dirtyFields[$key] = $recursiveDiff; |
121
|
|
|
$dirtyFields[$key] = $this->addIdentifiers($value, $dirtyFields[$key], $idSerializedKey); |
122
|
|
|
|
123
|
|
|
// if theres only ids not objects, keep them |
124
|
|
|
foreach ($value as $valueKey => $valueId) { |
125
|
|
|
if (is_string($valueId) && is_int($valueKey)) { |
126
|
|
|
$dirtyFields[$key][$valueKey] = $valueId; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} elseif (count($value) != count($oldSerializedModel[$key])) { |
130
|
|
|
// get all objects ids of new array |
131
|
|
|
$dirtyFields[$key] = []; |
132
|
|
|
$dirtyFields[$key] = $this->addIdentifiers($value, $dirtyFields[$key], $idSerializedKey); |
133
|
|
|
} |
134
|
|
|
} else { |
135
|
|
|
if ($value != $oldSerializedModel[$key]) { |
136
|
|
|
$dirtyFields[$key] = $value; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} else { |
140
|
|
|
$dirtyFields[$key] = $value; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $dirtyFields; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* addIdentifiers |
149
|
|
|
* |
150
|
|
|
* add defined identifiers to given model |
151
|
|
|
* |
152
|
|
|
* @param array $newSerializedModel |
153
|
|
|
* @param array $dirtyFields |
154
|
|
|
* @access private |
155
|
|
|
* @return array |
156
|
|
|
*/ |
157
|
|
|
private function addIdentifiers($newSerializedModel, $dirtyFields, $idSerializedKey = null) |
158
|
|
|
{ |
159
|
|
|
foreach ($newSerializedModel as $key => $value) { |
160
|
|
|
if ($idSerializedKey && isset($value[$idSerializedKey])) { |
161
|
|
|
$dirtyFields[$key][$idSerializedKey] = $value[$idSerializedKey]; |
162
|
|
|
} elseif (is_string($value) && is_int($key)) { |
163
|
|
|
$dirtyFields[$key] = $value; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return $dirtyFields; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.