@@ -6,13 +6,13 @@ discard block |
||
6 | 6 | use PDOException; |
7 | 7 | use PDO; |
8 | 8 | |
9 | -trait CrudTrait{ |
|
9 | +trait CrudTrait { |
|
10 | 10 | |
11 | 11 | protected ?Exception $fail = null; |
12 | 12 | |
13 | 13 | protected function check_fail() |
14 | 14 | { |
15 | - if(!is_null($this->fail)){ |
|
15 | + if (!is_null($this->fail)) { |
|
16 | 16 | throw $this->fail; |
17 | 17 | } |
18 | 18 | } |
@@ -27,13 +27,13 @@ discard block |
||
27 | 27 | return false; |
28 | 28 | } |
29 | 29 | |
30 | - protected function select(string $query,array $data): ?array |
|
30 | + protected function select(string $query, array $data): ?array |
|
31 | 31 | { |
32 | - try{ |
|
32 | + try { |
|
33 | 33 | $stmt = Connect::getInstance()->prepare("{$query}"); |
34 | 34 | $stmt->execute($data); |
35 | 35 | return $stmt->fetchAll(PDO::FETCH_ASSOC); |
36 | - }catch(Exception $exception){ |
|
36 | + } catch (Exception $exception) { |
|
37 | 37 | $this->fail = $exception; |
38 | 38 | } |
39 | 39 | return []; |
@@ -41,11 +41,11 @@ discard block |
||
41 | 41 | |
42 | 42 | protected function describe(): ?array |
43 | 43 | { |
44 | - try{ |
|
44 | + try { |
|
45 | 45 | $stmt = Connect::getInstance()->prepare("DESCRIBE {$this->table}"); |
46 | 46 | $stmt->execute(); |
47 | 47 | return $stmt->fetchAll(PDO::FETCH_ASSOC); |
48 | - }catch(Exception $exception){ |
|
48 | + } catch (Exception $exception) { |
|
49 | 49 | $this->fail = $exception; |
50 | 50 | return null; |
51 | 51 | } |
@@ -55,7 +55,7 @@ discard block |
||
55 | 55 | { |
56 | 56 | try { |
57 | 57 | $columns = implode(", ", array_keys($data)); |
58 | - $values = ":" . implode(", :", array_keys($data)); |
|
58 | + $values = ":".implode(", :", array_keys($data)); |
|
59 | 59 | |
60 | 60 | $stmt = Connect::getInstance()->prepare("INSERT INTO {$this->table} ({$columns}) VALUES ({$values})"); |
61 | 61 | |
@@ -92,7 +92,7 @@ discard block |
||
92 | 92 | try { |
93 | 93 | $stmt = Connect::getInstance()->prepare("DELETE FROM {$this->table} WHERE {$terms}"); |
94 | 94 | |
95 | - if($params){ |
|
95 | + if ($params) { |
|
96 | 96 | parse_str($params, $params); |
97 | 97 | $stmt->execute($params); |
98 | 98 | return true; |
@@ -17,7 +17,7 @@ discard block |
||
17 | 17 | private ?string $clause = null; |
18 | 18 | |
19 | 19 | |
20 | - private array $where = [''=> ["1",'=',"1"] ]; |
|
20 | + private array $where = [''=> ["1", '=', "1"]]; |
|
21 | 21 | private ?string $order = null; |
22 | 22 | private ?string $limit = null; |
23 | 23 | private ?int $offset = null; |
@@ -53,8 +53,8 @@ discard block |
||
53 | 53 | case 'Type': |
54 | 54 | $type = $value; |
55 | 55 | |
56 | - if(strpos($value,'(')){ |
|
57 | - switch (substr($value,0,strpos($value,'('))) { |
|
56 | + if (strpos($value, '(')) { |
|
57 | + switch (substr($value, 0, strpos($value, '('))) { |
|
58 | 58 | case 'varchar': |
59 | 59 | case 'char': |
60 | 60 | case 'text': $type = 'string'; break; |
@@ -75,7 +75,7 @@ discard block |
||
75 | 75 | case 'string': |
76 | 76 | case 'float': |
77 | 77 | case 'int': |
78 | - $this->$field = ['maxlength' => substr($value,(strpos($value,'(')+1),-1) ]; |
|
78 | + $this->$field = ['maxlength' => substr($value, (strpos($value, '(') + 1), -1)]; |
|
79 | 79 | break; |
80 | 80 | case 'date': |
81 | 81 | $this->$field = ['maxlength' => 10]; |
@@ -115,16 +115,16 @@ discard block |
||
115 | 115 | return $this; |
116 | 116 | } |
117 | 117 | |
118 | - public function __set(string $prop,$value): Datamanager |
|
118 | + public function __set(string $prop, $value): Datamanager |
|
119 | 119 | { |
120 | 120 | |
121 | - if(is_array($value)){ |
|
121 | + if (is_array($value)) { |
|
122 | 122 | $attr = array_keys($value)[0]; |
123 | 123 | $this->data[$prop][$attr] = $value[$attr]; |
124 | 124 | return $this; |
125 | 125 | } |
126 | 126 | |
127 | - if($this->full && !array_key_exists($prop,$this->data)){ |
|
127 | + if ($this->full && !array_key_exists($prop, $this->data)) { |
|
128 | 128 | throw new Exception("{$prop} field does not exist in the table {$this->table}."); |
129 | 129 | } |
130 | 130 | |
@@ -141,7 +141,7 @@ discard block |
||
141 | 141 | |
142 | 142 | public function __get(string $field) |
143 | 143 | { |
144 | - if($this->full && !array_key_exists($field,$this->data)){ |
|
144 | + if ($this->full && !array_key_exists($field, $this->data)) { |
|
145 | 145 | throw new Exception("{$field} field does not exist in the table {$this->table}."); |
146 | 146 | } |
147 | 147 | |
@@ -158,7 +158,7 @@ discard block |
||
158 | 158 | $deniable = (is_array($deniable)) ? $deniable : [$deniable]; |
159 | 159 | |
160 | 160 | foreach ($deniable as $field) { |
161 | - if(!array_key_exists($field,$this->data)){ |
|
161 | + if (!array_key_exists($field, $this->data)) { |
|
162 | 162 | throw new Exception("{$field} field does not exist in the table {$this->table}."); |
163 | 163 | } |
164 | 164 | |
@@ -178,11 +178,11 @@ discard block |
||
178 | 178 | |
179 | 179 | public function orderBy(string $field, string $ord = 'ASC'): Datamanager |
180 | 180 | { |
181 | - if(!array_key_exists(str_replace(['asc','ASC','desc','DESC',' '],'',$field),$this->data) && $this->full){ |
|
181 | + if (!array_key_exists(str_replace(['asc', 'ASC', 'desc', 'DESC', ' '], '', $field), $this->data) && $this->full) { |
|
182 | 182 | throw new Exception("{$field} field does not exist in the table {$this->table}."); |
183 | 183 | } |
184 | 184 | |
185 | - if(strpos(strtolower($field),'asc') || strpos(strtolower($field),'desc')){ |
|
185 | + if (strpos(strtolower($field), 'asc') || strpos(strtolower($field), 'desc')) { |
|
186 | 186 | $ord = ''; |
187 | 187 | } |
188 | 188 | |
@@ -195,7 +195,7 @@ discard block |
||
195 | 195 | $params = (is_array($params)) ? $params : [$params]; |
196 | 196 | $this->select = []; |
197 | 197 | foreach ($params as $field) { |
198 | - if(!array_key_exists($field,$this->data) && $this->full){ |
|
198 | + if (!array_key_exists($field, $this->data) && $this->full) { |
|
199 | 199 | throw new Exception("{$field} field does not exist in the table {$this->table}."); |
200 | 200 | } |
201 | 201 | |
@@ -207,11 +207,11 @@ discard block |
||
207 | 207 | |
208 | 208 | public function where(array $where): Datamanager |
209 | 209 | { |
210 | - $this->where['AND'] = (array_key_exists('AND',$this->where)) ?? ''; |
|
210 | + $this->where['AND'] = (array_key_exists('AND', $this->where)) ?? ''; |
|
211 | 211 | $w = []; |
212 | 212 | foreach ($where as $condition => $values) { |
213 | 213 | |
214 | - if(!is_array($values)){ |
|
214 | + if (!is_array($values)) { |
|
215 | 215 | $w['AND'][] = $values; |
216 | 216 | continue; |
217 | 217 | } |
@@ -222,18 +222,18 @@ discard block |
||
222 | 222 | |
223 | 223 | } |
224 | 224 | |
225 | - $this->where = array_merge($this->where,$w); |
|
225 | + $this->where = array_merge($this->where, $w); |
|
226 | 226 | |
227 | 227 | return $this; |
228 | 228 | } |
229 | 229 | |
230 | 230 | public function check_where_array(array $where) |
231 | 231 | { |
232 | - if(count($where) != 3){ |
|
233 | - throw new Exception("Condition where set incorrectly: ".implode(' ',$where)); |
|
232 | + if (count($where) != 3) { |
|
233 | + throw new Exception("Condition where set incorrectly: ".implode(' ', $where)); |
|
234 | 234 | } |
235 | 235 | |
236 | - if(!array_key_exists($where[0],$this->data) && $this->full){ |
|
236 | + if (!array_key_exists($where[0], $this->data) && $this->full) { |
|
237 | 237 | throw new Exception("{$where[0]} field does not exist in the table {$this->table}."); |
238 | 238 | } |
239 | 239 | } |
@@ -246,7 +246,7 @@ discard block |
||
246 | 246 | |
247 | 247 | public function offset(int $offset): Datamanager |
248 | 248 | { |
249 | - if(is_null($this->limit)){ |
|
249 | + if (is_null($this->limit)) { |
|
250 | 250 | throw new Exception("The limit must be set before the offset."); |
251 | 251 | } |
252 | 252 | |
@@ -276,7 +276,7 @@ discard block |
||
276 | 276 | |
277 | 277 | foreach ($arrayValues as $key => $value) { |
278 | 278 | |
279 | - if(!array_key_exists($key,$this->data)){ |
|
279 | + if (!array_key_exists($key, $this->data)) { |
|
280 | 280 | throw new Exception("{$key} field does not exist in the table {$this->table}."); |
281 | 281 | } |
282 | 282 | |
@@ -290,7 +290,7 @@ discard block |
||
290 | 290 | $string = ''; |
291 | 291 | foreach ($this->data as $key => $value) { |
292 | 292 | |
293 | - if(gettype($value)==='object'){ |
|
293 | + if (gettype($value) === 'object') { |
|
294 | 294 | $value = $value->getData()[$this->primary]['value']; |
295 | 295 | } |
296 | 296 | |
@@ -301,32 +301,32 @@ discard block |
||
301 | 301 | |
302 | 302 | public function remove(?bool $exec = false): Datamanager |
303 | 303 | { |
304 | - if($exec){ |
|
304 | + if ($exec) { |
|
305 | 305 | $this->clause = null; |
306 | 306 | |
307 | - if(count($this->where) == 1){ |
|
307 | + if (count($this->where) == 1) { |
|
308 | 308 | $this->removeById(); |
309 | 309 | return $this; |
310 | 310 | } |
311 | 311 | |
312 | 312 | $where = ''; |
313 | 313 | $data = ''; |
314 | - foreach($this->where as $clause => $condition){ |
|
315 | - if(strlen($clause) === 0){ |
|
314 | + foreach ($this->where as $clause => $condition) { |
|
315 | + if (strlen($clause) === 0) { |
|
316 | 316 | $where .= " {$clause} "; |
317 | 317 | $where .= " {$condition[0]} {$condition[1]} :q_{$condition[0]} "; |
318 | 318 | $data .= "q_{$condition[0]}={$condition[2]}&"; |
319 | 319 | continue; |
320 | 320 | } |
321 | 321 | |
322 | - foreach($condition as $column => $value){ |
|
322 | + foreach ($condition as $column => $value) { |
|
323 | 323 | $where .= " {$clause} "; |
324 | 324 | $where .= " {$value[0]} {$value[1]} :q_{$value[0]} "; |
325 | 325 | $data .= "q_{$value[0]}={$value[2]}&"; |
326 | 326 | } |
327 | 327 | } |
328 | 328 | |
329 | - $this->delete($where, substr($data,0,-1) ); |
|
329 | + $this->delete($where, substr($data, 0, -1)); |
|
330 | 330 | |
331 | 331 | $this->check_fail(); |
332 | 332 | |
@@ -340,7 +340,7 @@ discard block |
||
340 | 340 | |
341 | 341 | private function removeById(): bool |
342 | 342 | { |
343 | - $delete = $this->delete("{$this->primary}=:{$this->primary}","{$this->primary}={$this->getData()[$this->primary]['value']}"); |
|
343 | + $delete = $this->delete("{$this->primary}=:{$this->primary}", "{$this->primary}={$this->getData()[$this->primary]['value']}"); |
|
344 | 344 | |
345 | 345 | $this->check_fail(); |
346 | 346 | |
@@ -351,11 +351,11 @@ discard block |
||
351 | 351 | { |
352 | 352 | $data = []; |
353 | 353 | foreach ($this->data as $key => $value) { |
354 | - if($this->data[$key]['key'] === 'PRI' || strstr($this->data[$key]['extra'],'auto_increment')){ |
|
354 | + if ($this->data[$key]['key'] === 'PRI' || strstr($this->data[$key]['extra'], 'auto_increment')) { |
|
355 | 355 | continue; |
356 | 356 | } |
357 | 357 | |
358 | - if($this->data[$key]['changed'] && $this->data[$key]['upgradeable']){ |
|
358 | + if ($this->data[$key]['changed'] && $this->data[$key]['upgradeable']) { |
|
359 | 359 | $data[$key] = $this->data[$key]['value']; |
360 | 360 | } |
361 | 361 | } |
@@ -364,13 +364,13 @@ discard block |
||
364 | 364 | $params = $this->primary.'='.$this->getData()[$this->primary]['value']; |
365 | 365 | |
366 | 366 | $this->transaction('begin'); |
367 | - try{ |
|
367 | + try { |
|
368 | 368 | $this->update($data, $terms, $params); |
369 | 369 | |
370 | 370 | $this->check_fail(); |
371 | 371 | |
372 | 372 | $this->transaction('commit'); |
373 | - }catch(Exception $er){ |
|
373 | + } catch (Exception $er) { |
|
374 | 374 | $this->transaction('rollback'); |
375 | 375 | throw $er; |
376 | 376 | } |
@@ -385,11 +385,11 @@ discard block |
||
385 | 385 | $data = []; |
386 | 386 | |
387 | 387 | foreach ($this->data as $key => $value) { |
388 | - if(strstr($this->data[$key]['extra'],'auto_increment')){ |
|
388 | + if (strstr($this->data[$key]['extra'], 'auto_increment')) { |
|
389 | 389 | continue; |
390 | 390 | } |
391 | 391 | |
392 | - if(strlen($value['value']) > $value['maxlength']){ |
|
392 | + if (strlen($value['value']) > $value['maxlength']) { |
|
393 | 393 | throw new Exception("The information provided for column {$key} of table {$this->table} exceeded that allowed."); |
394 | 394 | } |
395 | 395 | |
@@ -399,7 +399,7 @@ discard block |
||
399 | 399 | } |
400 | 400 | |
401 | 401 | $this->transaction('begin'); |
402 | - try{ |
|
402 | + try { |
|
403 | 403 | |
404 | 404 | $id = $this->insert($data); |
405 | 405 | |
@@ -409,7 +409,7 @@ discard block |
||
409 | 409 | |
410 | 410 | $this->transaction('commit'); |
411 | 411 | |
412 | - }catch(Exception $er){ |
|
412 | + } catch (Exception $er) { |
|
413 | 413 | $this->transaction('rollback'); |
414 | 414 | throw $er; |
415 | 415 | } |
@@ -421,13 +421,13 @@ discard block |
||
421 | 421 | { |
422 | 422 | $entity = null; |
423 | 423 | |
424 | - if($this->getCount() === 0){ |
|
424 | + if ($this->getCount() === 0) { |
|
425 | 425 | return null; |
426 | 426 | } |
427 | 427 | |
428 | 428 | $entity = $this->setByDatabase($this->result[0]); |
429 | 429 | |
430 | - if(count($this->result) > 1){ |
|
430 | + if (count($this->result) > 1) { |
|
431 | 431 | $entity = []; |
432 | 432 | foreach ($this->result as $key => $value) { |
433 | 433 | $entity[] = $this->setByDatabase($value); |
@@ -439,14 +439,14 @@ discard block |
||
439 | 439 | |
440 | 440 | public function findById($id): Datamanager |
441 | 441 | { |
442 | - $this->where([$this->primary,'=',$id]); |
|
442 | + $this->where([$this->primary, '=', $id]); |
|
443 | 443 | return $this; |
444 | 444 | } |
445 | 445 | |
446 | 446 | public function execute(): Datamanager |
447 | 447 | { |
448 | - if(!is_null($this->clause)){ |
|
449 | - if($this->clause == 'remove'){ |
|
448 | + if (!is_null($this->clause)) { |
|
449 | + if ($this->clause == 'remove') { |
|
450 | 450 | return $this->remove(true); |
451 | 451 | } |
452 | 452 | } |
@@ -458,8 +458,8 @@ discard block |
||
458 | 458 | $select .= "{$key},"; |
459 | 459 | } |
460 | 460 | |
461 | - $select = substr($select,0,-1); |
|
462 | - $this->query = str_replace("*",$select,$this->query); |
|
461 | + $select = substr($select, 0, -1); |
|
462 | + $this->query = str_replace("*", $select, $this->query); |
|
463 | 463 | |
464 | 464 | $where = ''; |
465 | 465 | $whereData = []; |
@@ -467,31 +467,31 @@ discard block |
||
467 | 467 | $key = (!$key) ? '' : " {$key} "; |
468 | 468 | |
469 | 469 | |
470 | - if(is_array($value[0])){ |
|
470 | + if (is_array($value[0])) { |
|
471 | 471 | foreach ($value as $k => $v) { |
472 | 472 | $where .= " {$key} {$v[0]} {$v[1]} :q_{$v[0]} "; |
473 | 473 | $whereData["q_{$v[0]}"] = $v[2]; |
474 | 474 | } |
475 | - }else{ |
|
475 | + } else { |
|
476 | 476 | $where .= " {$key} {$value[0]} {$value[1]} :q_{$value[0]} "; |
477 | 477 | $whereData["q_{$value[0]}"] = $value[2]; |
478 | 478 | } |
479 | 479 | |
480 | 480 | } |
481 | - $where = substr($where,0,-1); |
|
481 | + $where = substr($where, 0, -1); |
|
482 | 482 | $this->query .= " WHERE {$where} "; |
483 | 483 | |
484 | 484 | $this->query .= $this->order; |
485 | 485 | |
486 | - if(!is_null($this->limit)){ |
|
486 | + if (!is_null($this->limit)) { |
|
487 | 487 | $this->query .= " LIMIT {$this->limit}"; |
488 | 488 | } |
489 | 489 | |
490 | - if(!is_null($this->offset)){ |
|
490 | + if (!is_null($this->offset)) { |
|
491 | 491 | $this->query .= " OFFSET {$this->offset}"; |
492 | 492 | } |
493 | 493 | |
494 | - $this->result = $this->select($this->query,$whereData); |
|
494 | + $this->result = $this->select($this->query, $whereData); |
|
495 | 495 | |
496 | 496 | $this->check_fail(); |
497 | 497 | |
@@ -505,7 +505,7 @@ discard block |
||
505 | 505 | { |
506 | 506 | $this->query = " SELECT * FROM {$this->table} "; |
507 | 507 | |
508 | - if(is_int($key)){ |
|
508 | + if (is_int($key)) { |
|
509 | 509 | return $this->findById($key); |
510 | 510 | } |
511 | 511 |
@@ -370,7 +370,7 @@ discard block |
||
370 | 370 | $this->check_fail(); |
371 | 371 | |
372 | 372 | $this->transaction('commit'); |
373 | - }catch(Exception $er){ |
|
373 | + } catch(Exception $er){ |
|
374 | 374 | $this->transaction('rollback'); |
375 | 375 | throw $er; |
376 | 376 | } |
@@ -409,7 +409,7 @@ discard block |
||
409 | 409 | |
410 | 410 | $this->transaction('commit'); |
411 | 411 | |
412 | - }catch(Exception $er){ |
|
412 | + } catch(Exception $er){ |
|
413 | 413 | $this->transaction('rollback'); |
414 | 414 | throw $er; |
415 | 415 | } |
@@ -472,7 +472,7 @@ discard block |
||
472 | 472 | $where .= " {$key} {$v[0]} {$v[1]} :q_{$v[0]} "; |
473 | 473 | $whereData["q_{$v[0]}"] = $v[2]; |
474 | 474 | } |
475 | - }else{ |
|
475 | + } else{ |
|
476 | 476 | $where .= " {$key} {$value[0]} {$value[1]} :q_{$value[0]} "; |
477 | 477 | $whereData["q_{$value[0]}"] = $value[2]; |
478 | 478 | } |