|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Isswp101\Persimmon\Model; |
|
4
|
|
|
|
|
5
|
|
|
use Isswp101\Persimmon\Collection\ICollection; |
|
6
|
|
|
use Isswp101\Persimmon\DI\Container; |
|
7
|
|
|
use Isswp101\Persimmon\Exceptions\IllegalCollectionException; |
|
8
|
|
|
use Isswp101\Persimmon\Exceptions\ModelNotFoundException; |
|
9
|
|
|
use Isswp101\Persimmon\QueryBuilder\IQueryBuilder; |
|
10
|
|
|
use Isswp101\Persimmon\Traits\Containerable; |
|
11
|
|
|
use Isswp101\Persimmon\Traits\Eventable; |
|
12
|
|
|
use Isswp101\Persimmon\Traits\Timestampable; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @TODO |
|
16
|
|
|
* 1. Cache |
|
17
|
|
|
* 2. Consider columns when searching |
|
18
|
|
|
* 3. Check __clone() |
|
19
|
|
|
*/ |
|
20
|
|
|
abstract class Eloquent implements IEloquent |
|
21
|
|
|
{ |
|
22
|
|
|
use Containerable, Timestampable, Eventable; |
|
23
|
|
|
|
|
24
|
|
|
protected $primaryKey; |
|
25
|
|
|
protected $exists = false; |
|
26
|
|
|
protected $timestamps = false; |
|
27
|
|
|
|
|
28
|
|
|
/** @MustBeOverridden */ |
|
29
|
|
|
const COLLECTION = null; |
|
30
|
|
|
|
|
31
|
|
|
const PRIMARY_KEY = 'id'; |
|
32
|
|
|
const CREATED_AT = 'created_at'; |
|
33
|
|
|
const UPDATED_AT = 'updated_at'; |
|
34
|
|
|
|
|
35
|
|
|
abstract protected static function di(): Container; |
|
36
|
|
|
|
|
37
|
|
|
public function __construct(array $attributes = []) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->fill($attributes); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function setPrimaryKey(string $key) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->primaryKey = $key; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function getPrimaryKey(): string |
|
48
|
|
|
{ |
|
49
|
|
|
if ($this->primaryKey == null) { |
|
50
|
|
|
$this->setPrimaryKey($this->{static::PRIMARY_KEY}); |
|
51
|
|
|
} |
|
52
|
|
|
return $this->primaryKey; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
final public static function getCollection(): string |
|
56
|
|
|
{ |
|
57
|
|
|
if (static::COLLECTION == null) { |
|
58
|
|
|
throw new IllegalCollectionException(); |
|
59
|
|
|
} |
|
60
|
|
|
return static::COLLECTION; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function exists(bool $value = null): bool |
|
64
|
|
|
{ |
|
65
|
|
|
if ($value != null) { |
|
66
|
|
|
$this->exists = $value; |
|
67
|
|
|
} |
|
68
|
|
|
return $this->exists; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public static function all(IQueryBuilder $query, callable $callback = null): ICollection |
|
72
|
|
|
{ |
|
73
|
|
|
$collection = static::di()->getRepository()->all($query, static::class, |
|
74
|
|
|
function (IEloquent $model) use ($callback) { |
|
75
|
|
|
$model->exists(true); |
|
76
|
|
|
if ($callback != null) { |
|
77
|
|
|
$callback($model); |
|
78
|
|
|
} |
|
79
|
|
|
}); |
|
80
|
|
|
return $collection; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public static function find($id, array $columns = []): IEloquent |
|
84
|
|
|
{ |
|
85
|
|
|
$model = static::di()->getRepository()->find($id, static::class, $columns); |
|
86
|
|
|
if ($model != null) { |
|
87
|
|
|
$model->exists = true; |
|
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
return $model; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public static function findOrFail($id, array $columns = []): IEloquent |
|
93
|
|
|
{ |
|
94
|
|
|
$model = static::find($id, $columns); |
|
95
|
|
|
if ($model == null) { |
|
96
|
|
|
throw new ModelNotFoundException(get_called_class(), $id); |
|
97
|
|
|
} |
|
98
|
|
|
return $model; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public static function create(array $attributes): IEloquent |
|
102
|
|
|
{ |
|
103
|
|
|
$model = new static($attributes); |
|
104
|
|
|
$model->save(); |
|
105
|
|
|
return $model; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public static function destroy($id) |
|
109
|
|
|
{ |
|
110
|
|
|
static::findOrFail($id)->delete(); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function save(array $columns = []) |
|
114
|
|
|
{ |
|
115
|
|
|
if ($this->saving() === false) { |
|
116
|
|
|
return; |
|
117
|
|
|
} |
|
118
|
|
|
$repository = $this->di()->getRepository(); |
|
119
|
|
|
if ($this->timestamps) { |
|
120
|
|
|
$this->updateTimestamps(); |
|
121
|
|
|
} |
|
122
|
|
|
if (!$this->exists) { |
|
123
|
|
|
$repository->insert($this); |
|
124
|
|
|
} else { |
|
125
|
|
|
$repository->update($this); |
|
126
|
|
|
} |
|
127
|
|
|
$this->exists = true; |
|
128
|
|
|
if ($this->saved() === false) { |
|
129
|
|
|
return; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
public function delete() |
|
134
|
|
|
{ |
|
135
|
|
|
if ($this->deleting() === false) { |
|
136
|
|
|
return; |
|
137
|
|
|
} |
|
138
|
|
|
$this->di()->getRepository()->delete($this); |
|
139
|
|
|
$this->exists = false; |
|
140
|
|
|
if ($this->deleted() === false) { |
|
141
|
|
|
return; |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
} |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: