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.6 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Quantum\Model; |
16
|
|
|
|
17
|
|
|
use Quantum\Libraries\Database\Contracts\DbalInterface; |
18
|
|
|
use Quantum\Model\Exceptions\ModelException; |
19
|
|
|
use Quantum\Paginator\Paginator; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class QtModel |
23
|
|
|
* @package Quantum\Model |
24
|
|
|
* @method string getTable() |
25
|
|
|
* @method DbalInterface select(...$columns) |
26
|
|
|
* @method DbalInterface findOne(int $id) |
27
|
|
|
* @method DbalInterface findOneBy(string $column, $value) |
28
|
|
|
* @method DbalInterface first() |
29
|
|
|
* @method DbalInterface criteria(string $column, string $operator, $value = null) |
30
|
|
|
* @method DbalInterface criterias(...$criterias) |
31
|
|
|
* @method DbalInterface having(string $column, string $operator, string $value = null) |
32
|
|
|
* @method DbalInterface orderBy(string $column, string $direction) |
33
|
|
|
* @method DbalInterface limit(int $limit) |
34
|
|
|
* @method array asArray() |
35
|
|
|
* @method DbalInterface create() |
36
|
|
|
* @method bool save() |
37
|
|
|
* @method bool delete() |
38
|
|
|
* @method bool deleteMany() |
39
|
|
|
* @method DbalInterface joinTo(QtModel $model, bool $switch = true) |
40
|
|
|
* @method DbalInterface joinThrough(QtModel $model, bool $switch = true) |
41
|
|
|
*/ |
42
|
|
|
abstract class QtModel |
43
|
|
|
{ |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* The database table associated with model |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
public $table; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Id column of table |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
public $idColumn = 'id'; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Foreign keys |
59
|
|
|
* @var array |
60
|
|
|
*/ |
61
|
|
|
public $foreignKeys = []; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Models fillable properties |
65
|
|
|
* @var array |
66
|
|
|
*/ |
67
|
|
|
protected $fillable = []; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Models hidden properties |
71
|
|
|
* @var array |
72
|
|
|
*/ |
73
|
|
|
public $hidden = []; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* ORM database abstract layer object |
77
|
|
|
* @var DbalInterface |
78
|
|
|
*/ |
79
|
|
|
private $ormInstance; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Sets the ORM instance |
83
|
|
|
* @param DbalInterface $ormInstance |
84
|
|
|
*/ |
85
|
|
|
public function setOrmInstance(DbalInterface $ormInstance) |
86
|
|
|
{ |
87
|
|
|
$this->ormInstance = $ormInstance; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return ModelCollection |
92
|
|
|
*/ |
93
|
|
|
public function get(): ModelCollection |
94
|
|
|
{ |
95
|
|
|
$models = array_map(function ($item) { |
96
|
|
|
return wrapToModel($item, static::class); |
97
|
|
|
}, $this->ormInstance->get()); |
98
|
|
|
|
99
|
|
|
return new ModelCollection($models); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param int $perPage |
104
|
|
|
* @param int $currentPage |
105
|
|
|
* @return Paginator |
106
|
|
|
*/ |
107
|
|
|
public function paginate(int $perPage, int $currentPage = 1): Paginator |
108
|
|
|
{ |
109
|
|
|
return new Paginator($this->ormInstance, static::class, $perPage, $currentPage); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Fills the object properties |
114
|
|
|
* @param array $props |
115
|
|
|
* @return QtModel |
116
|
|
|
* @throws ModelException |
117
|
|
|
*/ |
118
|
|
|
public function fillObjectProps(array $props): QtModel |
119
|
|
|
{ |
120
|
|
|
foreach ($props as $key => $value) { |
121
|
|
|
if ($key == $this->idColumn) { |
122
|
|
|
continue; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
if (!in_array($key, $this->fillable)) { |
126
|
|
|
throw ModelException::inappropriateProperty($key); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$this->prop($key, $value); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return bool |
137
|
|
|
*/ |
138
|
|
|
public function isEmpty() : bool |
139
|
|
|
{ |
140
|
|
|
return empty($this->asArray()); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Sets or gets the model property |
146
|
|
|
* @param string $property |
147
|
|
|
* @param mixed|null $value |
148
|
|
|
* @return mixed |
149
|
|
|
*/ |
150
|
|
|
public function prop(string $property, $value = null) |
151
|
|
|
{ |
152
|
|
|
return $this->ormInstance->prop(...func_get_args()); |
|
|
|
|
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Gets the model property with magic |
157
|
|
|
* @param string $property |
158
|
|
|
* @return mixed |
159
|
|
|
*/ |
160
|
|
|
public function __get(string $property) |
161
|
|
|
{ |
162
|
|
|
return $this->prop($property); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Sets a value to the model property with magic |
167
|
|
|
* @param string $property |
168
|
|
|
* @param mixed $value |
169
|
|
|
*/ |
170
|
|
|
public function __set(string $property, $value) |
171
|
|
|
{ |
172
|
|
|
$this->prop($property, $value); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param string $method |
177
|
|
|
* @param mixed|null $args |
178
|
|
|
* @return $this|array|int|string |
179
|
|
|
* @throws ModelException |
180
|
|
|
*/ |
181
|
|
|
public function __call(string $method, $args = null) |
182
|
|
|
{ |
183
|
|
|
if (!method_exists($this->ormInstance, $method)) { |
184
|
|
|
throw ModelException::undefinedMethod($method); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
$result = $this->ormInstance->{$method}(...$args); |
188
|
|
|
|
189
|
|
|
if (!$result instanceof DbalInterface) { |
190
|
|
|
return $result; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
return $this; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Keeps only relevant props at serialization |
198
|
|
|
* @return string[] |
199
|
|
|
*/ |
200
|
|
|
public function __sleep() |
201
|
|
|
{ |
202
|
|
|
return [ |
203
|
|
|
'table', |
204
|
|
|
'idColumn', |
205
|
|
|
'foreignKeys', |
206
|
|
|
'hidden' |
207
|
|
|
]; |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|