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
|
|
|
* @param array $newSerializedModel |
41
|
|
|
* @param array $oldSerializedModel |
42
|
|
|
* @access public |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
|
|
public function getDirtyData(array $newSerializedModel, array $oldSerializedModel, ClassMetadata $classMetadata) |
46
|
|
|
{ |
47
|
|
|
return $this->getDirtyFields($newSerializedModel, $oldSerializedModel, $classMetadata); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* registerClean |
52
|
|
|
* |
53
|
|
|
* @param string $id |
54
|
|
|
* @param object $entity |
55
|
|
|
* @access public |
56
|
|
|
* @return UnitOfWork |
57
|
|
|
*/ |
58
|
|
|
public function registerClean($id, $entity) |
59
|
|
|
{ |
60
|
|
|
if (is_object($entity)) { |
61
|
|
|
$entityStored = clone $entity; |
62
|
|
|
$this->storage[$id] = $entityStored; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* getDirtyEntity |
70
|
|
|
* |
71
|
|
|
* @param string $id |
72
|
|
|
* @access public |
73
|
|
|
* @return mixed |
74
|
|
|
*/ |
75
|
|
|
public function getDirtyEntity($id) |
76
|
|
|
{ |
77
|
|
|
if (isset($this->storage[$id])) { |
78
|
|
|
return $this->storage[$id]; |
79
|
|
|
} |
80
|
|
|
return null; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* clear |
85
|
|
|
* |
86
|
|
|
* @param string $id |
87
|
|
|
* @access public |
88
|
|
|
* @return UnitOfWork |
89
|
|
|
*/ |
90
|
|
|
public function clear($id) |
91
|
|
|
{ |
92
|
|
|
unset($this->storage[$id]); |
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* getDirtyFields |
98
|
|
|
* |
99
|
|
|
* @param array $newArrayModel |
|
|
|
|
100
|
|
|
* @param array $oldSerializedModel |
101
|
|
|
* @access private |
102
|
|
|
* @return array |
103
|
|
|
*/ |
104
|
|
|
private function getDirtyFields(array $newSerializedModel, array $oldSerializedModel, ClassMetadata $classMetadata = null) |
105
|
|
|
{ |
106
|
|
|
$dirtyFields = []; |
107
|
|
|
|
108
|
|
|
foreach ($newSerializedModel as $key => $value) { |
109
|
|
|
if (array_key_exists($key, $oldSerializedModel)) { |
110
|
|
|
if (is_array($value)) { |
111
|
|
|
$currentClassMetadata = $classMetadata->getRelation($key) ? $this->mapping->getClassMetadata($classMetadata->getRelation($key)->getTargetEntity()) : null; |
|
|
|
|
112
|
|
|
$idSerializedKey = $currentClassMetadata ? $currentClassMetadata->getIdSerializeKey() : null; |
113
|
|
|
$recursiveDiff = $this->getDirtyFields($value, $oldSerializedModel[$key], $currentClassMetadata); |
114
|
|
|
if (count($recursiveDiff)) { |
115
|
|
|
$dirtyFields[$key] = $recursiveDiff; |
116
|
|
|
$dirtyFields[$key] = $this->addIdentifiers($value, $dirtyFields[$key], $idSerializedKey); |
117
|
|
|
|
118
|
|
|
//if theres only ids not objects, keep them |
119
|
|
|
foreach ($value as $valueKey => $valueId) { |
120
|
|
|
if (is_string($valueId) && is_int($valueKey)) { |
121
|
|
|
$dirtyFields[$key][$valueKey] = $valueId; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
} elseif (count($value) != count($oldSerializedModel[$key])) { |
125
|
|
|
//get all objects ids of new array |
126
|
|
|
$dirtyFields[$key] = []; |
127
|
|
|
$dirtyFields[$key] = $this->addIdentifiers($value, $dirtyFields[$key], $idSerializedKey); |
128
|
|
|
} |
129
|
|
|
} else { |
130
|
|
|
if ($value != $oldSerializedModel[$key]) { |
131
|
|
|
$dirtyFields[$key] = $value; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
} else { |
135
|
|
|
$dirtyFields[$key] = $value; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $dirtyFields; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* addIdentifiers |
144
|
|
|
* |
145
|
|
|
* @param array $newSerializedModel |
146
|
|
|
* @param array $dirtyFields |
147
|
|
|
* @access private |
148
|
|
|
* @return array |
149
|
|
|
*/ |
150
|
|
|
private function addIdentifiers($newSerializedModel, $dirtyFields, $idSerializedKey = null) |
151
|
|
|
{ |
152
|
|
|
foreach ($newSerializedModel as $key => $value) { |
153
|
|
|
if ($idSerializedKey && isset($value[$idSerializedKey])) { |
154
|
|
|
$dirtyFields[$key][$idSerializedKey] = $value[$idSerializedKey]; |
155
|
|
|
} elseif (is_string($value) && is_int($key)) { |
156
|
|
|
$dirtyFields[$key] = $value; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return $dirtyFields; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
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.