@@ -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 []; |
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 | |
@@ -93,7 +93,7 @@ discard block |
||
93 | 93 | try { |
94 | 94 | $stmt = Connect::getInstance()->prepare("DELETE FROM {$this->table} WHERE {$terms}"); |
95 | 95 | |
96 | - if($params){ |
|
96 | + if ($params) { |
|
97 | 97 | parse_str($params, $arr); |
98 | 98 | $stmt->execute($arr); |
99 | 99 | 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 | |
@@ -197,7 +197,7 @@ discard block |
||
197 | 197 | |
198 | 198 | foreach ($params as $field) { |
199 | 199 | |
200 | - if(!array_key_exists($field,$this->data) && $this->full){ |
|
200 | + if (!array_key_exists($field, $this->data) && $this->full) { |
|
201 | 201 | throw new Exception("{$field} field does not exist in the table {$this->table}."); |
202 | 202 | } |
203 | 203 | |
@@ -210,11 +210,11 @@ discard block |
||
210 | 210 | |
211 | 211 | public function where(array $where): Datamanager |
212 | 212 | { |
213 | - $this->where['AND'] = (array_key_exists('AND',$this->where)) ?? ''; |
|
213 | + $this->where['AND'] = (array_key_exists('AND', $this->where)) ?? ''; |
|
214 | 214 | $w = []; |
215 | 215 | foreach ($where as $condition => $values) { |
216 | 216 | |
217 | - if(!is_array($values)){ |
|
217 | + if (!is_array($values)) { |
|
218 | 218 | $w['AND'][] = $values; |
219 | 219 | continue; |
220 | 220 | } |
@@ -225,18 +225,18 @@ discard block |
||
225 | 225 | |
226 | 226 | } |
227 | 227 | |
228 | - $this->where = array_merge($this->where,$w); |
|
228 | + $this->where = array_merge($this->where, $w); |
|
229 | 229 | |
230 | 230 | return $this; |
231 | 231 | } |
232 | 232 | |
233 | 233 | public function check_where_array(array $where) |
234 | 234 | { |
235 | - if(count($where) != 3){ |
|
236 | - throw new Exception("Condition where set incorrectly: ".implode(' ',$where)); |
|
235 | + if (count($where) != 3) { |
|
236 | + throw new Exception("Condition where set incorrectly: ".implode(' ', $where)); |
|
237 | 237 | } |
238 | 238 | |
239 | - if(!array_key_exists($where[0],$this->data) && $this->full){ |
|
239 | + if (!array_key_exists($where[0], $this->data) && $this->full) { |
|
240 | 240 | throw new Exception("{$where[0]} field does not exist in the table {$this->table}."); |
241 | 241 | } |
242 | 242 | } |
@@ -249,7 +249,7 @@ discard block |
||
249 | 249 | |
250 | 250 | public function offset(int $offset): Datamanager |
251 | 251 | { |
252 | - if(is_null($this->limit)){ |
|
252 | + if (is_null($this->limit)) { |
|
253 | 253 | throw new Exception("The limit must be set before the offset."); |
254 | 254 | } |
255 | 255 | |
@@ -279,7 +279,7 @@ discard block |
||
279 | 279 | |
280 | 280 | foreach ($arrayValues as $key => $value) { |
281 | 281 | |
282 | - if(!array_key_exists($key,$this->data)){ |
|
282 | + if (!array_key_exists($key, $this->data)) { |
|
283 | 283 | throw new Exception("{$key} field does not exist in the table {$this->table}."); |
284 | 284 | } |
285 | 285 | |
@@ -293,7 +293,7 @@ discard block |
||
293 | 293 | $string = ''; |
294 | 294 | foreach ($this->data as $key => $value) { |
295 | 295 | |
296 | - if(gettype($value)==='object'){ |
|
296 | + if (gettype($value) === 'object') { |
|
297 | 297 | $value = $value->getData()[$this->primary]['value']; |
298 | 298 | } |
299 | 299 | |
@@ -304,32 +304,32 @@ discard block |
||
304 | 304 | |
305 | 305 | public function remove(?bool $exec = false): Datamanager |
306 | 306 | { |
307 | - if($exec){ |
|
307 | + if ($exec) { |
|
308 | 308 | $this->clause = null; |
309 | 309 | |
310 | - if(count($this->where) == 1){ |
|
310 | + if (count($this->where) == 1) { |
|
311 | 311 | $this->removeById(); |
312 | 312 | return $this; |
313 | 313 | } |
314 | 314 | |
315 | 315 | $where = ''; |
316 | 316 | $data = ''; |
317 | - foreach($this->where as $clause => $condition){ |
|
318 | - if(strlen($clause) === 0){ |
|
317 | + foreach ($this->where as $clause => $condition) { |
|
318 | + if (strlen($clause) === 0) { |
|
319 | 319 | $where .= " {$clause} "; |
320 | 320 | $where .= " {$condition[0]} {$condition[1]} :q_{$condition[0]} "; |
321 | 321 | $data .= "q_{$condition[0]}={$condition[2]}&"; |
322 | 322 | continue; |
323 | 323 | } |
324 | 324 | |
325 | - foreach($condition as $column => $value){ |
|
325 | + foreach ($condition as $column => $value) { |
|
326 | 326 | $where .= " {$clause} "; |
327 | 327 | $where .= " {$value[0]} {$value[1]} :q_{$value[0]} "; |
328 | 328 | $data .= "q_{$value[0]}={$value[2]}&"; |
329 | 329 | } |
330 | 330 | } |
331 | 331 | |
332 | - $this->delete($where, substr($data,0,-1) ); |
|
332 | + $this->delete($where, substr($data, 0, -1)); |
|
333 | 333 | |
334 | 334 | $this->check_fail(); |
335 | 335 | |
@@ -343,7 +343,7 @@ discard block |
||
343 | 343 | |
344 | 344 | private function removeById(): bool |
345 | 345 | { |
346 | - $delete = $this->delete("{$this->primary}=:{$this->primary}","{$this->primary}={$this->getData()[$this->primary]['value']}"); |
|
346 | + $delete = $this->delete("{$this->primary}=:{$this->primary}", "{$this->primary}={$this->getData()[$this->primary]['value']}"); |
|
347 | 347 | |
348 | 348 | $this->check_fail(); |
349 | 349 | |
@@ -359,11 +359,11 @@ discard block |
||
359 | 359 | { |
360 | 360 | $data = []; |
361 | 361 | foreach ($this->data as $key => $value) { |
362 | - if(strstr($this->data[$key]['extra'],'auto_increment') && $key != $this->primary){ |
|
362 | + if (strstr($this->data[$key]['extra'], 'auto_increment') && $key != $this->primary) { |
|
363 | 363 | continue; |
364 | 364 | } |
365 | 365 | |
366 | - if($this->data[$key]['changed'] && $this->data[$key]['upgradeable'] || $this->primary == $key){ |
|
366 | + if ($this->data[$key]['changed'] && $this->data[$key]['upgradeable'] || $this->primary == $key) { |
|
367 | 367 | $data[$key] = $this->data[$key]['value']; |
368 | 368 | } |
369 | 369 | } |
@@ -372,13 +372,13 @@ discard block |
||
372 | 372 | $params = $this->primary.'='.$this->getData()[$this->primary]['value']; |
373 | 373 | |
374 | 374 | $this->transaction('begin'); |
375 | - try{ |
|
375 | + try { |
|
376 | 376 | $this->update($data, $terms, $params); |
377 | 377 | |
378 | 378 | $this->check_fail(); |
379 | 379 | |
380 | 380 | $this->transaction('commit'); |
381 | - }catch(Exception $er){ |
|
381 | + } catch (Exception $er) { |
|
382 | 382 | $this->transaction('rollback'); |
383 | 383 | throw $er; |
384 | 384 | } |
@@ -393,11 +393,11 @@ discard block |
||
393 | 393 | $data = []; |
394 | 394 | |
395 | 395 | foreach ($this->data as $key => $value) { |
396 | - if(strstr($this->data[$key]['extra'],'auto_increment')){ |
|
396 | + if (strstr($this->data[$key]['extra'], 'auto_increment')) { |
|
397 | 397 | continue; |
398 | 398 | } |
399 | 399 | |
400 | - if(strlen($value['value']) > $value['maxlength']){ |
|
400 | + if (strlen($value['value']) > $value['maxlength']) { |
|
401 | 401 | throw new Exception("The information provided for column {$key} of table {$this->table} exceeded that allowed."); |
402 | 402 | } |
403 | 403 | |
@@ -407,7 +407,7 @@ discard block |
||
407 | 407 | } |
408 | 408 | |
409 | 409 | $this->transaction('begin'); |
410 | - try{ |
|
410 | + try { |
|
411 | 411 | |
412 | 412 | $id = $this->insert($data); |
413 | 413 | |
@@ -417,7 +417,7 @@ discard block |
||
417 | 417 | |
418 | 418 | $this->transaction('commit'); |
419 | 419 | |
420 | - }catch(Exception $er){ |
|
420 | + } catch (Exception $er) { |
|
421 | 421 | $this->transaction('rollback'); |
422 | 422 | throw $er; |
423 | 423 | } |
@@ -427,13 +427,13 @@ discard block |
||
427 | 427 | |
428 | 428 | public function toEntity() |
429 | 429 | { |
430 | - if($this->getCount() === 0){ |
|
430 | + if ($this->getCount() === 0) { |
|
431 | 431 | return null; |
432 | 432 | } |
433 | 433 | |
434 | 434 | $entity = $this->setByDatabase($this->result[0]); |
435 | 435 | |
436 | - if(count($this->result) > 1){ |
|
436 | + if (count($this->result) > 1) { |
|
437 | 437 | $entity = []; |
438 | 438 | foreach ($this->result as $key => $value) { |
439 | 439 | $entity[] = $this->setByDatabase($value); |
@@ -445,14 +445,14 @@ discard block |
||
445 | 445 | |
446 | 446 | public function findById($id): Datamanager |
447 | 447 | { |
448 | - $this->where([$this->primary,'=',$id]); |
|
448 | + $this->where([$this->primary, '=', $id]); |
|
449 | 449 | return $this; |
450 | 450 | } |
451 | 451 | |
452 | 452 | public function execute(): Datamanager |
453 | 453 | { |
454 | - if(!is_null($this->clause)){ |
|
455 | - if($this->clause == 'remove'){ |
|
454 | + if (!is_null($this->clause)) { |
|
455 | + if ($this->clause == 'remove') { |
|
456 | 456 | return $this->remove(true); |
457 | 457 | } |
458 | 458 | } |
@@ -464,8 +464,8 @@ discard block |
||
464 | 464 | $select .= "{$key},"; |
465 | 465 | } |
466 | 466 | |
467 | - $select = substr($select,0,-1); |
|
468 | - $this->query = str_replace("*",$select,$this->query); |
|
467 | + $select = substr($select, 0, -1); |
|
468 | + $this->query = str_replace("*", $select, $this->query); |
|
469 | 469 | |
470 | 470 | $where = ''; |
471 | 471 | $whereData = []; |
@@ -473,31 +473,31 @@ discard block |
||
473 | 473 | $key = (!$key) ? '' : " {$key} "; |
474 | 474 | |
475 | 475 | |
476 | - if(is_array($value[0])){ |
|
476 | + if (is_array($value[0])) { |
|
477 | 477 | foreach ($value as $k => $v) { |
478 | 478 | $where .= " {$key} {$v[0]} {$v[1]} :q_{$v[0]} "; |
479 | 479 | $whereData["q_{$v[0]}"] = $v[2]; |
480 | 480 | } |
481 | - }else{ |
|
481 | + } else { |
|
482 | 482 | $where .= " {$key} {$value[0]} {$value[1]} :q_{$value[0]} "; |
483 | 483 | $whereData["q_{$value[0]}"] = $value[2]; |
484 | 484 | } |
485 | 485 | |
486 | 486 | } |
487 | - $where = substr($where,0,-1); |
|
487 | + $where = substr($where, 0, -1); |
|
488 | 488 | $this->query .= " WHERE {$where} "; |
489 | 489 | |
490 | 490 | $this->query .= $this->order; |
491 | 491 | |
492 | - if(!is_null($this->limit)){ |
|
492 | + if (!is_null($this->limit)) { |
|
493 | 493 | $this->query .= " LIMIT {$this->limit}"; |
494 | 494 | } |
495 | 495 | |
496 | - if(!is_null($this->offset)){ |
|
496 | + if (!is_null($this->offset)) { |
|
497 | 497 | $this->query .= " OFFSET {$this->offset}"; |
498 | 498 | } |
499 | 499 | |
500 | - $this->result = $this->select($this->query,$whereData); |
|
500 | + $this->result = $this->select($this->query, $whereData); |
|
501 | 501 | |
502 | 502 | $this->check_fail(); |
503 | 503 | |
@@ -511,7 +511,7 @@ discard block |
||
511 | 511 | { |
512 | 512 | $this->query = " SELECT * FROM {$this->table} "; |
513 | 513 | |
514 | - if(is_int($key)){ |
|
514 | + if (is_int($key)) { |
|
515 | 515 | return $this->findById($key); |
516 | 516 | } |
517 | 517 |
@@ -10,7 +10,7 @@ discard block |
||
10 | 10 | |
11 | 11 | $entity = new User(); |
12 | 12 | |
13 | -try{ |
|
13 | +try { |
|
14 | 14 | /* Set new info for insert in database */ |
15 | 15 | $entity->name = 'Henri Azevedo'; |
16 | 16 | $entity->email = '[email protected]'; |
@@ -23,7 +23,7 @@ discard block |
||
23 | 23 | $user = $entity->find()->execute()->first()->toEntity(); |
24 | 24 | |
25 | 25 | /* Search only for columns defined in advance */ |
26 | - $user = $entity->find()->only(['name','email'])->execute()->first(); |
|
26 | + $user = $entity->find()->only(['name', 'email'])->execute()->first(); |
|
27 | 27 | $name = $user->name; |
28 | 28 | $email = $user->email; |
29 | 29 | /* OR */ |
@@ -43,10 +43,10 @@ discard block |
||
43 | 43 | |
44 | 44 | /* Remove by cause *Where* */ |
45 | 45 | $user->remove()->where([ |
46 | - ['name','=','Other Name'], |
|
47 | - 'OR' => ['email','LIKE','[email protected]'] |
|
46 | + ['name', '=', 'Other Name'], |
|
47 | + 'OR' => ['email', 'LIKE', '[email protected]'] |
|
48 | 48 | ])->execute(); |
49 | -}catch(Exception $er){ |
|
49 | +} catch (Exception $er) { |
|
50 | 50 | |
51 | 51 | die($er->getCode().' - '.$er->getMessage()); |
52 | 52 |