1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Erykai\Database; |
4
|
|
|
|
5
|
|
|
use PDO; |
6
|
|
|
use stdClass; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* CLASS CRUD |
10
|
|
|
*/ |
11
|
|
|
class Database extends Resource |
|
|
|
|
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var string|null |
15
|
|
|
*/ |
16
|
|
|
private null|string $returnColumns = null; |
17
|
|
|
/** |
18
|
|
|
* @param $name |
19
|
|
|
* @param $value |
20
|
|
|
*/ |
21
|
|
|
public function __set($name, $value) |
22
|
|
|
{ |
23
|
|
|
if (empty($this->data)) { |
24
|
|
|
$this->data = new stdClass(); |
|
|
|
|
25
|
|
|
} |
26
|
|
|
$this->data->$name = $value; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param $name |
31
|
|
|
* @return mixed |
32
|
|
|
*/ |
33
|
|
|
public function __get($name) |
34
|
|
|
{ |
35
|
|
|
return $this->data->$name; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param $name |
40
|
|
|
* @return bool |
41
|
|
|
*/ |
42
|
|
|
public function __isset($name) |
43
|
|
|
{ |
44
|
|
|
return isset($this->data->$name); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string|null $columns |
49
|
|
|
*/ |
50
|
|
|
public function columns(?string $columns = null): void |
51
|
|
|
{ |
52
|
|
|
if($columns){ |
53
|
|
|
$this->returnColumns = $columns; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $columns |
59
|
|
|
* @param string|null $condition |
60
|
|
|
* @param array $params |
61
|
|
|
* @return $this |
62
|
|
|
*/ |
63
|
|
|
public function find(string $columns = '*', string $condition = null, array $params = null): static |
64
|
|
|
{ |
65
|
|
|
if($this->returnColumns){ |
66
|
|
|
$columns = $this->returnColumns . $columns; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$this->query = "SELECT $columns FROM $this->table"; |
|
|
|
|
70
|
|
|
$this->params = $params; |
|
|
|
|
71
|
|
|
if ($condition) { |
72
|
|
|
$this->query .= " WHERE $condition "; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $inner |
80
|
|
|
* @return $this |
81
|
|
|
*/ |
82
|
|
|
public function inner(string $inner): static |
83
|
|
|
{ |
84
|
|
|
if (str_contains($this->query, 'WHERE')) { |
85
|
|
|
$query = explode("WHERE", $this->query); |
86
|
|
|
$this->query = $query[0]; |
|
|
|
|
87
|
|
|
} |
88
|
|
|
$this->query .= " $inner "; |
89
|
|
|
if (isset($query[1])){ |
90
|
|
|
$this->query .= "WHERE $query[1] "; |
91
|
|
|
} |
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $order |
97
|
|
|
* @return $this |
98
|
|
|
*/ |
99
|
|
|
public function order(string $order = null): static |
100
|
|
|
{ |
101
|
|
|
if($order){ |
102
|
|
|
$this->queryEnd .= " ORDER BY $order "; |
|
|
|
|
103
|
|
|
} |
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string $column |
109
|
|
|
* @return $this |
110
|
|
|
*/ |
111
|
|
|
public function group(string $column): static |
112
|
|
|
{ |
113
|
|
|
$this->queryEnd .= " GROUP BY $column "; |
|
|
|
|
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param int $limit |
119
|
|
|
* @return $this |
120
|
|
|
*/ |
121
|
|
|
public function limit(int $limit): static |
122
|
|
|
{ |
123
|
|
|
$this->queryEnd .= " LIMIT $limit "; |
|
|
|
|
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param int $offset |
129
|
|
|
* @return $this |
130
|
|
|
*/ |
131
|
|
|
public function offset(int $offset): static |
132
|
|
|
{ |
133
|
|
|
$this->queryEnd .= " OFFSET $offset "; |
|
|
|
|
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param bool $all |
139
|
|
|
* @return object|null |
140
|
|
|
*/ |
141
|
|
|
public function fetch(bool $all = false): ?object |
142
|
|
|
{ |
143
|
|
|
if($this->queryEnd){ |
|
|
|
|
144
|
|
|
$this->query = $this->query . $this->queryEnd; |
|
|
|
|
145
|
|
|
} |
146
|
|
|
$this->data = null; |
|
|
|
|
147
|
|
|
$this->stmt = $this->conn->prepare($this->query); |
|
|
|
|
148
|
|
|
if(!$this->params){ |
149
|
|
|
$this->params = []; |
|
|
|
|
150
|
|
|
} |
151
|
|
|
$this->bind($this->params); |
152
|
|
|
$this->stmt->execute(); |
153
|
|
|
|
154
|
|
|
if ($all) { |
155
|
|
|
if ($this->stmt->rowCount()) { |
156
|
|
|
$this->data = (object)$this->stmt->fetchAll(); |
157
|
|
|
} else { |
158
|
|
|
$this->setResponse(404, "error", "no results found"); |
159
|
|
|
} |
160
|
|
|
} else if ($this->stmt->rowCount()) { |
161
|
|
|
$this->data = (object)$this->stmt->fetch(); |
162
|
|
|
} else { |
163
|
|
|
$this->setResponse(404, "error", "no results found"); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return $this->data; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param int $id |
171
|
|
|
* @return bool |
172
|
|
|
*/ |
173
|
|
|
public function delete(int $id): bool |
174
|
|
|
{ |
175
|
|
|
$dynamic = $this->nameId . " " . $id; |
|
|
|
|
176
|
|
|
|
177
|
|
|
$stmt = $this->conn->prepare("DELETE FROM $this->table WHERE $this->nameId = :$this->nameId"); |
|
|
|
|
178
|
|
|
$stmt->bindParam(":$this->nameId", $id, PDO::PARAM_INT); |
179
|
|
|
|
180
|
|
|
if ($stmt->execute()) { |
181
|
|
|
$this->setResponse(200, "success", "$dynamic was successfully removed", dynamic: $dynamic); |
182
|
|
|
return true; |
183
|
|
|
} |
184
|
|
|
$this->setResponse(400, "error", "failed to delete $dynamic", dynamic: $dynamic); |
185
|
|
|
return false; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param string $columns |
190
|
|
|
* @return object|null |
191
|
|
|
*/ |
192
|
|
|
public function data(string $columns = "*"): ?object |
193
|
|
|
{ |
194
|
|
|
if (!empty($this->data->{0})) { |
195
|
|
|
return $this->data; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
$id = $this->nameId; |
|
|
|
|
199
|
|
|
if (!isset($this->data->$id)) { |
200
|
|
|
$this->find($columns, "$id=:$id", [$id => $this->conn->lastInsertId()])->fetch(); |
|
|
|
|
201
|
|
|
return $this->data; |
202
|
|
|
} |
203
|
|
|
$this->find($columns, "$id=:$id", [$id => $this->data->$id])->fetch(); |
204
|
|
|
return $this->data; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
public function count() |
208
|
|
|
{ |
209
|
|
|
$this->data = null; |
|
|
|
|
210
|
|
|
$this->stmt = $this->conn->prepare($this->query); |
|
|
|
|
211
|
|
|
if(!$this->params){ |
212
|
|
|
$this->params = []; |
|
|
|
|
213
|
|
|
} |
214
|
|
|
$this->bind($this->params); |
215
|
|
|
$this->stmt->execute(); |
216
|
|
|
return $this->stmt->rowCount(); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @return bool |
221
|
|
|
*/ |
222
|
|
|
public function save(): bool |
223
|
|
|
{ |
224
|
|
|
$id = $this->nameId; |
|
|
|
|
225
|
|
|
if (!isset($this->data->$id)) { |
226
|
|
|
if ($this->create()) { |
227
|
|
|
$this->stmt->execute(); |
228
|
|
|
$this->setResponse(200, "success", "registration successful", $this->data); |
229
|
|
|
return true; |
230
|
|
|
} |
231
|
|
|
return false; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
if ($this->update()) { |
235
|
|
|
$this->stmt->execute(); |
236
|
|
|
$this->setResponse(200, "success", "updated successful", $this->data); |
237
|
|
|
return true; |
238
|
|
|
} |
239
|
|
|
return false; |
240
|
|
|
} |
241
|
|
|
/** |
242
|
|
|
* @return object |
243
|
|
|
*/ |
244
|
|
|
public function response(): object |
245
|
|
|
{ |
246
|
|
|
return $this->getResponse(); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
} |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths