|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Base daft objects. |
|
4
|
|
|
* |
|
5
|
|
|
* @author SignpostMarv |
|
6
|
|
|
*/ |
|
7
|
|
|
declare(strict_types=1); |
|
8
|
|
|
|
|
9
|
|
|
namespace SignpostMarv\DaftObject; |
|
10
|
|
|
|
|
11
|
|
|
use ParagonIE\EasyDB\EasyDB; |
|
12
|
|
|
use Throwable; |
|
13
|
|
|
|
|
14
|
|
|
abstract class AbstractDaftObjectEasyDBRepository extends DaftObjectMemoryRepository |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var EasyDB |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $db; |
|
20
|
|
|
|
|
21
|
5 |
|
protected function __construct(string $type, EasyDB $db) |
|
22
|
|
|
{ |
|
23
|
5 |
|
parent::__construct($type); |
|
24
|
5 |
|
$this->db = $db; |
|
25
|
5 |
|
} |
|
26
|
|
|
|
|
27
|
9 |
|
public static function DaftObjectRepositoryByType( |
|
28
|
|
|
string $type, |
|
29
|
|
|
? EasyDB $db = null |
|
30
|
|
|
) : DaftObjectRepository { |
|
31
|
|
|
if ( |
|
32
|
9 |
|
false === is_a( |
|
33
|
9 |
|
$type, |
|
34
|
9 |
|
DaftObjectCreatedByArray::class, |
|
35
|
9 |
|
true |
|
36
|
|
|
) |
|
37
|
|
|
) { |
|
38
|
2 |
|
throw new DaftObjectRepositoryTypeByClassMethodAndTypeException( |
|
39
|
2 |
|
1, |
|
40
|
2 |
|
static::class, |
|
41
|
2 |
|
__FUNCTION__, |
|
42
|
2 |
|
DaftObjectCreatedByArray::class, |
|
43
|
2 |
|
$type |
|
44
|
|
|
); |
|
45
|
|
|
} elseif ( |
|
46
|
7 |
|
false === is_a( |
|
47
|
7 |
|
$type, |
|
48
|
7 |
|
DefinesOwnIdPropertiesInterface::class, |
|
49
|
7 |
|
true |
|
50
|
|
|
) |
|
51
|
|
|
) { |
|
52
|
1 |
|
throw new DaftObjectRepositoryTypeByClassMethodAndTypeException( |
|
53
|
1 |
|
1, |
|
54
|
1 |
|
static::class, |
|
55
|
1 |
|
__FUNCTION__, |
|
56
|
1 |
|
DefinesOwnIdPropertiesInterface::class, |
|
57
|
1 |
|
$type |
|
58
|
|
|
); |
|
59
|
6 |
|
} elseif (false === ($db instanceof EasyDB)) { |
|
60
|
1 |
|
throw new DatabaseConnectionNotSpecifiedException( |
|
61
|
1 |
|
2, |
|
62
|
1 |
|
static::class, |
|
63
|
1 |
|
__FUNCTION__, |
|
64
|
1 |
|
EasyDB::class, |
|
65
|
1 |
|
'null' |
|
66
|
|
|
); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @var EasyDB $db |
|
71
|
|
|
*/ |
|
72
|
5 |
|
$db = $db; |
|
73
|
|
|
|
|
74
|
5 |
|
return new static($type, $db); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
5 |
|
public static function DaftObjectRepositoryByDaftObject( |
|
78
|
|
|
DefinesOwnIdPropertiesInterface $object, |
|
79
|
|
|
? EasyDB $db = null |
|
80
|
|
|
) : DaftObjectRepository { |
|
81
|
5 |
|
return static::DaftObjectRepositoryByType(get_class($object), $db); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param mixed $id |
|
86
|
|
|
*/ |
|
87
|
2 |
|
public function RemoveDaftObjectById($id) : void |
|
88
|
|
|
{ |
|
89
|
2 |
|
$id = array_values(is_array($id) ? $id : [$id]); |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @var DefinesOwnIdPropertiesInterface $type |
|
93
|
|
|
*/ |
|
94
|
2 |
|
$type = $this->type; |
|
95
|
2 |
|
$idkv = []; |
|
96
|
|
|
|
|
97
|
|
|
foreach ( |
|
98
|
2 |
|
array_values($type::DaftObjectIdProperties()) as $i => $prop |
|
99
|
|
|
) { |
|
100
|
2 |
|
$idkv[$prop] = $id[$i]; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
2 |
|
$where = []; |
|
104
|
2 |
|
foreach (array_keys($idkv) as $col) { |
|
105
|
2 |
|
$where[] = $this->db->escapeIdentifier($col) . ' = ?'; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$query = ( |
|
109
|
|
|
'DELETE FROM ' . |
|
110
|
2 |
|
$this->db->escapeIdentifier( |
|
111
|
2 |
|
$this->DaftObjectDatabaseTable() |
|
112
|
|
|
) . |
|
113
|
2 |
|
' WHERE ' . |
|
114
|
2 |
|
implode(' AND ', $where) |
|
115
|
|
|
); |
|
116
|
|
|
|
|
117
|
2 |
|
$this->db->safeQuery($query, array_values($idkv)); |
|
118
|
|
|
|
|
119
|
2 |
|
$this->ForgetDaftObjectById($id); |
|
120
|
2 |
|
} |
|
121
|
|
|
|
|
122
|
|
|
abstract protected function DaftObjectDatabaseTable() : string; |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @return string[] |
|
126
|
|
|
*/ |
|
127
|
2 |
|
protected function RememberDaftObjectDataCols( |
|
128
|
|
|
DaftObject $object, |
|
129
|
|
|
bool $exists |
|
130
|
|
|
) : array { |
|
131
|
2 |
|
$cols = $object::DaftObjectExportableProperties(); |
|
132
|
2 |
|
if ($exists) { |
|
133
|
2 |
|
$changed = $object->ChangedProperties(); |
|
134
|
2 |
|
$cols = array_filter( |
|
135
|
2 |
|
$cols, |
|
136
|
2 |
|
function (string $prop) use ($changed) : bool { |
|
137
|
2 |
|
return in_array($prop, $changed, true); |
|
138
|
2 |
|
} |
|
139
|
|
|
); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
2 |
|
return $cols; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @return mixed[] |
|
147
|
|
|
*/ |
|
148
|
2 |
|
protected function RememberDaftObjectDataValues( |
|
149
|
|
|
DaftObject $object, |
|
150
|
|
|
bool $exists |
|
151
|
|
|
) : array { |
|
152
|
2 |
|
$values = []; |
|
153
|
2 |
|
$cols = $this->RememberDaftObjectDataCols($object, $exists); |
|
154
|
2 |
|
foreach ($cols as $col) { |
|
155
|
2 |
|
$values[$col] = $object->$col; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
2 |
|
return $values; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
2 |
|
protected function RememberDaftObjectDataUpdate( |
|
162
|
|
|
bool $exists, |
|
163
|
|
|
array $id, |
|
164
|
|
|
array $values |
|
165
|
|
|
) : void { |
|
166
|
2 |
|
if (count($values) > 0) { |
|
167
|
2 |
|
if (false === $exists) { |
|
168
|
2 |
|
$this->db->insert( |
|
169
|
2 |
|
$this->DaftObjectDatabaseTable(), |
|
170
|
2 |
|
$values |
|
171
|
|
|
); |
|
172
|
|
|
} else { |
|
173
|
2 |
|
$this->db->update( |
|
174
|
2 |
|
$this->DaftObjectDatabaseTable(), |
|
175
|
2 |
|
$values, |
|
176
|
2 |
|
$id |
|
177
|
|
|
); |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
2 |
|
} |
|
181
|
|
|
|
|
182
|
2 |
|
protected function RememberDaftObjectData( |
|
183
|
|
|
DefinesOwnIdPropertiesInterface $object |
|
184
|
|
|
) : void { |
|
185
|
2 |
|
$id = []; |
|
186
|
|
|
|
|
187
|
2 |
|
foreach ($object::DaftObjectIdProperties() as $prop) { |
|
188
|
2 |
|
$id[$prop] = $object->$prop; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
2 |
|
$autoStartTransaction = (false === $this->db->inTransaction()); |
|
192
|
|
|
|
|
193
|
2 |
|
if (true === $autoStartTransaction) { |
|
194
|
2 |
|
$this->db->beginTransaction(); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
try { |
|
198
|
2 |
|
$exists = $this->DaftObjectExistsInDatabase($id); |
|
199
|
2 |
|
$values = $this->RememberDaftObjectDataValues($object, $exists); |
|
200
|
2 |
|
$this->RememberDaftObjectDataUpdate($exists, $id, $values); |
|
201
|
|
|
|
|
202
|
2 |
|
if (true === $autoStartTransaction) { |
|
203
|
2 |
|
$this->db->commit(); |
|
204
|
|
|
} |
|
205
|
|
|
} catch (Throwable $e) { |
|
206
|
|
|
if (true === $autoStartTransaction) { |
|
207
|
|
|
$this->db->rollBack(); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
throw $e; |
|
211
|
|
|
} |
|
212
|
2 |
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* @param mixed $id |
|
216
|
|
|
*/ |
|
217
|
2 |
|
protected function RecallDaftObjectFromData($id) : ? DaftObject |
|
218
|
|
|
{ |
|
219
|
|
|
/** |
|
220
|
|
|
* @var DefinesOwnIdPropertiesInterface $type |
|
221
|
|
|
*/ |
|
222
|
2 |
|
$type = $this->type; |
|
223
|
2 |
|
$idkv = []; |
|
224
|
|
|
|
|
225
|
|
|
foreach ( |
|
226
|
2 |
|
array_values($type::DaftObjectIdProperties()) as $i => $prop |
|
227
|
|
|
) { |
|
228
|
2 |
|
$idkv[$prop] = $id[$i]; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
2 |
|
if (true === $this->DaftObjectExistsInDatabase($idkv)) { |
|
232
|
2 |
|
$where = []; |
|
233
|
2 |
|
foreach (array_keys($idkv) as $col) { |
|
234
|
2 |
|
$where[] = $this->db->escapeIdentifier($col) . ' = ?'; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
2 |
|
$data = $this->db->safeQuery( |
|
238
|
|
|
( |
|
239
|
|
|
'SELECT * FROM ' . |
|
240
|
2 |
|
$this->db->escapeIdentifier( |
|
241
|
2 |
|
$this->DaftObjectDatabaseTable() |
|
242
|
|
|
) . |
|
243
|
2 |
|
' WHERE ' . |
|
244
|
2 |
|
implode(' AND ', $where) . |
|
245
|
2 |
|
' LIMIT 1' |
|
246
|
|
|
), |
|
247
|
2 |
|
array_values($idkv) |
|
248
|
|
|
); |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* @var \SignpostMarv\DaftObject\DaftObjectCreatedByArray $type |
|
252
|
|
|
*/ |
|
253
|
2 |
|
$type = $type; |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* @var DefinesOwnIdPropertiesInterface $out |
|
257
|
|
|
*/ |
|
258
|
2 |
|
$out = new $type($data[0]); |
|
259
|
|
|
|
|
260
|
2 |
|
return $out; |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
2 |
|
return null; |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
2 |
|
private function DaftObjectExistsInDatabase(array $id) : bool |
|
267
|
|
|
{ |
|
268
|
2 |
|
$where = []; |
|
269
|
2 |
|
foreach (array_keys($id) as $col) { |
|
270
|
2 |
|
$where[] = $this->db->escapeIdentifier($col) . ' = ?'; |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
return |
|
274
|
2 |
|
(int) $this->db->single( |
|
275
|
|
|
( |
|
276
|
|
|
'SELECT COUNT(*) FROM ' . |
|
277
|
2 |
|
$this->db->escapeIdentifier( |
|
278
|
2 |
|
$this->DaftObjectDatabaseTable() |
|
279
|
|
|
) . |
|
280
|
2 |
|
' WHERE ' . |
|
281
|
2 |
|
implode(' AND ', $where) |
|
282
|
|
|
), |
|
283
|
2 |
|
array_values($id) |
|
284
|
2 |
|
) >= 1; |
|
285
|
|
|
} |
|
286
|
|
|
} |
|
287
|
|
|
|