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
|
|
|
if ((isset($valueName) && !is_null($this->getValue($valueName))) || isset($this->data[$name])) { |
177
|
|
|
return true; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
// It's a relation |
181
|
|
|
$db = $this->table->getDatabase(); |
182
|
|
|
|
183
|
|
|
if (isset($db->$name)) { |
184
|
|
|
$table = $db->$name; |
185
|
|
|
|
186
|
|
|
// Has one |
187
|
|
|
if ($field = $this->table->getJoinField($table)) { |
188
|
|
|
return !is_null($this->getValue($field->getName())); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
// Has many or many-to-many always return true |
192
|
|
|
// because even if it's empty, it returns always a RowCollection |
193
|
|
|
if ($table->getJoinField($this->table) || $this->table->getJoinTable($table)) { |
194
|
|
|
return true; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
return false; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Removes the value of a field |
203
|
|
|
*/ |
204
|
|
|
public function __unset(string $name) |
205
|
|
|
{ |
206
|
|
|
unset($this->data[$name]); |
207
|
|
|
|
208
|
|
|
$this->__set($name, null); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Reload the data from the database |
213
|
|
|
*/ |
214
|
|
|
public function reload(bool $keepChanges = false): self |
215
|
|
|
{ |
216
|
|
|
$select = $this->table->select()->where('id = ', $this->id); |
|
|
|
|
217
|
|
|
$values = $select()->fetch(\PDO::FETCH_ASSOC); |
218
|
|
|
$this->values = $this->table->format($values); |
219
|
|
|
|
220
|
|
|
if (!$keepChanges) { |
221
|
|
|
$this->changes = []; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
return $this; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Returns an array with all fields of the row |
229
|
|
|
*/ |
230
|
|
|
public function toArray(): array |
231
|
|
|
{ |
232
|
|
|
return $this->changes + $this->values; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Edit the values using an array |
237
|
|
|
*/ |
238
|
|
|
public function edit(array $values): self |
239
|
|
|
{ |
240
|
|
|
foreach ($values as $name => $value) { |
241
|
|
|
$this->__set($name, $value); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
return $this; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Returns the id of the row. |
249
|
|
|
* If it does not exist (because it is not saved in the database yet), |
250
|
|
|
* execute a save() first |
251
|
|
|
*/ |
252
|
|
|
public function id(): int |
253
|
|
|
{ |
254
|
|
|
if (empty($this->id)) { |
255
|
|
|
$this->save(); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
return $this->id; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Insert/update the row in the database |
263
|
|
|
*/ |
264
|
|
|
public function save(): self |
265
|
|
|
{ |
266
|
|
|
if (!empty($this->changes)) { |
267
|
|
|
$eventDispatcher = $this->table->getEventDispatcher(); |
268
|
|
|
|
269
|
|
|
if ($eventDispatcher) { |
270
|
|
|
$eventDispatcher->dispatch(new BeforeSaveRow($this)); |
|
|
|
|
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
if (empty($this->id)) { |
274
|
|
|
$this->id = $this->table->insert($this->toArray())->run(); |
275
|
|
|
} elseif (empty($this->changes)) { |
276
|
|
|
return $this; |
277
|
|
|
} else { |
278
|
|
|
$this->table->update($this->changes) |
|
|
|
|
279
|
|
|
->where('id = ', $this->id) |
280
|
|
|
->run(); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
$this->values = $this->toArray(); |
284
|
|
|
$this->changes = []; |
285
|
|
|
$this->table->cache($this); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
return $this; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Delete the row in the database |
293
|
|
|
*/ |
294
|
|
|
public function delete(): self |
295
|
|
|
{ |
296
|
|
|
$id = $this->id; |
297
|
|
|
|
298
|
|
|
if (!empty($id)) { |
299
|
|
|
$this->table->delete() |
|
|
|
|
300
|
|
|
->where('id = ', $id) |
301
|
|
|
->run(); |
302
|
|
|
|
303
|
|
|
$this->values['id'] = null; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
return $this; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Relate this row with other rows |
311
|
|
|
*/ |
312
|
|
|
public function relate(Row ...$rows): self |
313
|
|
|
{ |
314
|
|
|
$table1 = $this->table; |
315
|
|
|
|
316
|
|
|
foreach ($rows as $row) { |
317
|
|
|
$table2 = $row->getTable(); |
318
|
|
|
|
319
|
|
|
//Has one |
320
|
|
|
if ($field = $table1->getJoinField($table2)) { |
321
|
|
|
$this->{$field->getName()} = $row->id(); |
322
|
|
|
continue; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
//Has many |
326
|
|
|
if ($field = $table2->getJoinField($table1)) { |
327
|
|
|
$row->{$field->getName()} = $this->id(); |
328
|
|
|
$row->save(); |
329
|
|
|
continue; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
//Has many to many |
333
|
|
|
if ($joinTable = $table1->getJoinTable($table2)) { |
334
|
|
|
$joinTable->insert([ |
335
|
|
|
$joinTable->getJoinField($table1)->getName() => $this->id(), |
336
|
|
|
$joinTable->getJoinField($table2)->getName() => $row->id(), |
337
|
|
|
]) |
338
|
|
|
->orIgnore() |
339
|
|
|
->run(); |
340
|
|
|
|
341
|
|
|
continue; |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
throw new RuntimeException( |
345
|
|
|
sprintf('The tables %s and %s are not related', $table1, $table2) |
346
|
|
|
); |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
return $this->save(); |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* Unrelate this row with other rows |
354
|
|
|
*/ |
355
|
|
|
public function unrelate(Row ...$rows): self |
356
|
|
|
{ |
357
|
|
|
$table1 = $this->table; |
358
|
|
|
|
359
|
|
|
foreach ($rows as $row) { |
360
|
|
|
$table2 = $row->getTable(); |
361
|
|
|
|
362
|
|
|
//Has one |
363
|
|
|
if ($field = $table1->getJoinField($table2)) { |
364
|
|
|
$this->{$field->getName()} = null; |
365
|
|
|
continue; |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
//Has many |
369
|
|
|
if ($field = $table2->getJoinField($table1)) { |
370
|
|
|
$row->{$field->getName()} = null; |
371
|
|
|
$row->save(); |
372
|
|
|
continue; |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
//Has many to many |
376
|
|
|
if ($joinTable = $table1->getJoinTable($table2)) { |
377
|
|
|
$joinTable->delete() |
378
|
|
|
->where("{$joinTable->getJoinField($table1)} = ", $this->id) |
379
|
|
|
->where("{$joinTable->getJoinField($table2)} = ", $row->id) |
380
|
|
|
->run(); |
381
|
|
|
|
382
|
|
|
continue; |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
throw new RuntimeException( |
386
|
|
|
sprintf('The tables %s and %s are not related', $table1, $table2) |
387
|
|
|
); |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
return $this->save(); |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
/** |
394
|
|
|
* Unrelate this row with all rows of other tables |
395
|
|
|
*/ |
396
|
|
|
public function unrelateAll(Table ...$tables): self |
397
|
|
|
{ |
398
|
|
|
$table1 = $this->table; |
399
|
|
|
|
400
|
|
|
foreach ($tables as $table2) { |
401
|
|
|
//Has one |
402
|
|
|
if ($field = $table1->getJoinField($table2)) { |
403
|
|
|
$this->{$field->getName()} = null; |
404
|
|
|
continue; |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
//Has many |
408
|
|
|
if ($field = $table2->getJoinField($table1)) { |
409
|
|
|
$table2->update([ |
410
|
|
|
$field->getName() => null, |
411
|
|
|
]) |
412
|
|
|
->relatedWith($table1) |
413
|
|
|
->run(); |
414
|
|
|
continue; |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
//Has many to many |
418
|
|
|
if ($joinTable = $table1->getJoinTable($table2)) { |
419
|
|
|
$joinTable->delete() |
420
|
|
|
->where("{$joinTable->getJoinField($table1)} = ", $this->id) |
421
|
|
|
->where("{$joinTable->getJoinField($table2)} IS NOT NULL") |
422
|
|
|
->run(); |
423
|
|
|
|
424
|
|
|
continue; |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
throw new RuntimeException( |
428
|
|
|
sprintf('The tables %s and %s are not related', $table1, $table2) |
429
|
|
|
); |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
return $this->save(); |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
/** |
436
|
|
|
* Creates a select query of a table related with this row |
437
|
|
|
*/ |
438
|
|
|
public function select(Table $table): Select |
439
|
|
|
{ |
440
|
|
|
//Has one |
441
|
|
|
if ($this->table->getJoinField($table)) { |
442
|
|
|
return $table->select()->one()->relatedWith($this); |
443
|
|
|
} |
444
|
|
|
|
445
|
|
|
return $table->select()->relatedWith($this); |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
/** |
449
|
|
|
* Return the real field name |
450
|
|
|
*/ |
451
|
|
|
private function getValueName(string $name): ?string |
452
|
|
|
{ |
453
|
|
|
if (array_key_exists($name, $this->values)) { |
454
|
|
|
return $name; |
455
|
|
|
} |
456
|
|
|
|
457
|
|
|
//It's a localizable field |
458
|
|
|
$language = $this->table->getDatabase()->getConfig(Database::CONFIG_LOCALE); |
459
|
|
|
|
460
|
|
|
if (!is_null($language)) { |
461
|
|
|
$name .= "_{$language}"; |
462
|
|
|
|
463
|
|
|
if (array_key_exists($name, $this->values)) { |
464
|
|
|
return $name; |
465
|
|
|
} |
466
|
|
|
} |
467
|
|
|
|
468
|
|
|
return null; |
469
|
|
|
} |
470
|
|
|
|
471
|
|
|
private function getValue(string $name) |
472
|
|
|
{ |
473
|
|
|
return array_key_exists($name, $this->changes) ? $this->changes[$name] : $this->values[$name]; |
474
|
|
|
} |
475
|
|
|
} |
476
|
|
|
|
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: