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
|
|
|
use ntentan\utils\Utils; |
30
|
|
|
use ntentan\atiaa\Db; |
31
|
|
|
use ntentan\panie\InjectionContainer; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Description of DataOperations |
35
|
|
|
* |
36
|
|
|
* @author ekow |
37
|
|
|
*/ |
38
|
|
|
class DataOperations { |
39
|
|
|
|
40
|
|
|
private $wrapper; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* |
44
|
|
|
* @var DriverAdapter |
45
|
|
|
*/ |
46
|
|
|
private $adapter; |
47
|
|
|
private $data; |
48
|
|
|
private $invalidFields; |
49
|
|
|
private $hasMultipleData; |
50
|
|
|
|
51
|
|
|
const MODE_SAVE = 0; |
52
|
|
|
const MODE_UPDATE = 1; |
53
|
|
|
|
54
|
36 |
|
public function __construct($wrapper, $adapter) { |
55
|
36 |
|
$this->wrapper = $wrapper; |
56
|
36 |
|
$this->adapter = $adapter; |
57
|
36 |
|
} |
58
|
|
|
|
59
|
10 |
|
public function doSave($hasMultipleData) { |
60
|
10 |
|
$this->hasMultipleData = $hasMultipleData; |
61
|
10 |
|
$invalidFields = []; |
62
|
10 |
|
$data = $this->wrapper->getData(); |
63
|
|
|
//$this->adapter->setModel($this->wrapper); |
|
|
|
|
64
|
10 |
|
$primaryKey = $this->wrapper->getDescription()->getPrimaryKey(); |
65
|
10 |
|
$singlePrimaryKey = null; |
|
|
|
|
66
|
10 |
|
$succesful = true; |
67
|
|
|
|
68
|
10 |
|
if (count($primaryKey) == 1) { |
69
|
10 |
|
$singlePrimaryKey = $primaryKey[0]; |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// Assign an empty array to force a validation error for empty models |
73
|
10 |
|
if (empty($data)) { |
74
|
2 |
|
$data = [[]]; |
75
|
|
|
} |
76
|
|
|
|
77
|
10 |
|
Db::getDriver()->beginTransaction(); |
78
|
|
|
|
79
|
10 |
|
foreach ($data as $i => $datum) { |
80
|
10 |
|
$status = $this->saveRecord($datum, $primaryKey); |
81
|
10 |
|
$data[$i] = $datum; |
82
|
|
|
|
83
|
10 |
|
if (!$status['success']) { |
84
|
4 |
|
$succesful = false; |
85
|
4 |
|
$invalidFields[$i] = $status['invalid_fields']; |
86
|
4 |
|
Db::getDriver()->rollback(); |
87
|
10 |
|
break; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
10 |
|
if ($succesful) { |
92
|
6 |
|
Db::getDriver()->commit(); |
93
|
|
|
} else { |
94
|
4 |
|
$this->assignValue($this->invalidFields, $invalidFields); |
95
|
|
|
} |
96
|
|
|
|
97
|
10 |
|
$this->wrapper->setData($hasMultipleData ? $data : $data[0]); |
98
|
|
|
|
99
|
10 |
|
return $succesful; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Save an individual record. |
104
|
|
|
* |
105
|
|
|
* @param array $record The record to be saved |
106
|
|
|
* @param type $primaryKey The primary keys of the record |
107
|
|
|
* @return boolean |
|
|
|
|
108
|
|
|
*/ |
109
|
10 |
|
private function saveRecord(&$record, $primaryKey) { |
110
|
|
|
$status = [ |
111
|
10 |
|
'success' => true, |
112
|
|
|
'pk_assigned' => null, |
113
|
|
|
'invalid_fields' => [] |
114
|
|
|
]; |
115
|
|
|
|
116
|
|
|
// Determine if the primary key of the record is set. |
117
|
10 |
|
$pkSet = $this->isPrimaryKeySet($primaryKey, $record); |
118
|
|
|
|
119
|
|
|
// Reset the data in the model to contain only the data to be saved |
120
|
10 |
|
$this->wrapper->setData($record); |
121
|
|
|
|
122
|
|
|
// Run preUpdate or preSave callbacks on models and behaviours |
123
|
10 |
|
if ($pkSet) { |
124
|
2 |
|
$this->wrapper->preUpdateCallback(); |
125
|
2 |
|
$record = $this->wrapper->getData(); |
126
|
2 |
|
$record = reset($record) === false ? [] : reset($record); |
127
|
2 |
|
$record = $this->runBehaviours('preUpdateCallback', [$record]); |
128
|
|
|
} else { |
129
|
8 |
|
$this->wrapper->preSaveCallback(); |
130
|
8 |
|
$record = $this->wrapper->getData(); |
131
|
8 |
|
$record = reset($record) === false ? [] : reset($record); |
132
|
8 |
|
$record = $this->runBehaviours('preSaveCallback', [$record]); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
// Validate the data |
136
|
10 |
|
$validity = $this->validate( |
137
|
10 |
|
$record, $pkSet ? DataOperations::MODE_UPDATE : DataOperations::MODE_SAVE |
138
|
|
|
); |
139
|
|
|
|
140
|
|
|
// Exit if data is invalid |
141
|
10 |
|
if ($validity !== true) { |
142
|
4 |
|
$status['invalid_fields'] = $validity; |
143
|
4 |
|
$status['success'] = false; |
144
|
4 |
|
return $status; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
// Assign the data to the wrapper again |
148
|
6 |
|
$this->wrapper->setData($record); |
149
|
|
|
|
150
|
|
|
// Update or save the data and run post callbacks |
151
|
6 |
|
if ($pkSet) { |
152
|
2 |
|
$this->adapter->update($record); |
153
|
2 |
|
$this->wrapper->postUpdateCallback(); |
154
|
2 |
|
$this->runBehaviours('postUpdateCallback', [$record]); |
155
|
|
|
} else { |
156
|
4 |
|
$this->adapter->insert($record); |
157
|
4 |
|
$keyValue = Db::getDriver()->getLastInsertId(); |
158
|
4 |
|
$this->wrapper->{$primaryKey[0]} = $keyValue; |
159
|
4 |
|
$this->wrapper->postSaveCallback($keyValue); |
160
|
4 |
|
$this->runBehaviours('postSaveCallback', [$record, $keyValue]); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
// Reset the data so it contains any modifications made by callbacks |
164
|
6 |
|
$record = $this->wrapper->getData()[0]; |
165
|
6 |
|
return $status; |
166
|
|
|
} |
167
|
|
|
|
168
|
10 |
|
private function validate($data, $mode) { |
|
|
|
|
169
|
10 |
|
$valid = true; |
170
|
10 |
|
$validator = \ntentan\panie\InjectionContainer::resolve( |
171
|
10 |
|
ModelValidator::class, ['model' => $this->wrapper, 'mode' => $mode] |
172
|
|
|
); |
173
|
|
|
|
174
|
10 |
|
if (!$validator->validate($data)) { |
175
|
4 |
|
$valid = false; |
176
|
|
|
} |
177
|
|
|
|
178
|
10 |
|
if ($valid) { |
179
|
6 |
|
$valid = $this->wrapper->onValidate(); |
180
|
|
|
} |
181
|
|
|
|
182
|
10 |
|
if ($valid === false) { |
183
|
4 |
|
$valid = $validator->getInvalidFields(); |
184
|
|
|
} |
185
|
|
|
|
186
|
10 |
|
return $valid; |
187
|
|
|
} |
188
|
|
|
|
189
|
10 |
|
private function isPrimaryKeySet($primaryKey, $data) { |
190
|
10 |
|
if (is_string($primaryKey)) { |
191
|
|
|
if (isset($data[$primaryKey])) { |
192
|
|
|
return true; |
193
|
|
|
} |
194
|
|
|
} |
195
|
10 |
|
foreach ($primaryKey as $keyField) { |
196
|
10 |
|
if (!isset($data[$keyField])) { |
197
|
8 |
|
break; |
198
|
|
|
} |
199
|
2 |
|
if ($data[$keyField] !== '' && $data[$keyField] !== null) { |
200
|
2 |
|
return true; |
201
|
|
|
} |
202
|
|
|
} |
203
|
8 |
|
return false; |
204
|
|
|
} |
205
|
|
|
|
206
|
4 |
|
private function assignValue(&$property, $value) { |
207
|
4 |
|
if ($this->hasMultipleData) { |
208
|
|
|
$property = $value; |
209
|
|
|
} else { |
210
|
4 |
|
$property = $value[0]; |
211
|
|
|
} |
212
|
4 |
|
} |
213
|
|
|
|
214
|
|
|
public function getData() { |
|
|
|
|
215
|
|
|
return $this->data; |
216
|
|
|
} |
217
|
|
|
|
218
|
10 |
|
public function getInvalidFields() { |
|
|
|
|
219
|
10 |
|
return $this->invalidFields; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
public function isItemDeletable($primaryKey, $data) { |
223
|
|
|
if ($this->isPrimaryKeySet($primaryKey, $data)) { |
224
|
|
|
return true; |
225
|
|
|
} else { |
226
|
|
|
return false; |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
230
|
10 |
|
private function runBehaviours($event, $args) { |
|
|
|
|
231
|
10 |
|
foreach ($this->wrapper->getBehaviours() as $behaviour) { |
232
|
|
|
$args[0] = call_user_func_array([$behaviour, $event], $args); |
233
|
|
|
} |
234
|
10 |
|
return $args[0]; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
} |
238
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.