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