|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kalanis\kw_mapper\Mappers; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use kalanis\kw_mapper\MapperException; |
|
7
|
|
|
use kalanis\kw_mapper\Records\ARecord; |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class AMapper |
|
12
|
|
|
* @package kalanis\kw_mapper\Mappers |
|
13
|
|
|
* Basic things you want to mapper do |
|
14
|
|
|
*/ |
|
15
|
|
|
abstract class AMapper |
|
16
|
|
|
{ |
|
17
|
|
|
use Shared\TEntityChanged; |
|
18
|
|
|
use Shared\TForeignKey; |
|
19
|
|
|
use Shared\TPrimaryKey; |
|
20
|
|
|
use Shared\TRelations; |
|
21
|
|
|
use Shared\TSource; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @throws MapperException |
|
25
|
|
|
*/ |
|
26
|
48 |
|
public function __construct() |
|
27
|
|
|
{ |
|
28
|
48 |
|
$this->setMap(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Set which record maps on which on service |
|
33
|
|
|
* @throws MapperException |
|
34
|
|
|
*/ |
|
35
|
|
|
abstract protected function setMap(): void; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Alias under which is mapper available in the heap of the others |
|
39
|
|
|
* @return string |
|
40
|
|
|
*/ |
|
41
|
|
|
abstract public function getAlias(): string; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Things to do before saving record |
|
45
|
|
|
* @param ARecord $record |
|
46
|
|
|
* @return bool |
|
47
|
|
|
*/ |
|
48
|
19 |
|
protected function beforeSave(/** @scrutinizer ignore-unused */ ARecord $record): bool |
|
49
|
|
|
{ |
|
50
|
19 |
|
return true; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Things to do after saving record |
|
55
|
|
|
* @param ARecord $record |
|
56
|
|
|
* @return bool |
|
57
|
|
|
*/ |
|
58
|
13 |
|
protected function afterSave(/** @scrutinizer ignore-unused */ ARecord $record): bool |
|
59
|
|
|
{ |
|
60
|
13 |
|
return true; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Things to do before deleting record with params |
|
65
|
|
|
* @param ARecord $record |
|
66
|
|
|
* @return bool |
|
67
|
|
|
*/ |
|
68
|
13 |
|
protected function beforeDelete(/** @scrutinizer ignore-unused */ ARecord $record): bool |
|
69
|
|
|
{ |
|
70
|
13 |
|
return true; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Things to do after deleting record with params |
|
75
|
|
|
* @param ARecord $record |
|
76
|
|
|
* @return bool |
|
77
|
|
|
*/ |
|
78
|
9 |
|
protected function afterDelete(/** @scrutinizer ignore-unused */ ARecord $record): bool |
|
79
|
|
|
{ |
|
80
|
9 |
|
return true; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Things to do before updating record |
|
85
|
|
|
* @param ARecord $record |
|
86
|
|
|
* @return bool |
|
87
|
|
|
*/ |
|
88
|
10 |
|
protected function beforeUpdate(/** @scrutinizer ignore-unused */ ARecord $record): bool |
|
89
|
|
|
{ |
|
90
|
10 |
|
return true; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Things to do after updating record |
|
95
|
|
|
* @param ARecord $record |
|
96
|
|
|
* @return bool |
|
97
|
|
|
*/ |
|
98
|
6 |
|
protected function afterUpdate(/** @scrutinizer ignore-unused */ ARecord $record): bool |
|
99
|
|
|
{ |
|
100
|
6 |
|
return true; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Things to do before inserting record |
|
105
|
|
|
* @param ARecord $record |
|
106
|
|
|
* @return bool |
|
107
|
|
|
*/ |
|
108
|
18 |
|
protected function beforeInsert(/** @scrutinizer ignore-unused */ ARecord $record): bool |
|
109
|
|
|
{ |
|
110
|
18 |
|
return true; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Things to do after inserting record |
|
115
|
|
|
* @param ARecord $record |
|
116
|
|
|
* @return bool |
|
117
|
|
|
*/ |
|
118
|
14 |
|
protected function afterInsert(/** @scrutinizer ignore-unused */ ARecord $record): bool |
|
119
|
|
|
{ |
|
120
|
14 |
|
return true; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Things to do before loading record |
|
125
|
|
|
* @param ARecord $record |
|
126
|
|
|
* @return bool |
|
127
|
|
|
*/ |
|
128
|
23 |
|
protected function beforeLoad(/** @scrutinizer ignore-unused */ ARecord $record): bool |
|
129
|
|
|
{ |
|
130
|
23 |
|
return true; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Things to do after loading record |
|
135
|
|
|
* @param ARecord $record |
|
136
|
|
|
* @return bool |
|
137
|
|
|
*/ |
|
138
|
14 |
|
protected function afterLoad(/** @scrutinizer ignore-unused */ ARecord $record): bool |
|
139
|
|
|
{ |
|
140
|
14 |
|
return true; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Insert data |
|
145
|
|
|
* @param ARecord $record |
|
146
|
|
|
* @throws MapperException |
|
147
|
|
|
* @return bool |
|
148
|
|
|
*/ |
|
149
|
19 |
|
public function insert(ARecord $record): bool |
|
150
|
|
|
{ |
|
151
|
19 |
|
if (!$this->beforeInsert($record)) { |
|
152
|
1 |
|
return false; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
18 |
|
if (!$this->insertRecord($record)) { |
|
156
|
2 |
|
return false; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
15 |
|
return $this->afterInsert($record); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Insert data - process |
|
164
|
|
|
* @param ARecord $record |
|
165
|
|
|
* @throws MapperException |
|
166
|
|
|
* @return bool |
|
167
|
|
|
*/ |
|
168
|
|
|
abstract protected function insertRecord(/** @scrutinizer ignore-unused */ ARecord $record): bool; |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Update data - by entries in record |
|
172
|
|
|
* @param ARecord $record |
|
173
|
|
|
* @throws MapperException |
|
174
|
|
|
* @return bool |
|
175
|
|
|
*/ |
|
176
|
11 |
|
public function update(ARecord $record): bool |
|
177
|
|
|
{ |
|
178
|
11 |
|
if (!$this->beforeUpdate($record)) { |
|
179
|
1 |
|
return false; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
10 |
|
if (!$this->updateRecord($record)) { |
|
183
|
2 |
|
return false; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
7 |
|
return $this->afterUpdate($record); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Update data - by entries in record - process |
|
191
|
|
|
* @param ARecord $record |
|
192
|
|
|
* @throws MapperException |
|
193
|
|
|
* @return bool |
|
194
|
|
|
*/ |
|
195
|
|
|
abstract protected function updateRecord(/** @scrutinizer ignore-unused */ ARecord $record): bool; |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Load record from storage |
|
199
|
|
|
* @param ARecord $record |
|
200
|
|
|
* @throws MapperException |
|
201
|
|
|
* @return boolean |
|
202
|
|
|
*/ |
|
203
|
25 |
|
public function load(ARecord $record): bool |
|
204
|
|
|
{ |
|
205
|
25 |
|
if (!$this->beforeLoad($record)) { |
|
206
|
2 |
|
return false; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
23 |
|
if (!$this->loadRecord($record)) { |
|
210
|
4 |
|
return false; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
16 |
|
return $this->afterLoad($record); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Count records with equal data as predefined one |
|
218
|
|
|
* @param ARecord $record |
|
219
|
|
|
* @throws MapperException |
|
220
|
|
|
* @return int |
|
221
|
|
|
*/ |
|
222
|
|
|
abstract public function countRecord(/** @scrutinizer ignore-unused */ ARecord $record): int; |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* Load record from storage |
|
226
|
|
|
* @param ARecord $record |
|
227
|
|
|
* @throws MapperException |
|
228
|
|
|
* @return ARecord[] |
|
229
|
|
|
*/ |
|
230
|
|
|
abstract public function loadMultiple(/** @scrutinizer ignore-unused */ ARecord $record): array; |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Load record from storage - process |
|
234
|
|
|
* @param ARecord $record |
|
235
|
|
|
* @throws MapperException |
|
236
|
|
|
* @return boolean |
|
237
|
|
|
*/ |
|
238
|
|
|
abstract protected function loadRecord(/** @scrutinizer ignore-unused */ ARecord $record): bool; |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* Save record object |
|
242
|
|
|
* @param ARecord $record |
|
243
|
|
|
* @param bool $forceInsert |
|
244
|
|
|
* @throws MapperException |
|
245
|
|
|
* @return bool |
|
246
|
|
|
*/ |
|
247
|
21 |
|
public function save(ARecord $record, bool $forceInsert = false): bool |
|
248
|
|
|
{ |
|
249
|
21 |
|
if (!$this->beforeSave($record)) { |
|
250
|
2 |
|
return false; |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
19 |
|
$hasPreset = 0; |
|
254
|
19 |
|
$hasNewOne = 0; |
|
255
|
19 |
|
foreach ($record as $key => $value) { |
|
256
|
19 |
|
$fromStorage = $record->getEntry($key)->isFromStorage(); |
|
257
|
19 |
|
$changed = $this->ifEntryChanged($record->getEntry($key)); |
|
258
|
19 |
|
$hasPreset += intval($fromStorage && $changed); |
|
259
|
19 |
|
$hasNewOne += intval(!$fromStorage && $changed); |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
19 |
|
$result = (boolval($hasPreset) && boolval($hasNewOne) && !$forceInsert) |
|
263
|
8 |
|
? $this->update($record) |
|
264
|
18 |
|
: $this->insert($record) |
|
265
|
19 |
|
; |
|
266
|
|
|
|
|
267
|
16 |
|
if (!$result) { |
|
268
|
3 |
|
return false; |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
14 |
|
return $this->afterSave($record); |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* Remove record from storage |
|
276
|
|
|
* @param ARecord $record |
|
277
|
|
|
* @throws MapperException |
|
278
|
|
|
* @return bool |
|
279
|
|
|
*/ |
|
280
|
14 |
|
public function delete(ARecord $record): bool |
|
281
|
|
|
{ |
|
282
|
14 |
|
if (!$this->beforeDelete($record)) { |
|
283
|
1 |
|
return false; |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
13 |
|
if (!$this->deleteRecord($record)) { |
|
287
|
4 |
|
return false; |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
10 |
|
return $this->afterDelete($record); |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
/** |
|
294
|
|
|
* Remove record from storage - process |
|
295
|
|
|
* @param ARecord $record |
|
296
|
|
|
* @throws MapperException |
|
297
|
|
|
* @return bool |
|
298
|
|
|
*/ |
|
299
|
|
|
abstract protected function deleteRecord(/** @scrutinizer ignore-unused */ ARecord $record): bool; |
|
300
|
|
|
} |
|
301
|
|
|
|