1 | <?php |
||
15 | abstract class Eloquent implements IEloquent |
||
16 | { |
||
17 | use Containerable, Timestampable, Eventable; |
||
18 | |||
19 | protected $primaryKey; |
||
20 | protected $exists = false; |
||
21 | protected $timestamps = false; |
||
22 | protected $cache = false; |
||
23 | |||
24 | /** @MustBeOverridden */ |
||
25 | const COLLECTION = null; |
||
26 | |||
27 | const PRIMARY_KEY = 'id'; |
||
28 | const CREATED_AT = 'created_at'; |
||
29 | const UPDATED_AT = 'updated_at'; |
||
30 | |||
31 | abstract protected static function di(): Container; |
||
32 | |||
33 | public function __construct(array $attributes = []) |
||
34 | { |
||
35 | $this->fill($attributes); |
||
36 | } |
||
37 | |||
38 | public function setPrimaryKey(string $key) |
||
39 | { |
||
40 | $this->primaryKey = $key; |
||
41 | } |
||
42 | |||
43 | public function getPrimaryKey(): string |
||
44 | { |
||
45 | if ($this->primaryKey == null) { |
||
46 | $this->setPrimaryKey($this->{static::PRIMARY_KEY}); |
||
47 | } |
||
48 | return $this->primaryKey; |
||
49 | } |
||
50 | |||
51 | final public static function getCollection(): string |
||
52 | { |
||
53 | if (static::COLLECTION == null) { |
||
54 | throw new IllegalCollectionException(); |
||
55 | } |
||
56 | return static::COLLECTION; |
||
57 | } |
||
58 | |||
59 | public function exists(bool $value = null): bool |
||
66 | |||
67 | public static function all(IQueryBuilder $query, callable $callback = null): ICollection |
||
68 | { |
||
69 | $collection = static::di()->getRepository()->all($query, static::class, |
||
70 | function (IEloquent $model) use ($callback) { |
||
71 | $model->exists(true); |
||
78 | |||
79 | public static function find(string $id, array $columns = []): ?IEloquent |
||
94 | |||
95 | public static function findOrFail(string $id, array $columns = []): IEloquent |
||
103 | |||
104 | public static function create(array $attributes): IEloquent |
||
110 | |||
111 | public static function destroy(string $id): void |
||
115 | |||
116 | public function save(): void |
||
135 | |||
136 | public function delete(): void |
||
147 | } |