-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathModel.php
More file actions
617 lines (507 loc) · 14.3 KB
/
Copy pathModel.php
File metadata and controls
617 lines (507 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
<?php
declare(strict_types=1);
namespace Zero\Lib;
use Closure;
use InvalidArgumentException;
use JsonSerializable;
use ReflectionMethod;
use RuntimeException;
use Zero\Lib\DB\DBML;
use Zero\Lib\DB\DBMLExpression;
use Zero\Lib\Model\Concerns\InteractsWithRelations;
use Zero\Lib\Model\ModelQuery;
use Zero\Lib\Support\Paginator;
use Zero\Lib\Support\Str;
/**
* Minimalist active-record style model built on top of the DBML query builder.
*
* The base model provides conveniences inspired by Laravel's Eloquent such as
* mass-assignment, timestamp management, soft-delete opt-in, and fluent query
* access, while remaining dependency-free. Extend this class within
* `App\Models` to create strongly-typed representations of database tables.
* Enable soft deletes by setting `protected bool $softDeletes = true;` on the
* child model and ensuring the table includes a nullable `deleted_at` column.
*/
class Model implements JsonSerializable
{
use InteractsWithRelations;
/**
* Explicit table name. When null we derive the table from the class name.
*/
protected ?string $table = null;
/**
* Database connection name (key under `config/database.php`). Null = default.
*/
protected ?string $connection = null;
/**
* Name of the primary key column.
*/
protected string $primaryKey = 'id';
/**
* Indicates whether the primary key is auto-incrementing.
*/
protected bool $incrementing = true;
/**
* Automatically assign a UUID when inserting.
*/
protected bool $usesUuid = false;
/**
* Column that should receive the generated UUID (defaults to the primary key).
*/
protected ?string $uuidColumn = null;
/**
* List of attributes that are mass assignable. Empty array permits all.
*
* @var string[]
*/
protected array $fillable = [];
/**
* Whether created_at/updated_at columns should be maintained automatically.
*/
protected bool $timestamps = true;
/**
* Toggle soft delete behaviour; set to true on child models to opt-in.
*/
protected bool $softDeletes = false;
/**
* Column name that stores the soft delete timestamp.
*/
protected string $deletedAtColumn = 'deleted_at';
/**
* Column names used for timestamps when enabled.
*/
protected string $createdAtColumn = 'created_at';
protected string $updatedAtColumn = 'updated_at';
/**
* Internal attribute bag representing the current model state.
*
* @var array<string, mixed>
*/
protected array $attributes = [];
/**
* Snapshot of attributes retrieved from the database for dirty tracking.
*
* @var array<string, mixed>
*/
protected array $original = [];
/**
* Eager-loaded relationship data keyed by relation name.
*
* @var array<string, mixed>
*/
protected array $relations = [];
/**
* Indicates whether this model exists in the database.
*/
protected bool $exists = false;
public function __construct(array $attributes = [], bool $exists = false)
{
$this->exists = $exists;
if ($exists) {
$this->forceFill($attributes);
} else {
$this->fill($attributes);
}
$this->syncOriginal();
}
/**
* Begin a fluent query for the model's table.
*/
public static function query(): ModelQuery
{
return (new static())->newQuery();
}
public static function with(array|string $relations): ModelQuery
{
return static::query()->with($relations);
}
public static function withCount(array|string $relations): ModelQuery
{
return static::query()->withCount($relations);
}
/**
* Retrieve all rows for the model.
*
* @return static[]
*/
public static function all(): array
{
return static::query()->get();
}
/**
* Paginate model results.
*/
public static function paginate(int $perPage = 15, int $page = 1): Paginator
{
return static::query()->paginate($perPage, $page);
}
/**
* Simple pagination without executing an additional count query.
*/
public static function simplePaginate(int $perPage = 15, int $page = 1): Paginator
{
return static::query()->simplePaginate($perPage, $page);
}
/**
* Find a model by its primary key.
*/
public static function find(mixed $id): ?static
{
$result = static::query()->find($id);
return $result instanceof static ? $result : null;
}
/**
* Persist a new model instance with the provided attributes.
*/
public static function create(array $attributes): static
{
$model = new static();
$model->fill($attributes);
$model->save();
return $model;
}
/**
* Update the first matching record or create it with the provided values.
*/
public static function updateOrCreate(array $attributes, array $values = []): static
{
/** @var static $model */
$model = static::query()->updateOrCreate($attributes, $values);
return $model;
}
/**
* Retrieve the first matching record or create it with the provided values.
*/
public static function findOrCreate(array $attributes, array $values = []): static
{
/** @var static $model */
$model = static::query()->findOrCreate($attributes, $values);
return $model;
}
/**
* Determine if the model exists in the database.
*/
public function exists(): bool
{
return $this->exists;
}
/**
* Get the value of the primary key.
*/
public function getKey(): mixed
{
return $this->attributes[$this->primaryKey] ?? null;
}
/**
* Persist the model to the database (insert or update).
*/
public function save(): bool
{
return $this->exists ? $this->performUpdate() : $this->performInsert();
}
/**
* Mass-assign the provided attributes and persist the model.
*/
public function update(?array $attributes = null): bool
{
if(!is_null($attributes)) {
$this->fill($attributes);
}
return $this->save();
}
/**
* Delete the model from the database.
*/
public function delete(): bool
{
if (! $this->exists) {
return false;
}
if ($this->usesSoftDeletes()) {
return $this->performSoftDelete();
}
return $this->performHardDelete();
}
/**
* Force a hard delete even when soft deletes are enabled.
*/
public function forceDelete(): bool
{
if (! $this->exists) {
return false;
}
return $this->performHardDelete();
}
/**
* Restore a soft deleted model instance.
*/
public function restore(): bool
{
if (! $this->usesSoftDeletes()) {
return false;
}
if (! $this->trashed()) {
return true;
}
$this->fireHook('beforeRestore');
$this->attributes[$this->getDeletedAtColumn()] = null;
$restored = $this->performUpdate();
if ($restored) {
$this->fireHook('afterRestore');
}
return $restored;
}
/**
* Determine whether the model instance has been soft deleted.
*/
public function trashed(): bool
{
if (! $this->usesSoftDeletes()) {
return false;
}
return $this->getAttribute($this->getDeletedAtColumn()) !== null;
}
/**
* Expose whether the model uses soft deletes.
*/
public function usesSoftDeletes(): bool
{
return $this->softDeletes;
}
/**
* Resolve the column name that stores the soft delete timestamp.
*/
public function getDeletedAtColumn(): string
{
return $this->deletedAtColumn;
}
/**
* Reload the model state from the database.
*/
public function refresh(): static
{
if (! $this->exists) {
return $this;
}
$key = $this->getKey();
if ($key === null) {
throw new RuntimeException('Cannot refresh a model without a primary key value.');
}
$query = static::query();
if ($this->usesSoftDeletes()) {
$query = $query->withTrashed();
}
$fresh = $query->where($this->getPrimaryKey(), $key)
->first();
if ($fresh instanceof static) {
$this->forceFill($fresh->toArray());
$this->exists = true;
$this->syncOriginal();
}
return $this;
}
/**
* Fill the model with an array of attributes respecting the fillable whitelist.
*/
public function fill(array $attributes): static
{
foreach ($attributes as $key => $value) {
if ($this->isFillable($key)) {
$this->attributes[$key] = $value;
}
}
return $this;
}
/**
* Assign attributes directly, ignoring fillable restrictions.
*/
public function forceFill(array $attributes): static
{
foreach ($attributes as $key => $value) {
$this->attributes[$key] = $value;
}
return $this;
}
/**
* Retrieve an attribute value by key.
*/
public function getAttribute(string $key): mixed
{
return $this->attributes[$key] ?? null;
}
/**
* Determine if a given attribute or relation is set.
*/
public function hasAttribute(string $key): bool
{
return array_key_exists($key, $this->attributes);
}
/**
* Convert the model instance to an array.
*
* @return array<string, mixed>
*/
public function toArray(): array
{
return $this->attributes;
}
public function jsonSerialize(): array
{
return $this->toArray();
}
/**
* Determine whether the given relation has been loaded.
*/
public function relationLoaded(string $key): bool
{
return array_key_exists($key, $this->relations);
}
/**
* Retrieve a previously loaded relation value.
*/
public function getRelation(string $key): mixed
{
return $this->relations[$key] ?? null;
}
/**
* Set a relation value on the model.
*/
public function setRelation(string $key, mixed $value): static
{
$this->relations[$key] = $value;
return $this;
}
/**
* Retrieve all loaded relations.
*
* @return array<string, mixed>
*/
public function getRelations(): array
{
return $this->relations;
}
/**
* Determine if the model has any dirty (changed) attributes.
*/
public function isDirty(): bool
{
return ! empty($this->getDirty());
}
/**
* Return the underlying attribute array.
*/
public function getAttributes(): array
{
return $this->attributes;
}
/**
* Get the original raw attributes loaded from storage.
*/
public function getOriginal(): array
{
return $this->original;
}
/**
* Accessor for the primary key column name.
*/
public function getPrimaryKey(): string
{
return $this->primaryKey;
}
protected function ensureUuidKey(): void
{
if (! $this->usesUuid) {
return;
}
$column = $this->uuidColumn ?? $this->primaryKey;
if (! $column) {
return;
}
$current = $this->attributes[$column] ?? null;
if ($current === null || $current === '') {
$this->attributes[$column] = Str::uuid();
}
}
protected function singularTableName(string $table): string
{
if (str_ends_with($table, 'ies')) {
return substr($table, 0, -3) . 'y';
}
if (preg_match('/(xes|ses|zes|ches|shes)$/', $table)) {
return substr($table, 0, -2);
}
if (str_ends_with($table, 's')) {
return substr($table, 0, -1);
}
return $table;
}
protected function pluralizeTableName(string $table): string
{
if (str_ends_with($table, 'y')) {
return substr($table, 0, -1) . 'ies';
}
if (preg_match('/(s|x|z|ch|sh)$/', $table)) {
return $table . 'es';
}
if (!str_ends_with($table, 's')) {
return $table . 's';
}
return $table;
}
/**
* Lifecycle hooks. Override in child classes as needed.
*/
protected function beforeCreate(): void {}
protected function afterCreate(): void {}
protected function beforeUpdate(): void {}
protected function afterUpdate(): void {}
protected function beforeSave(): void {}
protected function afterSave(): void {}
protected function beforeDelete(): void {}
protected function afterDelete(): void {}
protected function beforeRestore(): void {}
protected function afterRestore(): void {}
private function fireHook(string $method): void
{
if (!method_exists($this, $method)) {
return;
}
$reflection = new ReflectionMethod($this, $method);
if ($reflection->getDeclaringClass()->getName() === self::class) {
return; // Skip base no-op definitions
}
$reflection->invoke($this);
}
public function __get(string $key): mixed
{
if (property_exists($this, $key)) {
return $this->{$key};
}
if ($this->hasAttribute($key)) {
return $this->attributes[$key];
}
return $this->getRelationValue($key);
}
public function __set(string $key, mixed $value): void
{
if (property_exists($this, $key)) {
$this->{$key} = $value;
return;
}
$this->attributes[$key] = $value;
}
public function __isset(string $key): bool
{
if (property_exists($this, $key)) {
return isset($this->{$key});
}
if ($this->hasAttribute($key)) {
return isset($this->attributes[$key]);
}
return $this->relationLoaded($key) && isset($this->relations[$key]);
}
public function __unset(string $key): void
{
unset($this->attributes[$key], $this->relations[$key]);
}
}