-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.php
More file actions
365 lines (309 loc) · 10.6 KB
/
Model.php
File metadata and controls
365 lines (309 loc) · 10.6 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
<?php
/**
* @package InitORM\ORM
* @license MIT
*/
declare(strict_types=1);
namespace InitORM\ORM;
use BadMethodCallException;
use InitORM\Database\Facade\DB;
use InitORM\Database\Interfaces\DatabaseInterface;
use InitORM\DBAL\DataMapper\Interfaces\DataMapperInterface;
use InitORM\ORM\Exceptions\DeletableException;
use InitORM\ORM\Exceptions\ModelException;
use InitORM\ORM\Exceptions\ReadableException;
use InitORM\ORM\Exceptions\UpdatableException;
use InitORM\ORM\Exceptions\WritableException;
use InitORM\ORM\Interfaces\EntityInterface;
use InitORM\ORM\Interfaces\ModelInterface;
use InitORM\ORM\Utils\Helper;
use ReflectionClass;
/**
* Abstract base for active-record-style models. Each subclass binds a
* database table (via {@see self::$schema}) to an entity class (via
* {@see self::$entity}) and exposes the CRUD helpers declared on
* {@see ModelInterface}.
*
* A model holds a private {@see DatabaseInterface}. Unknown method calls are
* forwarded to it via {@see self::__call()}; when the underlying Database
* returns itself (chainable builder calls), the call is re-wrapped to return
* this Model so fluent chains span the wrapper boundary — for example
* `MyModel::where(...)->limit(...)->read()` works because both `where` and
* `limit` return the Model.
*
* @mixin DatabaseInterface
*/
abstract class Model implements ModelInterface
{
protected DatabaseInterface $db;
/**
* Optional standalone connection credentials. When null, the model
* binds to the shared {@see DB::getDatabase()} facade instance.
*
* @var array<string, mixed>|null
*/
protected ?array $credentials = null;
/**
* Backing table name. Auto-derived from the subclass short name via
* {@see Helper::camelCaseToSnakeCase()} when left unset.
*/
protected string $schema;
/**
* Primary-key column. Used by {@see self::update()} to lift the PK out
* of the SET map into a WHERE clause and by {@see self::save()} to
* decide between insert and update.
*/
protected string $schemaId = 'id';
/**
* Entity class used to hydrate read() results. Must implement
* {@see EntityInterface} (otherwise hydration is delegated to PDO's
* FETCH_CLASS which still works, but the contract is loosened).
*
* @var class-string
*/
protected string $entity = Entity::class;
protected bool $writable = true;
protected bool $readable = true;
protected bool $updatable = true;
protected bool $deletable = true;
/**
* Column name auto-filled with `date($timestampFormat)` on each create.
* Disabled when null.
*/
protected ?string $createdField = null;
/**
* Column name auto-filled with `date($timestampFormat)` on each update.
* Disabled when null.
*/
protected ?string $updatedField = null;
/**
* When true, {@see self::delete()} sets {@see self::$deletedField}
* instead of issuing a DELETE; reads filter out rows whose
* {@see self::$deletedField} is non-null. {@see self::$deletedField} is
* required when this is on (enforced in the constructor).
*/
protected bool $useSoftDeletes = false;
/**
* Column name used to mark a row as soft-deleted (must be nullable in
* the underlying schema). Required when {@see self::$useSoftDeletes} is
* true.
*/
protected ?string $deletedField = null;
/**
* `date()` format string used for created / updated / deleted columns.
*/
protected string $timestampFormat = 'Y-m-d H:i:s';
/**
* One-shot scope flag set by {@see self::onlyDeleted()}; consumed by the
* next {@see self::read()} call.
*/
private bool $isOnlyDeleted = false;
/**
* @throws ModelException When {@see self::$useSoftDeletes} is on but no
* {@see self::$deletedField} is configured.
*/
public function __construct()
{
if (!isset($this->schema)) {
$shortName = (new ReflectionClass($this))->getShortName();
$this->schema = Helper::camelCaseToSnakeCase($shortName);
}
if ($this->useSoftDeletes && empty($this->deletedField)) {
throw new ModelException(sprintf(
'%s has $useSoftDeletes enabled but $deletedField is not configured.',
static::class
));
}
$this->db = $this->credentials === null
? DB::getDatabase()
: DB::connect($this->credentials);
}
/**
* Forward unknown calls to the inner {@see DatabaseInterface}. Chainable
* calls (those the Database forwards back as itself) re-wrap to return
* this Model so fluent chains continue across the wrapper boundary.
*
* @param array<int, mixed> $arguments
*
* @throws BadMethodCallException When the method does not exist on the
* underlying Database or query builder.
*/
public function __call(string $name, array $arguments): mixed
{
if (!method_exists($this->db, $name)) {
// Database itself forwards to the builder via __call; we can't
// method_exists() the builder transparently, so let Database
// decide and surface its DatabaseException as-is. The presence
// check is best-effort for the direct Database surface.
try {
$result = $this->db->{$name}(...$arguments);
} catch (\Throwable $e) {
throw new BadMethodCallException(
sprintf('Method "%s::%s" does not exist.', static::class, $name),
0,
$e
);
}
} else {
$result = $this->db->{$name}(...$arguments);
}
return $result instanceof DatabaseInterface ? $this : $result;
}
/**
* @inheritDoc
*/
public function getSchema(): string
{
return $this->schema;
}
/**
* @inheritDoc
*/
public function getSchemaId(): string
{
return $this->schemaId;
}
/**
* @inheritDoc
*/
public function getDatabase(): DatabaseInterface
{
return $this->db;
}
/**
* @inheritDoc
*/
public function create(array $set = []): bool
{
if (!$this->writable) {
throw new WritableException(sprintf('%s is not writable.', static::class));
}
if ($this->createdField !== null && $this->createdField !== '') {
$set[$this->createdField] = date($this->timestampFormat);
}
return $this->db->create($this->schema, $set);
}
/**
* @inheritDoc
*/
public function createBatch(array $set = []): bool
{
if (!$this->writable) {
throw new WritableException(sprintf('%s is not writable.', static::class));
}
if ($this->createdField !== null && $this->createdField !== '' && !empty($set)) {
$now = date($this->timestampFormat);
foreach ($set as &$row) {
$row[$this->createdField] = $now;
}
unset($row);
}
return $this->db->createBatch($this->schema, $set);
}
/**
* @inheritDoc
*/
public function read(array $selector = [], array $conditions = []): DataMapperInterface
{
if (!$this->readable) {
throw new ReadableException(sprintf('%s is not readable.', static::class));
}
if ($this->useSoftDeletes) {
if ($this->isOnlyDeleted) {
$this->db->whereIsNotNull($this->deletedField);
$this->isOnlyDeleted = false;
} else {
$this->db->whereIsNull($this->deletedField);
}
}
return $this->db
->read($this->schema, $selector, $conditions)
->asClass($this->entity);
}
/**
* @inheritDoc
*/
public function update(array $set = [], ?array $conditions = null): bool
{
if (!$this->updatable) {
throw new UpdatableException(sprintf('%s is not updatable.', static::class));
}
if ($this->schemaId !== '' && isset($set[$this->schemaId])) {
$this->db->where($this->schemaId, '=', $set[$this->schemaId]);
unset($set[$this->schemaId]);
}
if ($this->updatedField !== null && $this->updatedField !== '') {
$set[$this->updatedField] = date($this->timestampFormat);
}
if ($this->useSoftDeletes) {
$this->db->whereIsNull($this->deletedField);
}
return $this->db->update($this->schema, $set, $conditions);
}
/**
* @inheritDoc
*/
public function updateBatch(array $set = [], ?string $referenceColumn = null): bool
{
if (!$this->updatable) {
throw new UpdatableException(sprintf('%s is not updatable.', static::class));
}
if ($this->updatedField !== null && $this->updatedField !== '' && !empty($set)) {
$now = date($this->timestampFormat);
foreach ($set as &$row) {
$row[$this->updatedField] = $now;
}
unset($row);
}
if ($this->useSoftDeletes) {
$this->db->whereIsNull($this->deletedField);
}
return $this->db->updateBatch($referenceColumn ?? $this->schemaId, $this->schema, $set);
}
/**
* @inheritDoc
*/
public function delete(?array $conditions = null, bool $purge = false): bool
{
if (!$this->deletable) {
throw new DeletableException(sprintf('%s is not deletable.', static::class));
}
if ($this->useSoftDeletes && !$purge) {
$this->db
->whereIsNull($this->deletedField)
->set($this->deletedField, date($this->timestampFormat));
return $this->db->update($this->schema, null, $conditions);
}
return $this->db->delete($this->schema, $conditions);
}
/**
* @inheritDoc
*/
public function save(EntityInterface $entity): bool
{
$data = $entity->toArray();
$hasId = $this->schemaId !== ''
&& isset($data[$this->schemaId])
&& $data[$this->schemaId] !== null
&& $data[$this->schemaId] !== '';
return $hasId ? $this->update($data) : $this->create($data);
}
/**
* @inheritDoc
*/
public function onlyDeleted(): static
{
$this->isOnlyDeleted = true;
return $this;
}
/**
* @inheritDoc
*/
public function ignoreDeleted(): static
{
if ($this->useSoftDeletes) {
$this->db->whereIsNull($this->deletedField);
}
return $this;
}
}