1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Quantum PHP Framework |
5
|
|
|
* |
6
|
|
|
* An open source software development framework for PHP |
7
|
|
|
* |
8
|
|
|
* @package Quantum |
9
|
|
|
* @author Arman Ag. <[email protected]> |
10
|
|
|
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
11
|
|
|
* @link http://quantum.softberg.org/ |
12
|
|
|
* @since 2.9.8 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Quantum\Model; |
16
|
|
|
|
17
|
|
|
use Quantum\Libraries\Database\Contracts\DbalInterface; |
18
|
|
|
use Quantum\Paginator\Exceptions\PaginatorException; |
19
|
|
|
use Quantum\Paginator\Factories\PaginatorFactory; |
20
|
|
|
use Quantum\Model\Exceptions\ModelException; |
21
|
|
|
use Quantum\App\Exceptions\BaseException; |
22
|
|
|
use Quantum\Paginator\Paginator; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class QtModel |
26
|
|
|
* @package Quantum\Model |
27
|
|
|
* @method string getTable() |
28
|
|
|
* @method string getModelName() |
29
|
|
|
* @method static select(...$columns) |
30
|
|
|
* @method static findOne(int $id) |
31
|
|
|
* @method static findOneBy(string $column, $value) |
32
|
|
|
* @method static first() |
33
|
|
|
* @method static criteria(string $column, string $operator, $value = null) |
34
|
|
|
* @method static criterias(...$criterias) |
35
|
|
|
* @method static having(string $column, string $operator, string $value = null) |
36
|
|
|
* @method static orderBy(string $column, string $direction) |
37
|
|
|
* @method static offset(int $offset) |
38
|
|
|
* @method static limit(int $limit) |
39
|
|
|
* @method int count() |
40
|
|
|
* @method array asArray() |
41
|
|
|
* @method static create() |
42
|
|
|
* @method bool save() |
43
|
|
|
* @method bool delete() |
44
|
|
|
* @method bool deleteMany() |
45
|
|
|
* @method static joinTo(QtModel $model, bool $switch = true) |
46
|
|
|
* @method static joinThrough(QtModel $model, bool $switch = true) |
47
|
|
|
* @method static isNull(string $column) |
48
|
|
|
* @method static isNotNull(string $column) |
49
|
|
|
*/ |
50
|
|
|
abstract class QtModel |
51
|
|
|
{ |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* The database table associated with model |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
public $table; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Id column of table |
61
|
|
|
* @var string |
62
|
|
|
*/ |
63
|
|
|
public $idColumn = 'id'; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Foreign keys |
67
|
|
|
* @var array |
68
|
|
|
*/ |
69
|
|
|
public $foreignKeys = []; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Models fillable properties |
73
|
|
|
* @var array |
74
|
|
|
*/ |
75
|
|
|
protected $fillable = []; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Models hidden properties |
79
|
|
|
* @var array |
80
|
|
|
*/ |
81
|
|
|
public $hidden = []; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* ORM database abstract layer object |
85
|
|
|
* @var DbalInterface |
86
|
|
|
*/ |
87
|
|
|
protected $ormInstance; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Sets the ORM instance |
91
|
|
|
* @param DbalInterface $ormInstance |
92
|
|
|
*/ |
93
|
|
|
public function setOrmInstance(DbalInterface $ormInstance) |
94
|
|
|
{ |
95
|
|
|
$this->ormInstance = $ormInstance; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Gets ORM instance of current model |
100
|
|
|
* @return DbalInterface |
101
|
|
|
*/ |
102
|
|
|
public function getOrmInstance(): DbalInterface |
103
|
|
|
{ |
104
|
|
|
return $this->ormInstance; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Returns the model's foreign key relations. |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
|
|
public function relations(): array |
112
|
|
|
{ |
113
|
|
|
return []; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return ModelCollection |
118
|
|
|
*/ |
119
|
|
|
public function get(): ModelCollection |
120
|
|
|
{ |
121
|
|
|
$models = array_map(function ($item) { |
122
|
|
|
return wrapToModel($item, static::class); |
123
|
|
|
}, $this->ormInstance->get()); |
124
|
|
|
|
125
|
|
|
return new ModelCollection($models); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param int $perPage |
130
|
|
|
* @param int $currentPage |
131
|
|
|
* @return Paginator |
132
|
|
|
* @throws BaseException |
133
|
|
|
* @throws PaginatorException |
134
|
|
|
*/ |
135
|
|
|
public function paginate(int $perPage, int $currentPage = 1): Paginator |
136
|
|
|
{ |
137
|
|
|
return PaginatorFactory::create(Paginator::MODEL, [ |
138
|
|
|
'model' => $this, |
139
|
|
|
'perPage' => $perPage, |
140
|
|
|
'page' => $currentPage |
141
|
|
|
]); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Fills the object properties |
146
|
|
|
* @param array $props |
147
|
|
|
* @return QtModel |
148
|
|
|
* @throws ModelException |
149
|
|
|
*/ |
150
|
|
|
public function fillObjectProps(array $props): QtModel |
151
|
|
|
{ |
152
|
|
|
foreach ($props as $key => $value) { |
153
|
|
|
if ($key == $this->idColumn) { |
154
|
|
|
continue; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
if (!in_array($key, $this->fillable)) { |
158
|
|
|
throw ModelException::inappropriateProperty($key); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
$this->prop($key, $value); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return $this; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @return bool |
169
|
|
|
*/ |
170
|
|
|
public function isEmpty() : bool |
171
|
|
|
{ |
172
|
|
|
return empty($this->asArray()); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Sets or gets the model property |
178
|
|
|
* @param string $property |
179
|
|
|
* @param mixed|null $value |
180
|
|
|
* @return mixed |
181
|
|
|
*/ |
182
|
|
|
public function prop(string $property, $value = null) |
183
|
|
|
{ |
184
|
|
|
return $this->ormInstance->prop(...func_get_args()); |
|
|
|
|
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Gets the model property with magic |
189
|
|
|
* @param string $property |
190
|
|
|
* @return mixed |
191
|
|
|
*/ |
192
|
|
|
public function __get(string $property) |
193
|
|
|
{ |
194
|
|
|
return $this->prop($property); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Sets a value to the model property with magic |
199
|
|
|
* @param string $property |
200
|
|
|
* @param mixed $value |
201
|
|
|
*/ |
202
|
|
|
public function __set(string $property, $value) |
203
|
|
|
{ |
204
|
|
|
$this->prop($property, $value); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @param string $method |
209
|
|
|
* @param mixed|null $args |
210
|
|
|
* @return $this|array|int|string |
211
|
|
|
* @throws ModelException |
212
|
|
|
*/ |
213
|
|
|
public function __call(string $method, $args = null) |
214
|
|
|
{ |
215
|
|
|
if (!method_exists($this->ormInstance, $method)) { |
216
|
|
|
throw ModelException::undefinedMethod($method); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
$result = $this->ormInstance->{$method}(...$args); |
220
|
|
|
|
221
|
|
|
if (!$result instanceof DbalInterface) { |
222
|
|
|
return $result; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
return $this; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Keeps only relevant props at serialization |
230
|
|
|
* @return string[] |
231
|
|
|
*/ |
232
|
|
|
public function __sleep() |
233
|
|
|
{ |
234
|
|
|
return [ |
235
|
|
|
'table', |
236
|
|
|
'idColumn', |
237
|
|
|
'hidden' |
238
|
|
|
]; |
239
|
|
|
} |
240
|
|
|
} |