1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace SimpleCrud; |
5
|
|
|
|
6
|
|
|
use BadMethodCallException; |
7
|
|
|
use JsonSerializable; |
8
|
|
|
use RuntimeException; |
9
|
|
|
use SimpleCrud\Events\BeforeSaveRow; |
10
|
|
|
use SimpleCrud\Queries\Select; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Stores the data of an table row. |
14
|
|
|
*/ |
15
|
|
|
class Row implements JsonSerializable |
16
|
|
|
{ |
17
|
|
|
private $table; |
18
|
|
|
private $values = []; |
19
|
|
|
private $changes = []; |
20
|
|
|
private $data = []; |
21
|
|
|
|
22
|
|
|
public function __construct(Table $table, array $values) |
23
|
|
|
{ |
24
|
|
|
$this->table = $table; |
25
|
|
|
|
26
|
|
|
if (empty($values['id'])) { |
27
|
|
|
$this->values = $table->getDefaults(); |
28
|
|
|
$this->changes = $table->getDefaults($values); |
29
|
|
|
unset($this->changes['id']); |
30
|
|
|
} else { |
31
|
|
|
$this->values = $table->getDefaults($values); |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function __debugInfo(): array |
36
|
|
|
{ |
37
|
|
|
return [ |
38
|
|
|
'table' => (string) $this->table, |
39
|
|
|
'values' => $this->values, |
40
|
|
|
'changes' => $this->changes, |
41
|
|
|
'data' => $this->data, |
42
|
|
|
]; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function __call(string $name, array $arguments): Select |
46
|
|
|
{ |
47
|
|
|
$db = $this->table->getDatabase(); |
48
|
|
|
|
49
|
|
|
//Relations |
50
|
|
|
if (isset($db->$name)) { |
51
|
|
|
return $this->select($db->$name); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
throw new BadMethodCallException( |
55
|
|
|
sprintf('Invalid method call %s', $name) |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function setData(array $data): self |
60
|
|
|
{ |
61
|
|
|
$this->data = $data + $this->data; |
62
|
|
|
|
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param Row|RowCollection|null $row |
68
|
|
|
*/ |
69
|
|
|
public function link(Table $table, $row = null): self |
70
|
|
|
{ |
71
|
|
|
return $this->setData([$table->getName() => $row]); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @see JsonSerializable |
76
|
|
|
*/ |
77
|
|
|
public function jsonSerialize() |
78
|
|
|
{ |
79
|
|
|
return $this->toArray(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Magic method to stringify the values. |
84
|
|
|
*/ |
85
|
|
|
public function __toString() |
86
|
|
|
{ |
87
|
|
|
return json_encode($this, JSON_NUMERIC_CHECK); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Returns the table associated with this row |
92
|
|
|
*/ |
93
|
|
|
public function getTable(): Table |
94
|
|
|
{ |
95
|
|
|
return $this->table; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Returns the value of: |
100
|
|
|
* - a value field |
101
|
|
|
* - a related table |
102
|
|
|
*/ |
103
|
|
|
public function &__get(string $name) |
104
|
|
|
{ |
105
|
|
|
if ($name === 'id') { |
106
|
|
|
return $this->values['id']; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
//It's a value |
110
|
|
|
if ($valueName = $this->getValueName($name)) { |
111
|
|
|
$value = $this->getValue($valueName); |
112
|
|
|
return $value; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
//It's custom data |
116
|
|
|
if (array_key_exists($name, $this->data)) { |
117
|
|
|
return $this->data[$name]; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$db = $this->table->getDatabase(); |
121
|
|
|
|
122
|
|
|
if (isset($db->$name)) { |
123
|
|
|
$this->setData([ |
124
|
|
|
$name => $this->select($db->$name)->run(), |
125
|
|
|
]); |
126
|
|
|
|
127
|
|
|
return $this->data[$name]; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
throw new RuntimeException( |
131
|
|
|
sprintf('Undefined property "%s" in the table %s', $name, $this->table) |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Change the value of |
137
|
|
|
* - a field |
138
|
|
|
* - a localized field |
139
|
|
|
* @param mixed $value |
140
|
|
|
*/ |
141
|
|
|
public function __set(string $name, $value) |
142
|
|
|
{ |
143
|
|
|
if ($name === 'id') { |
144
|
|
|
if (!is_null($this->values['id']) && !is_null($value)) { |
145
|
|
|
throw new RuntimeException('The field "id" cannot be overrided'); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$this->values['id'] = $value; |
149
|
|
|
|
150
|
|
|
return $value; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
//It's a value |
154
|
|
|
if ($valueName = $this->getValueName($name)) { |
155
|
|
|
if ($this->values[$valueName] === $value) { |
156
|
|
|
unset($this->changes[$valueName]); |
157
|
|
|
} else { |
158
|
|
|
$this->changes[$valueName] = $value; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $value; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
throw new RuntimeException( |
165
|
|
|
sprintf('The field %s does not exists', $name) |
166
|
|
|
); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Check whether a value is set or not |
171
|
|
|
*/ |
172
|
|
|
public function __isset(string $name): bool |
173
|
|
|
{ |
174
|
|
|
$valueName = $this->getValueName($name); |
175
|
|
|
|
176
|
|
|
return (isset($valueName) && !is_null($this->getValue($valueName))) || isset($this->data[$name]); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Removes the value of a field |
181
|
|
|
*/ |
182
|
|
|
public function __unset(string $name) |
183
|
|
|
{ |
184
|
|
|
unset($this->data[$name]); |
185
|
|
|
|
186
|
|
|
$this->__set($name, null); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Reload the data from the database |
191
|
|
|
*/ |
192
|
|
|
public function reload(bool $keepChanges = false): self |
193
|
|
|
{ |
194
|
|
|
$select = $this->table->select()->where('id = ', $this->id); |
|
|
|
|
195
|
|
|
$values = $select()->fetch(\PDO::FETCH_ASSOC); |
196
|
|
|
$this->values = $this->table->format($values); |
197
|
|
|
|
198
|
|
|
if (!$keepChanges) { |
199
|
|
|
$this->changes = []; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
return $this; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Returns an array with all fields of the row |
207
|
|
|
*/ |
208
|
|
|
public function toArray(): array |
209
|
|
|
{ |
210
|
|
|
return $this->changes + $this->values; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Edit the values using an array |
215
|
|
|
*/ |
216
|
|
|
public function edit(array $values): self |
217
|
|
|
{ |
218
|
|
|
foreach ($values as $name => $value) { |
219
|
|
|
$this->__set($name, $value); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
return $this; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Returns the id of the row. |
227
|
|
|
* If it does not exist (because it is not saved in the database yet), |
228
|
|
|
* execute a save() first |
229
|
|
|
*/ |
230
|
|
|
public function id(): int |
231
|
|
|
{ |
232
|
|
|
if (empty($this->id)) { |
233
|
|
|
$this->save(); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
return $this->id; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Insert/update the row in the database |
241
|
|
|
*/ |
242
|
|
|
public function save(): self |
243
|
|
|
{ |
244
|
|
|
if (!empty($this->changes)) { |
245
|
|
|
$eventDispatcher = $this->table->getEventDispatcher(); |
246
|
|
|
|
247
|
|
|
if ($eventDispatcher) { |
248
|
|
|
$eventDispatcher->dispatch(new BeforeSaveRow($this)); |
|
|
|
|
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
if (empty($this->id)) { |
252
|
|
|
$values = array_filter( |
253
|
|
|
$this->toArray(), |
254
|
|
|
function ($value) { |
255
|
|
|
return $value !== null; |
256
|
|
|
} |
257
|
|
|
); |
258
|
|
|
|
259
|
|
|
$this->id = $this->table->insert($values)->run(); |
260
|
|
|
} elseif (empty($this->changes)) { |
261
|
|
|
return $this; |
262
|
|
|
} else { |
263
|
|
|
$this->table->update($this->changes) |
|
|
|
|
264
|
|
|
->where('id = ', $this->id) |
265
|
|
|
->run(); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
$this->values = $this->toArray(); |
269
|
|
|
$this->changes = []; |
270
|
|
|
$this->table->cache($this); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
return $this; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Delete the row in the database |
278
|
|
|
*/ |
279
|
|
|
public function delete(): self |
280
|
|
|
{ |
281
|
|
|
$id = $this->id; |
282
|
|
|
|
283
|
|
|
if (!empty($id)) { |
284
|
|
|
$this->table->delete() |
|
|
|
|
285
|
|
|
->where('id = ', $id) |
286
|
|
|
->run(); |
287
|
|
|
|
288
|
|
|
$this->values['id'] = null; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
return $this; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* Relate this row with other rows |
296
|
|
|
*/ |
297
|
|
|
public function relate(Row ...$rows): self |
298
|
|
|
{ |
299
|
|
|
$table1 = $this->table; |
300
|
|
|
|
301
|
|
|
foreach ($rows as $row) { |
302
|
|
|
$table2 = $row->getTable(); |
303
|
|
|
|
304
|
|
|
//Has one |
305
|
|
|
if ($field = $table1->getJoinField($table2)) { |
306
|
|
|
$this->{$field->getName()} = $row->id(); |
307
|
|
|
continue; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
//Has many |
311
|
|
|
if ($field = $table2->getJoinField($table1)) { |
312
|
|
|
$row->{$field->getName()} = $this->id(); |
313
|
|
|
$row->save(); |
314
|
|
|
continue; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
//Has many to many |
318
|
|
|
if ($joinTable = $table1->getJoinTable($table2)) { |
319
|
|
|
$joinTable->insert([ |
320
|
|
|
$joinTable->getJoinField($table1)->getName() => $this->id(), |
321
|
|
|
$joinTable->getJoinField($table2)->getName() => $row->id(), |
322
|
|
|
]) |
323
|
|
|
->orIgnore() |
324
|
|
|
->run(); |
325
|
|
|
|
326
|
|
|
continue; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
throw new RuntimeException( |
330
|
|
|
sprintf('The tables %s and %s are not related', $table1, $table2) |
331
|
|
|
); |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
return $this->save(); |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* Unrelate this row with other rows |
339
|
|
|
*/ |
340
|
|
|
public function unrelate(Row ...$rows): self |
341
|
|
|
{ |
342
|
|
|
$table1 = $this->table; |
343
|
|
|
|
344
|
|
|
foreach ($rows as $row) { |
345
|
|
|
$table2 = $row->getTable(); |
346
|
|
|
|
347
|
|
|
//Has one |
348
|
|
|
if ($field = $table1->getJoinField($table2)) { |
349
|
|
|
$this->{$field->getName()} = null; |
350
|
|
|
continue; |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
//Has many |
354
|
|
|
if ($field = $table2->getJoinField($table1)) { |
355
|
|
|
$row->{$field->getName()} = null; |
356
|
|
|
$row->save(); |
357
|
|
|
continue; |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
//Has many to many |
361
|
|
|
if ($joinTable = $table1->getJoinTable($table2)) { |
362
|
|
|
$joinTable->delete() |
363
|
|
|
->where("{$joinTable->getJoinField($table1)} = ", $this->id) |
364
|
|
|
->where("{$joinTable->getJoinField($table2)} = ", $row->id) |
365
|
|
|
->run(); |
366
|
|
|
|
367
|
|
|
continue; |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
throw new RuntimeException( |
371
|
|
|
sprintf('The tables %s and %s are not related', $table1, $table2) |
372
|
|
|
); |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
return $this->save(); |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
/** |
379
|
|
|
* Unrelate this row with all rows of other tables |
380
|
|
|
*/ |
381
|
|
|
public function unrelateAll(Table ...$tables): self |
382
|
|
|
{ |
383
|
|
|
$table1 = $this->table; |
384
|
|
|
|
385
|
|
|
foreach ($tables as $table2) { |
386
|
|
|
//Has one |
387
|
|
|
if ($field = $table1->getJoinField($table2)) { |
388
|
|
|
$this->{$field->getName()} = null; |
389
|
|
|
continue; |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
//Has many |
393
|
|
|
if ($field = $table2->getJoinField($table1)) { |
394
|
|
|
$table2->update([ |
395
|
|
|
$field->getName() => null, |
396
|
|
|
]) |
397
|
|
|
->relatedWith($table1) |
398
|
|
|
->run(); |
399
|
|
|
continue; |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
//Has many to many |
403
|
|
|
if ($joinTable = $table1->getJoinTable($table2)) { |
404
|
|
|
$joinTable->delete() |
405
|
|
|
->where("{$joinTable->getJoinField($table1)} = ", $this->id) |
406
|
|
|
->where("{$joinTable->getJoinField($table2)} IS NOT NULL") |
407
|
|
|
->run(); |
408
|
|
|
|
409
|
|
|
continue; |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
throw new RuntimeException( |
413
|
|
|
sprintf('The tables %s and %s are not related', $table1, $table2) |
414
|
|
|
); |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
return $this->save(); |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
/** |
421
|
|
|
* Creates a select query of a table related with this row |
422
|
|
|
*/ |
423
|
|
|
public function select(Table $table): Select |
424
|
|
|
{ |
425
|
|
|
//Has one |
426
|
|
|
if ($this->table->getJoinField($table)) { |
427
|
|
|
return $table->select()->one()->relatedWith($this); |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
return $table->select()->relatedWith($this); |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
/** |
434
|
|
|
* Return the real field name |
435
|
|
|
*/ |
436
|
|
|
private function getValueName(string $name): ?string |
437
|
|
|
{ |
438
|
|
|
if (array_key_exists($name, $this->values)) { |
439
|
|
|
return $name; |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
//It's a localizable field |
443
|
|
|
$language = $this->table->getDatabase()->getConfig(Database::CONFIG_LOCALE); |
444
|
|
|
|
445
|
|
|
if (!is_null($language)) { |
446
|
|
|
$name .= "_{$language}"; |
447
|
|
|
|
448
|
|
|
if (array_key_exists($name, $this->values)) { |
449
|
|
|
return $name; |
450
|
|
|
} |
451
|
|
|
} |
452
|
|
|
|
453
|
|
|
return null; |
454
|
|
|
} |
455
|
|
|
|
456
|
|
|
private function getValue(string $name) |
457
|
|
|
{ |
458
|
|
|
return array_key_exists($name, $this->changes) ? $this->changes[$name] : $this->values[$name]; |
459
|
|
|
} |
460
|
|
|
} |
461
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: