1 | <?php |
||
36 | class DataOperations |
||
37 | { |
||
38 | |||
39 | private $wrapper; |
||
40 | |||
41 | /** |
||
42 | * Private instance of driver adapter |
||
43 | * |
||
44 | * @var DriverAdapter |
||
45 | */ |
||
46 | private $adapter; |
||
47 | |||
48 | /** |
||
49 | * Copy of data to be manipulated in the operations. |
||
50 | * @var array |
||
51 | */ |
||
52 | private $data; |
||
53 | |||
54 | /** |
||
55 | * Fields that contained errors after save or update operations were performed. |
||
56 | * @var array |
||
57 | */ |
||
58 | private $invalidFields = []; |
||
59 | |||
60 | /** |
||
61 | * Set to true when the model holds multiple records. |
||
62 | * @var bool |
||
63 | */ |
||
64 | private $hasMultipleData; |
||
65 | |||
66 | /** |
||
67 | * An instance of the atiaa driver. |
||
68 | * @var Driver |
||
69 | */ |
||
70 | private $driver; |
||
71 | |||
72 | /** |
||
73 | * Used to indicate save operation is in save mode to create new items. |
||
74 | */ |
||
75 | const MODE_SAVE = 0; |
||
76 | |||
77 | /** |
||
78 | * Used to indicate save operation is in update mode to update existing items. |
||
79 | */ |
||
80 | const MODE_UPDATE = 1; |
||
81 | |||
82 | /** |
||
83 | * |
||
84 | * @param \ntentan\nibii\RecordWrapper $wrapper |
||
85 | * @param Driver $driver |
||
86 | */ |
||
87 | 34 | public function __construct(RecordWrapper $wrapper, Driver $driver) |
|
93 | |||
94 | /** |
||
95 | * |
||
96 | * @param bool $hasMultipleData |
||
97 | * @return bool |
||
98 | */ |
||
99 | 10 | public function doSave(bool $hasMultipleData): bool |
|
137 | |||
138 | /** |
||
139 | * |
||
140 | * @return bool|array |
||
141 | */ |
||
142 | public function doValidate() |
||
149 | |||
150 | /** |
||
151 | * Save an individual record. |
||
152 | * |
||
153 | * @param array $record The record to be saved |
||
154 | * @param array $primaryKey The primary keys of the record |
||
155 | * @return array |
||
156 | */ |
||
157 | 10 | private function saveRecord(array &$record, array $primaryKey): array |
|
158 | { |
||
159 | $status = [ |
||
160 | 10 | 'success' => true, |
|
161 | 'pk_assigned' => null, |
||
162 | 'invalid_fields' => [] |
||
163 | ]; |
||
164 | |||
165 | // Determine if the primary key of the record is set. |
||
166 | 10 | $pkSet = $this->isPrimaryKeySet($primaryKey, $record); |
|
167 | |||
168 | // Reset the data in the model to contain only the data to be saved |
||
169 | 10 | $this->wrapper->setData($record); |
|
170 | |||
171 | // Run preUpdate or preSave callbacks on models and behaviours |
||
172 | 10 | if ($pkSet) { |
|
173 | 2 | $this->wrapper->preUpdateCallback(); |
|
174 | 2 | $record = $this->wrapper->getData(); |
|
175 | 2 | $record = reset($record) === false ? [] : reset($record); |
|
176 | } else { |
||
177 | 8 | $this->wrapper->preSaveCallback(); |
|
178 | 8 | $record = $this->wrapper->getData(); |
|
179 | 8 | $record = reset($record) === false ? [] : reset($record); |
|
180 | } |
||
181 | |||
182 | // Validate the data |
||
183 | 10 | $validity = $this->validate($record, $pkSet ? DataOperations::MODE_UPDATE : DataOperations::MODE_SAVE); |
|
184 | |||
185 | // Exit if data is invalid |
||
186 | 10 | if ($validity !== true) { |
|
187 | 8 | $status['invalid_fields'] = $validity; |
|
188 | 8 | $status['success'] = false; |
|
189 | 8 | return $status; |
|
190 | } |
||
191 | |||
192 | // Save any relationships that are attached to the data |
||
193 | 2 | $relationships = $this->wrapper->getDescription()->getRelationships(); |
|
194 | 2 | $presentRelationships = []; |
|
195 | |||
196 | 2 | foreach ($relationships ?? [] as $model => $relationship) { |
|
197 | 2 | if (isset($record[$model])) { |
|
198 | $relationship->preSave($record, $record[$model]); |
||
199 | 2 | $presentRelationships[$model] = $relationship; |
|
200 | } |
||
201 | } |
||
202 | |||
203 | // Assign the data to the wrapper again |
||
204 | 2 | $this->wrapper->setData($record); |
|
205 | |||
206 | // Update or save the data and run post callbacks |
||
207 | 2 | if ($pkSet) { |
|
208 | 2 | $this->adapter->update($record); |
|
209 | 2 | $this->wrapper->postUpdateCallback(); |
|
210 | } else { |
||
211 | $this->adapter->insert($record); |
||
212 | $keyValue = $this->driver->getLastInsertId(); |
||
213 | $this->wrapper->{$primaryKey[0]} = $keyValue; |
||
214 | $this->wrapper->postSaveCallback($keyValue); |
||
215 | } |
||
216 | |||
217 | // Reset the data so it contains any modifications made by callbacks |
||
218 | 2 | $record = $this->wrapper->getData()[0]; |
|
219 | 2 | foreach ($presentRelationships as $model => $relationship) { |
|
220 | $relationship->postSave($record); |
||
221 | } |
||
222 | |||
223 | 2 | return $status; |
|
224 | } |
||
225 | |||
226 | /** |
||
227 | * |
||
228 | * @param array $data |
||
229 | * @param int $mode |
||
230 | * @return bool|array |
||
231 | */ |
||
232 | 10 | private function validate(array $data, int $mode) |
|
243 | |||
244 | /** |
||
245 | * |
||
246 | * @param string|array $primaryKey |
||
247 | * @param array $data |
||
248 | * @return bool |
||
249 | */ |
||
250 | 10 | private function isPrimaryKeySet($primaryKey, array $data) : bool |
|
262 | |||
263 | /** |
||
264 | * |
||
265 | * @param mixed $property |
||
266 | * @param mixed $value |
||
267 | */ |
||
268 | 8 | private function assignValue(&$property, $value) : void |
|
276 | |||
277 | /** |
||
278 | * |
||
279 | * @return array |
||
280 | */ |
||
281 | public function getData() : array |
||
285 | |||
286 | /** |
||
287 | * |
||
288 | * @return array |
||
289 | */ |
||
290 | 10 | public function getInvalidFields() : array |
|
294 | |||
295 | /** |
||
296 | * |
||
297 | * @param string $primaryKey |
||
298 | * @param array $data |
||
299 | * @return bool |
||
300 | */ |
||
301 | public function isItemDeletable(string $primaryKey, array $data) : bool |
||
309 | |||
310 | } |
||
311 |