|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Isswp101\Persimmon\Model; |
|
4
|
|
|
|
|
5
|
|
|
use Isswp101\Persimmon\Contracts\Presentable; |
|
6
|
|
|
use Isswp101\Persimmon\Contracts\Storable; |
|
7
|
|
|
use Isswp101\Persimmon\DI\Container; |
|
8
|
|
|
use Isswp101\Persimmon\Exceptions\IllegalCollectionException; |
|
9
|
|
|
use Isswp101\Persimmon\Exceptions\ModelNotFoundException; |
|
10
|
|
|
use Isswp101\Persimmon\Traits\Containerable; |
|
11
|
|
|
use Isswp101\Persimmon\Traits\Eventable; |
|
12
|
|
|
use Isswp101\Persimmon\Traits\Timestampable; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* |
|
16
|
|
|
* @TODO |
|
17
|
|
|
* 1. Events + |
|
18
|
|
|
* 2. Timestamps + |
|
19
|
|
|
* 3. Cache |
|
20
|
|
|
* 4. Update __toString() + |
|
21
|
|
|
*/ |
|
22
|
|
|
abstract class BaseModel implements IEloquent, Storable, Presentable |
|
23
|
|
|
{ |
|
24
|
|
|
use Containerable, Timestampable, Eventable; |
|
25
|
|
|
|
|
26
|
|
|
protected $exists = false; |
|
27
|
|
|
protected $timestamps = false; |
|
28
|
|
|
|
|
29
|
|
|
/** @MustBeOverridden */ |
|
30
|
|
|
const collection = null; |
|
31
|
|
|
|
|
32
|
|
|
const PRIMARY_KEY = 'id'; |
|
33
|
|
|
const CREATED_AT = 'created_at'; |
|
34
|
|
|
const UPDATED_AT = 'updated_at'; |
|
35
|
|
|
|
|
36
|
|
|
abstract protected static function di(): Container; |
|
37
|
|
|
|
|
38
|
|
|
public function __construct(array $attributes = []) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->fill($attributes); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function getPrimaryKey(): string |
|
44
|
|
|
{ |
|
45
|
|
|
return $this->{static::PRIMARY_KEY}; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function setPrimaryKey(string $key) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->{static::PRIMARY_KEY} = $key; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public static function getCollection(): string |
|
54
|
|
|
{ |
|
55
|
|
|
if (static::collection == null) { |
|
56
|
|
|
throw new IllegalCollectionException(); |
|
57
|
|
|
} |
|
58
|
|
|
return static::collection; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function exists(bool $value = null): bool |
|
62
|
|
|
{ |
|
63
|
|
|
if ($value != null) { |
|
64
|
|
|
$this->exists = $value; |
|
65
|
|
|
} |
|
66
|
|
|
return $this->exists; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public static function find($id, array $columns = null): IEloquent |
|
70
|
|
|
{ |
|
71
|
|
|
$model = static::di()->getRepository()->find($id, static::class, $columns); |
|
|
|
|
|
|
72
|
|
|
if ($model != null) { |
|
73
|
|
|
$model->exists = true; |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
return $model; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public static function findOrFail($id, array $columns = null): IEloquent |
|
79
|
|
|
{ |
|
80
|
|
|
$model = static::find($id, $columns); |
|
81
|
|
|
if ($model == null) { |
|
82
|
|
|
throw new ModelNotFoundException(get_called_class(), $id); |
|
83
|
|
|
} |
|
84
|
|
|
return $model; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public static function create(array $attributes): IEloquent |
|
88
|
|
|
{ |
|
89
|
|
|
return null; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public static function destroy($id) |
|
93
|
|
|
{ |
|
94
|
|
|
return null; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function save(array $columns = null) |
|
98
|
|
|
{ |
|
99
|
|
|
if ($this->saving() === false) { |
|
100
|
|
|
return; |
|
101
|
|
|
} |
|
102
|
|
|
$repository = $this->di()->getRepository(); |
|
103
|
|
|
if ($this->timestamps) { |
|
104
|
|
|
$this->updateTimestamps(); |
|
105
|
|
|
} |
|
106
|
|
|
if (!$this->exists) { |
|
107
|
|
|
$repository->insert($this); |
|
108
|
|
|
} else { |
|
109
|
|
|
$repository->update($this); |
|
110
|
|
|
} |
|
111
|
|
|
$this->exists = true; |
|
112
|
|
|
if ($this->saved() === false) { |
|
113
|
|
|
return; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function delete() |
|
118
|
|
|
{ |
|
119
|
|
|
if ($this->deleting() === false) { |
|
120
|
|
|
return; |
|
121
|
|
|
} |
|
122
|
|
|
$this->di()->getRepository()->delete($this->getId()); |
|
123
|
|
|
$this->exists = false; |
|
124
|
|
|
if ($this->deleted() === false) { |
|
125
|
|
|
return; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
} |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.