1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Isswp101\Persimmon; |
4
|
|
|
|
5
|
|
|
use Elasticsearch\Common\Exceptions\Missing404Exception; |
6
|
|
|
use Exception; |
7
|
|
|
use Isswp101\Persimmon\Contracts\Arrayable; |
8
|
|
|
use Isswp101\Persimmon\Contracts\Jsonable; |
9
|
|
|
use Isswp101\Persimmon\Contracts\Stringable; |
10
|
|
|
use Isswp101\Persimmon\DAL\IDAL; |
11
|
|
|
use Isswp101\Persimmon\Exceptions\ModelNotFoundException; |
12
|
|
|
use Isswp101\Persimmon\Traits\Cacheable; |
13
|
|
|
use Isswp101\Persimmon\Traits\Eventable; |
14
|
|
|
use Isswp101\Persimmon\Traits\Fillable; |
15
|
|
|
use Isswp101\Persimmon\Traits\Idable; |
16
|
|
|
use Isswp101\Persimmon\Traits\Mergeable; |
17
|
|
|
use Isswp101\Persimmon\Traits\Presentable; |
18
|
|
|
use Isswp101\Persimmon\Traits\Timestampable; |
19
|
|
|
use Isswp101\Persimmon\Traits\Userable; |
20
|
|
|
use JsonSerializable; |
21
|
|
|
use LogicException; |
22
|
|
|
|
23
|
|
|
abstract class Model implements Arrayable, Jsonable, Stringable, JsonSerializable |
24
|
|
|
{ |
25
|
|
|
use Presentable, Eventable, Mergeable; |
26
|
|
|
use Idable, Userable, Timestampable; |
27
|
|
|
use Fillable, Cacheable; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var IDAL |
31
|
|
|
*/ |
32
|
|
|
public $_dal; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var bool |
36
|
|
|
*/ |
37
|
|
|
public $_exist = false; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Create a new instance. |
41
|
|
|
* |
42
|
|
|
* @param IDAL $dal |
43
|
|
|
* @param array $attributes |
44
|
|
|
*/ |
45
|
52 |
|
public function __construct(IDAL $dal, array $attributes = []) |
46
|
|
|
{ |
47
|
52 |
|
$this->_dal = $dal; |
48
|
|
|
|
49
|
52 |
|
$this->fill($attributes); |
50
|
52 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
|
public function toArray() |
56
|
|
|
{ |
57
|
28 |
|
return array_where(get_object_vars($this), function ($key) { |
58
|
28 |
|
return !starts_with($key, '_'); |
59
|
28 |
|
}); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
|
|
public function jsonSerialize() |
66
|
|
|
{ |
67
|
|
|
return $this->toArray(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Clone object. |
72
|
|
|
*/ |
73
|
|
|
public function __clone() |
74
|
|
|
{ |
75
|
|
|
$this->_dal = clone $this->_dal; |
76
|
|
|
$this->_dal->setModel($this); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Save the model. |
81
|
|
|
* |
82
|
|
|
* @param array $columns |
83
|
|
|
* @return bool |
84
|
|
|
*/ |
85
|
24 |
|
public function save($columns = ['*']) |
86
|
|
|
{ |
87
|
24 |
|
$columns = $columns ? (array)$columns : ['*']; |
88
|
|
|
|
89
|
24 |
|
if ($this->saving() === false) { |
90
|
|
|
return false; |
91
|
|
|
} |
92
|
|
|
|
93
|
24 |
|
$this->fillTimestamp(); |
94
|
|
|
|
95
|
24 |
|
$this->_dal->put($columns); |
96
|
|
|
|
97
|
24 |
|
$this->_exist = true; |
98
|
|
|
|
99
|
|
|
// self::cache()->put($id, $this); |
100
|
|
|
|
101
|
24 |
|
if ($this->saved() === false) { |
102
|
|
|
return false; |
103
|
|
|
} |
104
|
|
|
|
105
|
24 |
|
return true; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Delete the model. |
110
|
|
|
* |
111
|
|
|
* @return bool |
112
|
|
|
*/ |
113
|
6 |
|
public function delete() |
114
|
|
|
{ |
115
|
6 |
|
if ($this->deleting() === false) { |
116
|
2 |
|
return false; |
117
|
|
|
} |
118
|
|
|
|
119
|
6 |
|
$this->_dal->delete(); |
120
|
|
|
|
121
|
6 |
|
$this->_exist = false; |
122
|
|
|
|
123
|
6 |
|
$cache = self::cache(); |
124
|
6 |
|
$cache->forget($this->getId()); |
125
|
|
|
|
126
|
6 |
|
if ($this->deleted() === false) { |
127
|
|
|
return false; |
128
|
|
|
} |
129
|
|
|
|
130
|
6 |
|
return true; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Create a new instance. |
135
|
|
|
* |
136
|
|
|
* @return static |
137
|
|
|
*/ |
138
|
|
|
public static function createInstance() |
139
|
|
|
{ |
140
|
|
|
throw new LogicException('Static method `createInstance()` must be overridden and return `new static(args)`'); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Find a model by its primary key. |
145
|
|
|
* |
146
|
|
|
* @param mixed $id |
147
|
|
|
* @param array $columns |
148
|
|
|
* @param array $options |
149
|
|
|
* @return static |
150
|
|
|
*/ |
151
|
22 |
|
public static function find($id, array $columns = ['*'], $options = []) |
152
|
|
|
{ |
153
|
|
|
// Return a cached instance if one exists |
154
|
22 |
|
$cache = self::cache(); |
155
|
22 |
|
if ($cache->containsAttributes($id, $columns)) { |
156
|
10 |
|
return $cache->get($id); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
// Return attributes which are not cached |
160
|
14 |
|
$columns = $cache->getNotCachedAttributes($id, $columns); |
161
|
|
|
|
162
|
|
|
// Create a new model |
163
|
14 |
|
$model = static::createInstance(); |
164
|
14 |
|
$model->setId($id); |
165
|
|
|
|
166
|
|
|
// Merge options |
167
|
14 |
|
$options = array_merge($options, ['columns' => $columns == ['*'] ? [] : $columns]); |
168
|
|
|
|
169
|
|
|
// Get by id |
170
|
|
|
try { |
171
|
14 |
|
$model->_dal->get($id, $options); |
172
|
14 |
|
} catch (Missing404Exception $e) { |
173
|
4 |
|
return null; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
// Fill internal attributes |
177
|
10 |
|
$model->_exist = true; |
178
|
|
|
|
179
|
|
|
// Put model to cache |
180
|
10 |
|
$model = $cache->put($id, $model, $columns); |
181
|
|
|
|
182
|
10 |
|
return $model; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Find a model by its primary key or return new model. |
187
|
|
|
* |
188
|
|
|
* @param mixed $id |
189
|
|
|
* @return static |
190
|
|
|
*/ |
191
|
2 |
|
public static function findOrNew($id) |
192
|
|
|
{ |
193
|
2 |
|
$model = static::find($id); |
194
|
2 |
|
if (is_null($model)) { |
195
|
2 |
|
$model = static::createInstance(); |
196
|
2 |
|
$model->setId($id); |
197
|
2 |
|
} |
198
|
2 |
|
return $model; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Find a model by its primary key or throw an exception. |
203
|
|
|
* |
204
|
|
|
* @param mixed $id |
205
|
|
|
* @param array $columns |
206
|
|
|
* @param int $parent |
207
|
|
|
* @return static |
208
|
|
|
*/ |
209
|
4 |
|
public static function findOrFail($id, array $columns = ['*'], $parent = null) |
210
|
|
|
{ |
211
|
4 |
|
$model = static::find($id, $columns, ['parent' => $parent]); |
212
|
4 |
|
if (is_null($model)) { |
213
|
2 |
|
throw new ModelNotFoundException(get_called_class(), $id); |
214
|
|
|
} |
215
|
2 |
|
return $model; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Save a new model and return the instance. |
220
|
|
|
* |
221
|
|
|
* @param array $attributes |
222
|
|
|
* @throws Exception |
223
|
|
|
* @return static |
224
|
|
|
*/ |
225
|
8 |
|
public static function create(array $attributes = []) |
226
|
|
|
{ |
227
|
8 |
|
$model = static::createInstance(); |
228
|
|
|
|
229
|
8 |
|
if (array_key_exists('id', $attributes)) { |
230
|
8 |
|
$model->setId($attributes['id']); |
231
|
8 |
|
} |
232
|
|
|
|
233
|
8 |
|
$model->fill($attributes); |
234
|
|
|
|
235
|
8 |
|
$model->save(); |
236
|
|
|
|
237
|
8 |
|
return $model; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Destroy the models by the given id. |
242
|
|
|
* |
243
|
|
|
* @param mixed $id |
244
|
|
|
*/ |
245
|
2 |
|
public static function destroy($id) |
246
|
|
|
{ |
247
|
2 |
|
$ids = is_array($id) ? $id : [$id]; |
248
|
2 |
|
foreach ($ids as $id) { |
249
|
2 |
|
$model = static::find($id); |
250
|
2 |
|
if (!is_null($model)) { |
251
|
2 |
|
$model->delete(); |
252
|
2 |
|
} |
253
|
2 |
|
} |
254
|
2 |
|
} |
255
|
|
|
} |
256
|
|
|
|