1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SimplePHP\Model; |
4
|
|
|
|
5
|
|
|
use SimplePHP\Root\Connection; |
6
|
|
|
use SimplePHP\Root\Functions; |
7
|
|
|
use PDO; |
8
|
|
|
use PDOException; |
9
|
|
|
use Exception; |
10
|
|
|
use Error; |
11
|
|
|
use stdClass; |
12
|
|
|
use SimplePHP\Model\CRUD as Actions; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class SimplePHP |
16
|
|
|
* @package NicollasSilva\SimplePHP |
17
|
|
|
*/ |
18
|
|
|
class SimplePHP extends Connection { |
19
|
|
|
use Actions, Functions; |
20
|
|
|
|
21
|
|
|
/** @var string */ |
22
|
|
|
protected $sentence = ''; |
23
|
|
|
|
24
|
|
|
/** @var string */ |
25
|
|
|
protected $offset; |
26
|
|
|
|
27
|
|
|
/** @var string */ |
28
|
|
|
protected $order; |
29
|
|
|
|
30
|
|
|
/** @var string */ |
31
|
|
|
protected $params = '*'; |
32
|
|
|
|
33
|
|
|
/** @var string */ |
34
|
|
|
protected $where; |
35
|
|
|
|
36
|
|
|
/** @var string */ |
37
|
|
|
protected $limit; |
38
|
|
|
|
39
|
|
|
/** @var string */ |
40
|
|
|
protected $table; |
41
|
|
|
|
42
|
|
|
/** @var object|null */ |
43
|
|
|
protected $data; |
44
|
|
|
|
45
|
|
|
/** @var bool */ |
46
|
|
|
protected $type; |
47
|
|
|
|
48
|
|
|
/** @var string */ |
49
|
|
|
protected $primary; |
50
|
|
|
|
51
|
|
|
/** @var array */ |
52
|
|
|
protected $request = []; |
53
|
|
|
|
54
|
|
|
/** @var array */ |
55
|
|
|
protected $excepts = []; |
56
|
|
|
|
57
|
|
|
/** @var bool */ |
58
|
|
|
protected $count = false; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get tablename of children model |
62
|
|
|
* @param string|null $tableName |
63
|
|
|
*/ |
64
|
|
|
function __construct(String $tableName, String $primaryKey) |
65
|
|
|
{ |
66
|
|
|
parent::__construct(); $this->table = $tableName; $this->primary = $primaryKey; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param int|null $id |
71
|
|
|
* @return SimplePHP|null |
72
|
|
|
*/ |
73
|
|
|
public function find(int $id = null): ?SimplePHP |
74
|
|
|
{ |
75
|
|
|
is_int($id) ? $this->where([['id', '=', $id]]) : null; |
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param array $where |
81
|
|
|
* @param string $condition = 'AND' |
82
|
|
|
* @return SimplePHP|null |
83
|
|
|
*/ |
84
|
|
|
public function where(Array $where, String $condition = 'AND'): ?SimplePHP |
85
|
|
|
{ |
86
|
|
|
foreach($where as $enclosures) { |
87
|
|
|
$this->where .= $enclosures[0]." ".$enclosures[1]." '".$enclosures[2]."' {$condition} "; |
88
|
|
|
} |
89
|
|
|
$this->where = "WHERE " . rtrim($this->where, " {$condition} "); |
90
|
|
|
|
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param array $params |
96
|
|
|
* @return SimplePHP|null |
97
|
|
|
*/ |
98
|
|
|
public function only(Array $params): ?SimplePHP |
99
|
|
|
{ |
100
|
|
|
$this->params = implode($params, ','); |
|
|
|
|
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param int $limit |
106
|
|
|
* @return SimplePHP|null |
107
|
|
|
*/ |
108
|
|
|
public function limit(int $limit): ?SimplePHP |
109
|
|
|
{ |
110
|
|
|
$this->limit = "LIMIT $limit"; |
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param int $offset |
116
|
|
|
* @return SimplePHP|null |
117
|
|
|
*/ |
118
|
|
|
public function offset(int $offset): ?SimplePHP |
119
|
|
|
{ |
120
|
|
|
$this->offset = "OFFSET $offset"; |
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param string $order |
126
|
|
|
* @param string $ordenation = 'ASC' |
127
|
|
|
* @return SimplePHP|null |
128
|
|
|
*/ |
129
|
|
|
public function orderBy(String $prop, String $ordenation = 'ASC'): ?SimplePHP |
130
|
|
|
{ |
131
|
|
|
if(mb_strlen($this->order) < 9) { |
132
|
|
|
$this->order = "ORDER BY {$prop} {$ordenation}"; |
133
|
|
|
} else { |
134
|
|
|
$this->order .= ", {$prop} {$ordenation}"; |
135
|
|
|
} |
136
|
|
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param string $name |
141
|
|
|
* @param mixed $arguments |
142
|
|
|
* @return null |
143
|
|
|
*/ |
144
|
|
|
public function __call(String $name, $arguments) |
145
|
|
|
{ |
146
|
|
|
return $this->writeLog("This method does not exist at the SimplePHP: \"<b>{$name}</b>\"."); |
|
|
|
|
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param array $deniable |
151
|
|
|
* @return SimplePHP|null |
152
|
|
|
*/ |
153
|
|
|
public function except(Array $deniable) |
154
|
|
|
{ |
155
|
|
|
$this->excepts = $deniable; |
156
|
|
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @return null |
161
|
|
|
*/ |
162
|
|
|
private function deny() |
163
|
|
|
{ |
164
|
|
|
if (!empty($this->excepts)) { |
165
|
|
|
foreach ($this->excepts as $except) { |
166
|
|
|
if (isset($this->data[$except])) unset($this->data[$except]); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param $prop |
173
|
|
|
* @param $value |
174
|
|
|
* @return null |
175
|
|
|
*/ |
176
|
|
|
public function __set($prop, $value) |
177
|
|
|
{ |
178
|
|
|
if (empty($this->data)) { |
179
|
|
|
$this->data = new stdClass(); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
$this->data->$prop = $value; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param $attribute |
187
|
|
|
* @return bool |
188
|
|
|
*/ |
189
|
|
|
public function __isset($attribute): bool |
190
|
|
|
{ |
191
|
|
|
return isset($this->data->$attribute); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param $prop |
196
|
|
|
* @return string|null |
197
|
|
|
*/ |
198
|
|
|
public function __get($prop) |
199
|
|
|
{ |
200
|
|
|
return $this->data->$prop ?? null; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
public function count(): SimplePHP |
204
|
|
|
{ |
205
|
|
|
$this->count = true; |
206
|
|
|
return $this; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @return array|object|int|null |
211
|
|
|
*/ |
212
|
|
|
public function execute(bool $type = false) |
213
|
|
|
{ |
214
|
|
|
$this->type = $type; |
215
|
|
|
try { |
216
|
|
|
$execute = $this->conn->query("SELECT {$this->params} FROM {$this->table} {$this->where} {$this->order} {$this->limit} {$this->offset}"); |
|
|
|
|
217
|
|
|
$execute->rowCount() > 1 ? |
218
|
|
|
$this->data = ($this->type ? $execute->fetchAll(PDO::FETCH_CLASS, static::class) : $execute->fetchAll(PDO::FETCH_ASSOC)) : $this->data = ($this->type ? $execute->fetchObject(static::class) : $execute->fetch(PDO::FETCH_ASSOC)); |
219
|
|
|
$this->deny(); |
220
|
|
|
return !$this->count ? $this->data : $execute->rowCount(); |
221
|
|
|
} catch (PDOException $exc) { |
222
|
|
|
return $this->writeLog($exc->getCode(), true); |
|
|
|
|
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @return null|bool |
228
|
|
|
*/ |
229
|
|
|
public function destroy() |
230
|
|
|
{ |
231
|
|
|
$primary = $this->primary; |
232
|
|
|
if (!isset($this->data->$primary)) { |
233
|
|
|
return $this->writeLog("The primary index was not found."); |
|
|
|
|
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
return $this->delete($this->data->$primary); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @return null|bool |
241
|
|
|
*/ |
242
|
|
|
public function save() |
243
|
|
|
{ |
244
|
|
|
$primary = $this->primary; |
245
|
|
|
$data = json_decode(json_encode($this->data), true); |
246
|
|
|
if (empty($primary) || !isset($data[$primary])) { |
247
|
|
|
return $this->writeLog("The primary index was not found."); |
|
|
|
|
248
|
|
|
} else if (!$this->find($data[$primary])->execute()) { |
249
|
|
|
return $this->writeLog("The primary index was not found in the database."); |
|
|
|
|
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
$otherPrimary = $data[$primary]; |
253
|
|
|
unset($data[$primary]); |
254
|
|
|
$parameters = implode(',', array_keys($data)); |
255
|
|
|
$values = array_values($data); |
256
|
|
|
|
257
|
|
|
return $this->update($parameters, $values, $otherPrimary); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* @return SimplePHP|null |
262
|
|
|
*/ |
263
|
|
|
public function request(Array $request): ?SimplePHP |
264
|
|
|
{ |
265
|
|
|
$this->request = $request; |
266
|
|
|
return $this; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* @return null|bool |
271
|
|
|
*/ |
272
|
|
|
public function create() |
273
|
|
|
{ |
274
|
|
|
$request = $this->request; |
275
|
|
|
if (empty($request)) { |
276
|
|
|
return $this->writeLog("No information was passed to record."); |
|
|
|
|
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
$parameters = implode(',', array_keys($request)); |
280
|
|
|
$values = array_values($request); |
281
|
|
|
|
282
|
|
|
return $this->insert($parameters, $values); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* @param string $message |
287
|
|
|
* @return null |
288
|
|
|
*/ |
289
|
|
|
protected function writeLog($message, $pdo = false) |
290
|
|
|
{ |
291
|
|
|
$message = $pdo ? "Error: PDOCode " . $message : $message; |
292
|
|
|
$archive = fopen(dirname(__DIR__) . DIRECTORY_SEPARATOR . "Logs" . DIRECTORY_SEPARATOR . "Logs.txt", 'a+'); |
293
|
|
|
fwrite($archive, "-----SimplePHPLog-----\n" . date("d/m/Y H:i:s", time()) . " -> ". $message ."\n-------\n"); |
|
|
|
|
294
|
|
|
fclose($archive); |
|
|
|
|
295
|
|
|
return null; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* @param string $table |
300
|
|
|
* @return SimplePHP|null |
301
|
|
|
*/ |
302
|
|
|
public function useTable(String $table): ?SimplePHP |
303
|
|
|
{ |
304
|
|
|
$this->table = $table; |
305
|
|
|
return $this; |
306
|
|
|
} |
307
|
|
|
} |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.