1 | <?php |
||
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 = []) |
|
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) |
|
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) |
|
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 = []) |
|
239 | |||
240 | /** |
||
241 | * Destroy the models by the given id. |
||
242 | * |
||
243 | * @param mixed $id |
||
244 | */ |
||
245 | 2 | public static function destroy($id) |
|
255 | } |
||
256 |