1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CoffeeCode\DataLayer; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use PDO; |
7
|
|
|
use PDOException; |
8
|
|
|
use stdClass; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class DataLayer |
12
|
|
|
* @package CoffeeCode\DataLayer |
13
|
|
|
*/ |
14
|
|
|
abstract class DataLayer |
15
|
|
|
{ |
16
|
|
|
use CrudTrait; |
17
|
|
|
|
18
|
|
|
/** @var string $entity database table */ |
19
|
|
|
private $entity; |
20
|
|
|
|
21
|
|
|
/** @var string $primary table primary key field */ |
22
|
|
|
private $primary; |
23
|
|
|
|
24
|
|
|
/** @var array $required table required fields */ |
25
|
|
|
private $required; |
26
|
|
|
|
27
|
|
|
/** @var string $timestamps control created and updated at */ |
28
|
|
|
private $timestamps; |
29
|
|
|
|
30
|
|
|
/** @var string */ |
31
|
|
|
protected $statement; |
32
|
|
|
|
33
|
|
|
/** @var string */ |
34
|
|
|
protected $params; |
35
|
|
|
|
36
|
|
|
/** @var string */ |
37
|
|
|
protected $group; |
38
|
|
|
|
39
|
|
|
/** @var string */ |
40
|
|
|
protected $order; |
41
|
|
|
|
42
|
|
|
/** @var int */ |
43
|
|
|
protected $limit; |
44
|
|
|
|
45
|
|
|
/** @var int */ |
46
|
|
|
protected $offset; |
47
|
|
|
|
48
|
|
|
/** @var \PDOException|null */ |
49
|
|
|
protected $fail; |
50
|
|
|
|
51
|
|
|
/** @var object|null */ |
52
|
|
|
protected $data; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* DataLayer constructor. |
56
|
|
|
* @param string $entity |
57
|
|
|
* @param array $required |
58
|
|
|
* @param string $primary |
59
|
|
|
* @param bool $timestamps |
60
|
|
|
*/ |
61
|
|
|
public function __construct(string $entity, array $required, string $primary = 'id', bool $timestamps = true) |
62
|
|
|
{ |
63
|
|
|
$this->entity = $entity; |
64
|
|
|
$this->primary = $primary; |
65
|
|
|
$this->required = $required; |
66
|
|
|
$this->timestamps = $timestamps; |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param $name |
71
|
|
|
* @param $value |
72
|
|
|
*/ |
73
|
|
|
public function __set($name, $value) |
74
|
|
|
{ |
75
|
|
|
if (empty($this->data)) { |
76
|
|
|
$this->data = new stdClass(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$this->data->$name = $value; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param $name |
84
|
|
|
* @return bool |
85
|
|
|
*/ |
86
|
|
|
public function __isset($name) |
87
|
|
|
{ |
88
|
|
|
return isset($this->data->$name); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param $name |
93
|
|
|
* @return string|null |
94
|
|
|
*/ |
95
|
|
|
public function __get($name) |
96
|
|
|
{ |
97
|
|
|
return ($this->data->$name ?? null); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return object|null |
102
|
|
|
*/ |
103
|
|
|
public function data(): ?object |
104
|
|
|
{ |
105
|
|
|
return $this->data; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return PDOException|Exception|null |
110
|
|
|
*/ |
111
|
|
|
public function fail() |
112
|
|
|
{ |
113
|
|
|
return $this->fail; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param string|null $terms |
118
|
|
|
* @param string|null $params |
119
|
|
|
* @param string $columns |
120
|
|
|
* @return DataLayer |
121
|
|
|
*/ |
122
|
|
|
public function find(?string $terms = null, ?string $params = null, string $columns = "*"): DataLayer |
123
|
|
|
{ |
124
|
|
|
if ($terms) { |
125
|
|
|
$this->statement = "SELECT {$columns} FROM {$this->entity} WHERE {$terms}"; |
126
|
|
|
parse_str($params, $this->params); |
|
|
|
|
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$this->statement = "SELECT {$columns} FROM {$this->entity}"; |
131
|
|
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param int $id |
136
|
|
|
* @param string $columns |
137
|
|
|
* @return DataLayer|null |
138
|
|
|
*/ |
139
|
|
|
public function findById(int $id, string $columns = "*"): ?DataLayer |
140
|
|
|
{ |
141
|
|
|
$find = $this->find($this->primary . " = :id", "id={$id}", $columns); |
142
|
|
|
return $find->fetch(); |
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param string $column |
147
|
|
|
* @return DataLayer|null |
148
|
|
|
*/ |
149
|
|
|
public function group(string $column): ?DataLayer |
150
|
|
|
{ |
151
|
|
|
$this->group = " GROUP BY {$column}"; |
152
|
|
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param string $columnOrder |
157
|
|
|
* @return DataLayer|null |
158
|
|
|
*/ |
159
|
|
|
public function order(string $columnOrder): ?DataLayer |
160
|
|
|
{ |
161
|
|
|
$this->order = " ORDER BY {$columnOrder}"; |
162
|
|
|
return $this; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param int $limit |
167
|
|
|
* @return DataLayer|null |
168
|
|
|
*/ |
169
|
|
|
public function limit(int $limit): ?DataLayer |
170
|
|
|
{ |
171
|
|
|
$this->limit = " LIMIT {$limit}"; |
|
|
|
|
172
|
|
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param int $offset |
177
|
|
|
* @return DataLayer|null |
178
|
|
|
*/ |
179
|
|
|
public function offset(int $offset): ?DataLayer |
180
|
|
|
{ |
181
|
|
|
$this->offset = " OFFSET {$offset}"; |
|
|
|
|
182
|
|
|
return $this; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param bool $all |
187
|
|
|
* @return array|mixed|null |
188
|
|
|
*/ |
189
|
|
|
public function fetch(bool $all = false) |
190
|
|
|
{ |
191
|
|
|
try { |
192
|
|
|
$stmt = Connect::getInstance()->prepare($this->statement . $this->group . $this->order . $this->limit . $this->offset); |
193
|
|
|
$stmt->execute($this->params); |
|
|
|
|
194
|
|
|
|
195
|
|
|
if (!$stmt->rowCount()) { |
196
|
|
|
return null; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
if ($all) { |
200
|
|
|
return $stmt->fetchAll(PDO::FETCH_CLASS, static::class); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
return $stmt->fetchObject(static::class); |
204
|
|
|
} catch (PDOException $exception) { |
205
|
|
|
$this->fail = $exception; |
206
|
|
|
return null; |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @return int |
212
|
|
|
*/ |
213
|
|
|
public function count(): int |
214
|
|
|
{ |
215
|
|
|
$stmt = Connect::getInstance()->prepare($this->statement); |
216
|
|
|
$stmt->execute($this->params); |
|
|
|
|
217
|
|
|
return $stmt->rowCount(); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @return bool |
222
|
|
|
*/ |
223
|
|
|
public function save(): bool |
224
|
|
|
{ |
225
|
|
|
$primary = $this->primary; |
226
|
|
|
$id = null; |
227
|
|
|
|
228
|
|
|
try { |
229
|
|
|
if (!$this->required()) { |
230
|
|
|
throw new Exception("Preencha os campos necessários"); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** Update */ |
234
|
|
|
if (!empty($this->data->$primary)) { |
235
|
|
|
$id = $this->data->$primary; |
236
|
|
|
$this->update($this->safe(), $this->primary . " = :id", "id={$id}"); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** Create */ |
240
|
|
|
if (empty($this->data->$primary)) { |
241
|
|
|
$id = $this->create($this->safe()); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
if (!$id) { |
245
|
|
|
return false; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
$this->data = $this->findById($id)->data(); |
249
|
|
|
return true; |
250
|
|
|
} catch (Exception $exception) { |
251
|
|
|
$this->fail = $exception; |
|
|
|
|
252
|
|
|
return false; |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* @return bool |
258
|
|
|
*/ |
259
|
|
|
public function destroy(): bool |
260
|
|
|
{ |
261
|
|
|
$primary = $this->primary; |
262
|
|
|
$id = $this->data->$primary; |
263
|
|
|
|
264
|
|
|
if (empty($id)) { |
265
|
|
|
return false; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
$destroy = $this->delete($this->primary . " = :id", "id={$id}"); |
269
|
|
|
return $destroy; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* @return bool |
274
|
|
|
*/ |
275
|
|
|
protected function required(): bool |
276
|
|
|
{ |
277
|
|
|
$data = (array)$this->data(); |
278
|
|
|
foreach ($this->required as $field) { |
279
|
|
|
if (empty($data[$field])) { |
280
|
|
|
return false; |
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
return true; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* @return array|null |
288
|
|
|
*/ |
289
|
|
|
protected function safe(): ?array |
290
|
|
|
{ |
291
|
|
|
$safe = (array)$this->data; |
292
|
|
|
unset($safe[$this->primary]); |
293
|
|
|
|
294
|
|
|
return $safe; |
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.