|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* The MIT License |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright 2015 ekow. |
|
7
|
|
|
* |
|
8
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
9
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
10
|
|
|
* in the Software without restriction, including without limitation the rights |
|
11
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
12
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
13
|
|
|
* furnished to do so, subject to the following conditions: |
|
14
|
|
|
* |
|
15
|
|
|
* The above copyright notice and this permission notice shall be included in |
|
16
|
|
|
* all copies or substantial portions of the Software. |
|
17
|
|
|
* |
|
18
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
19
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
20
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
21
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
22
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
23
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
24
|
|
|
* THE SOFTWARE. |
|
25
|
|
|
*/ |
|
26
|
|
|
|
|
27
|
|
|
namespace ntentan\nibii; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Description of DataOperations |
|
31
|
|
|
* |
|
32
|
|
|
* @author ekow |
|
33
|
|
|
*/ |
|
34
|
|
|
class DataOperations { |
|
35
|
|
|
|
|
36
|
|
|
private $wrapper; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* |
|
40
|
|
|
* @var DriverAdapter |
|
41
|
|
|
*/ |
|
42
|
|
|
private $adapter; |
|
43
|
|
|
private $data; |
|
44
|
|
|
private $invalidFields; |
|
45
|
|
|
private $hasMultipleData; |
|
46
|
|
|
private $driver; |
|
47
|
|
|
private $container; |
|
48
|
|
|
|
|
49
|
|
|
const MODE_SAVE = 0; |
|
50
|
|
|
const MODE_UPDATE = 1; |
|
51
|
|
|
|
|
52
|
35 |
View Code Duplication |
public function __construct(ORMContext $context, RecordWrapper $wrapper, DriverAdapter $adapter) { |
|
53
|
35 |
|
$this->wrapper = $wrapper; |
|
54
|
35 |
|
$this->adapter = $adapter; |
|
55
|
35 |
|
$this->driver = $context->getDbContext()->getDriver(); |
|
56
|
35 |
|
$this->container = $context->getContainer(); |
|
57
|
35 |
|
} |
|
58
|
|
|
|
|
59
|
10 |
|
public function doSave($hasMultipleData) { |
|
60
|
10 |
|
$this->hasMultipleData = $hasMultipleData; |
|
61
|
10 |
|
$invalidFields = []; |
|
62
|
10 |
|
$data = $this->wrapper->getData(); |
|
63
|
10 |
|
$primaryKey = $this->wrapper->getDescription()->getPrimaryKey(); |
|
64
|
10 |
|
$singlePrimaryKey = null; |
|
|
|
|
|
|
65
|
10 |
|
$succesful = true; |
|
66
|
|
|
|
|
67
|
10 |
|
if (count($primaryKey) == 1) { |
|
68
|
10 |
|
$singlePrimaryKey = $primaryKey[0]; |
|
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
// Assign an empty array to force a validation error for empty models |
|
72
|
10 |
|
if (empty($data)) { |
|
73
|
2 |
|
$data = [[]]; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
10 |
|
$this->driver->beginTransaction(); |
|
77
|
|
|
|
|
78
|
10 |
|
foreach ($data as $i => $datum) { |
|
79
|
10 |
|
$status = $this->saveRecord($datum, $primaryKey); |
|
|
|
|
|
|
80
|
10 |
|
$data[$i] = $datum; |
|
81
|
|
|
|
|
82
|
10 |
|
if (!$status['success']) { |
|
83
|
4 |
|
$succesful = false; |
|
84
|
4 |
|
$invalidFields[$i] = $status['invalid_fields']; |
|
85
|
4 |
|
$this->driver->rollback(); |
|
86
|
10 |
|
break; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
10 |
|
if ($succesful) { |
|
91
|
6 |
|
$this->driver->commit(); |
|
92
|
|
|
} else { |
|
93
|
4 |
|
$this->assignValue($this->invalidFields, $invalidFields); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
10 |
|
$this->wrapper->setData($hasMultipleData ? $data : $data[0]); |
|
97
|
|
|
|
|
98
|
10 |
|
return $succesful; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function doValidate() { |
|
|
|
|
|
|
102
|
|
|
$record = $this->wrapper->getData()[0]; |
|
103
|
|
|
$primaryKey = $this->wrapper->getDescription()->getPrimaryKey(); |
|
104
|
|
|
$pkSet = $this->isPrimaryKeySet($primaryKey, $record); |
|
105
|
|
|
return $this->validate( |
|
106
|
|
|
$record, $pkSet ? DataOperations::MODE_UPDATE : DataOperations::MODE_SAVE |
|
107
|
|
|
); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Save an individual record. |
|
112
|
|
|
* |
|
113
|
|
|
* @param array $record The record to be saved |
|
114
|
|
|
* @param type $primaryKey The primary keys of the record |
|
115
|
|
|
* @return boolean |
|
|
|
|
|
|
116
|
|
|
*/ |
|
117
|
10 |
|
private function saveRecord(&$record, $primaryKey) { |
|
118
|
|
|
$status = [ |
|
119
|
10 |
|
'success' => true, |
|
120
|
|
|
'pk_assigned' => null, |
|
121
|
|
|
'invalid_fields' => [] |
|
122
|
|
|
]; |
|
123
|
|
|
|
|
124
|
|
|
// Determine if the primary key of the record is set. |
|
125
|
10 |
|
$pkSet = $this->isPrimaryKeySet($primaryKey, $record); |
|
126
|
|
|
|
|
127
|
|
|
// Reset the data in the model to contain only the data to be saved |
|
128
|
10 |
|
$this->wrapper->setData($record); |
|
129
|
|
|
|
|
130
|
|
|
// Run preUpdate or preSave callbacks on models and behaviours |
|
131
|
10 |
|
if ($pkSet) { |
|
132
|
2 |
|
$this->wrapper->preUpdateCallback(); |
|
133
|
2 |
|
$record = $this->wrapper->getData(); |
|
134
|
2 |
|
$record = reset($record) === false ? [] : reset($record); |
|
135
|
2 |
|
$record = $this->runBehaviours('preUpdateCallback', [$record]); |
|
136
|
|
|
} else { |
|
137
|
8 |
|
$this->wrapper->preSaveCallback(); |
|
138
|
8 |
|
$record = $this->wrapper->getData(); |
|
139
|
8 |
|
$record = reset($record) === false ? [] : reset($record); |
|
140
|
8 |
|
$record = $this->runBehaviours('preSaveCallback', [$record]); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
// Validate the data |
|
144
|
10 |
|
$validity = $this->validate( |
|
145
|
10 |
|
$record, $pkSet ? DataOperations::MODE_UPDATE : DataOperations::MODE_SAVE |
|
146
|
|
|
); |
|
147
|
|
|
|
|
148
|
|
|
// Exit if data is invalid |
|
149
|
10 |
|
if ($validity !== true) { |
|
150
|
4 |
|
$status['invalid_fields'] = $validity; |
|
151
|
4 |
|
$status['success'] = false; |
|
152
|
4 |
|
return $status; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
6 |
|
$relationships = $this->wrapper->getDescription()->getRelationships(); |
|
156
|
|
|
|
|
157
|
6 |
|
foreach($relationships as $model => $relationship) { |
|
158
|
6 |
|
if(isset($record[$model])) { |
|
159
|
6 |
|
$relationship->preSave($record, $record[$model]); |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
// Assign the data to the wrapper again |
|
164
|
6 |
|
$this->wrapper->setData($record); |
|
165
|
|
|
|
|
166
|
|
|
// Update or save the data and run post callbacks |
|
167
|
6 |
|
if ($pkSet) { |
|
168
|
2 |
|
$this->adapter->update($record); |
|
169
|
2 |
|
$this->wrapper->postUpdateCallback(); |
|
170
|
2 |
|
$this->runBehaviours('postUpdateCallback', [$record]); |
|
171
|
|
|
} else { |
|
172
|
4 |
|
$this->adapter->insert($record); |
|
173
|
4 |
|
$keyValue = $this->driver->getLastInsertId(); |
|
174
|
4 |
|
$this->wrapper->{$primaryKey[0]} = $keyValue; |
|
175
|
4 |
|
$this->wrapper->postSaveCallback($keyValue); |
|
176
|
4 |
|
$this->runBehaviours('postSaveCallback', [$record, $keyValue]); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
6 |
|
foreach($relationships as $model => $relationship) { |
|
180
|
6 |
|
if(isset($record[$model])) { |
|
181
|
6 |
|
$relationship->postSave($record, $record[$model]); |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
// Reset the data so it contains any modifications made by callbacks |
|
186
|
6 |
|
$record = $this->wrapper->getData()[0]; |
|
187
|
6 |
|
return $status; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
10 |
|
private function validate($data, $mode) { |
|
|
|
|
|
|
191
|
10 |
|
$valid = true; |
|
192
|
10 |
|
$validator = $this->container->resolve( |
|
193
|
10 |
|
ModelValidator::class, ['model' => $this->wrapper, 'mode' => $mode] |
|
194
|
|
|
); |
|
195
|
|
|
|
|
196
|
10 |
|
if (!$validator->validate($data)) { |
|
197
|
4 |
|
$valid = false; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
10 |
|
if ($valid) { |
|
201
|
6 |
|
$valid = $this->wrapper->onValidate(); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
10 |
|
if ($valid === false) { |
|
205
|
4 |
|
$valid = $validator->getInvalidFields(); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
10 |
|
return $valid; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
10 |
|
private function isPrimaryKeySet($primaryKey, $data) { |
|
212
|
10 |
|
if (is_string($primaryKey) && ($data[$primaryKey] !== null || $data[$primaryKey] !== '')) { |
|
213
|
|
|
return true; |
|
214
|
|
|
} |
|
215
|
10 |
|
foreach ($primaryKey as $keyField) { |
|
216
|
10 |
|
if (!isset($data[$keyField]) || $data[$keyField] === null || $data[$keyField] === '') { |
|
217
|
10 |
|
return false; |
|
218
|
|
|
} |
|
219
|
|
|
} |
|
220
|
2 |
|
return true; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
4 |
|
private function assignValue(&$property, $value) { |
|
224
|
4 |
|
if ($this->hasMultipleData) { |
|
225
|
|
|
$property = $value; |
|
226
|
|
|
} else { |
|
227
|
4 |
|
$property = $value[0]; |
|
228
|
|
|
} |
|
229
|
4 |
|
} |
|
230
|
|
|
|
|
231
|
|
|
public function getData() { |
|
|
|
|
|
|
232
|
|
|
return $this->data; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
10 |
|
public function getInvalidFields() { |
|
|
|
|
|
|
236
|
10 |
|
return $this->invalidFields; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
public function isItemDeletable($primaryKey, $data) { |
|
240
|
|
|
if ($this->isPrimaryKeySet($primaryKey, $data)) { |
|
241
|
|
|
return true; |
|
242
|
|
|
} else { |
|
243
|
|
|
return false; |
|
244
|
|
|
} |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
10 |
|
private function runBehaviours($event, $args) { |
|
|
|
|
|
|
248
|
10 |
|
foreach ($this->wrapper->getBehaviours() as $behaviour) { |
|
249
|
|
|
$args[0] = call_user_func_array([$behaviour, $event], $args); |
|
250
|
|
|
} |
|
251
|
10 |
|
return $args[0]; |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
} |
|
255
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.