@@ -24,7 +24,7 @@ |
||
| 24 | 24 | } |
| 25 | 25 | |
| 26 | 26 | if(!Connect::getInstance()->inTransaction()){ |
| 27 | - return Connect::getInstance()->beginTransaction(); |
|
| 27 | + return Connect::getInstance()->beginTransaction(); |
|
| 28 | 28 | } |
| 29 | 29 | |
| 30 | 30 | switch ($transaction) { |
@@ -6,24 +6,24 @@ 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 | } |
| 19 | 19 | |
| 20 | 20 | protected function transaction(string $transaction): ?bool |
| 21 | 21 | { |
| 22 | - if(array_key_exists($transaction,['begin','commit','rollback'])){ |
|
| 22 | + if (array_key_exists($transaction, ['begin', 'commit', 'rollback'])) { |
|
| 23 | 23 | throw new Exception("{$transaction} é um estado inválido para transações."); |
| 24 | 24 | } |
| 25 | 25 | |
| 26 | - if(!Connect::getInstance()->inTransaction()){ |
|
| 26 | + if (!Connect::getInstance()->inTransaction()) { |
|
| 27 | 27 | return Connect::getInstance()->beginTransaction(); |
| 28 | 28 | } |
| 29 | 29 | |
@@ -34,13 +34,13 @@ discard block |
||
| 34 | 34 | return false; |
| 35 | 35 | } |
| 36 | 36 | |
| 37 | - protected function select(string $query,array $data): ?array |
|
| 37 | + protected function select(string $query, array $data): ?array |
|
| 38 | 38 | { |
| 39 | - try{ |
|
| 39 | + try { |
|
| 40 | 40 | $stmt = Connect::getInstance()->prepare("{$query}"); |
| 41 | 41 | $stmt->execute($data); |
| 42 | 42 | return $stmt->fetchAll(PDO::FETCH_ASSOC); |
| 43 | - }catch(Exception $exception){ |
|
| 43 | + } catch (Exception $exception) { |
|
| 44 | 44 | $this->fail = $exception; |
| 45 | 45 | } |
| 46 | 46 | return []; |
@@ -48,11 +48,11 @@ discard block |
||
| 48 | 48 | |
| 49 | 49 | protected function describe(): array |
| 50 | 50 | { |
| 51 | - try{ |
|
| 51 | + try { |
|
| 52 | 52 | $stmt = Connect::getInstance()->prepare("DESCRIBE {$this->table}"); |
| 53 | 53 | $stmt->execute(); |
| 54 | 54 | return $stmt->fetchAll(PDO::FETCH_ASSOC); |
| 55 | - }catch(Exception $exception){ |
|
| 55 | + } catch (Exception $exception) { |
|
| 56 | 56 | $this->fail = $exception; |
| 57 | 57 | return []; |
| 58 | 58 | } |
@@ -62,7 +62,7 @@ discard block |
||
| 62 | 62 | { |
| 63 | 63 | try { |
| 64 | 64 | $columns = implode(", ", array_keys($data)); |
| 65 | - $values = ":" . implode(", :", array_keys($data)); |
|
| 65 | + $values = ":".implode(", :", array_keys($data)); |
|
| 66 | 66 | |
| 67 | 67 | $stmt = Connect::getInstance()->prepare("INSERT INTO {$this->table} ({$columns}) VALUES ({$values})"); |
| 68 | 68 | |
@@ -100,7 +100,7 @@ discard block |
||
| 100 | 100 | try { |
| 101 | 101 | $stmt = Connect::getInstance()->prepare("DELETE FROM {$this->table} WHERE {$terms}"); |
| 102 | 102 | |
| 103 | - if($params){ |
|
| 103 | + if ($params) { |
|
| 104 | 104 | parse_str($params, $arr); |
| 105 | 105 | $stmt->execute($arr); |
| 106 | 106 | 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 | |
@@ -305,14 +305,14 @@ discard block |
||
| 305 | 305 | private function mountRemove(): array |
| 306 | 306 | { |
| 307 | 307 | $return = ['data' => '', 'where' => '']; |
| 308 | - foreach($this->where as $clause => $condition){ |
|
| 309 | - if(strlen($clause) === 0){ |
|
| 308 | + foreach ($this->where as $clause => $condition) { |
|
| 309 | + if (strlen($clause) === 0) { |
|
| 310 | 310 | $return['where'] .= " {$clause} {$condition[0]} {$condition[1]} :q_{$condition[0]} "; |
| 311 | 311 | $return['data'] .= "q_{$condition[0]}={$condition[2]}&"; |
| 312 | 312 | continue; |
| 313 | 313 | } |
| 314 | 314 | |
| 315 | - foreach($condition as $value){ |
|
| 315 | + foreach ($condition as $value) { |
|
| 316 | 316 | $return['where'] .= " {$clause} {$value[0]} {$value[1]} :q_{$value[0]} "; |
| 317 | 317 | $return['data'] .= "q_{$value[0]}={$value[2]}&"; |
| 318 | 318 | } |
@@ -322,19 +322,19 @@ discard block |
||
| 322 | 322 | |
| 323 | 323 | public function remove(?bool $exec = false): Datamanager |
| 324 | 324 | { |
| 325 | - if(!$exec){ |
|
| 325 | + if (!$exec) { |
|
| 326 | 326 | $this->clause = 'remove'; |
| 327 | 327 | return $this; |
| 328 | 328 | } |
| 329 | 329 | |
| 330 | 330 | $this->clause = null; |
| 331 | 331 | |
| 332 | - if(count($this->where) == 1){ |
|
| 332 | + if (count($this->where) == 1) { |
|
| 333 | 333 | $this->removeById(); |
| 334 | 334 | return $this; |
| 335 | 335 | } |
| 336 | 336 | |
| 337 | - $this->delete($this->mountRemove()['where'], substr( $this->mountRemove()['data'] ,0,-1) ); |
|
| 337 | + $this->delete($this->mountRemove()['where'], substr($this->mountRemove()['data'], 0, -1)); |
|
| 338 | 338 | |
| 339 | 339 | $this->check_fail(); |
| 340 | 340 | |
@@ -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 | |
@@ -360,11 +360,11 @@ discard block |
||
| 360 | 360 | $return = ['data' => '']; |
| 361 | 361 | |
| 362 | 362 | foreach ($this->data as $key => $value) { |
| 363 | - if(strstr($this->data[$key]['extra'],'auto_increment') && $key !== $this->primary){ |
|
| 363 | + if (strstr($this->data[$key]['extra'], 'auto_increment') && $key !== $this->primary) { |
|
| 364 | 364 | continue; |
| 365 | 365 | } |
| 366 | 366 | |
| 367 | - if(($this->data[$key]['changed'] && $this->data[$key]['upgradeable']) || $this->primary === $key){ |
|
| 367 | + if (($this->data[$key]['changed'] && $this->data[$key]['upgradeable']) || $this->primary === $key) { |
|
| 368 | 368 | $return['data'][$key] = $this->data[$key]['value']; |
| 369 | 369 | } |
| 370 | 370 | } |
@@ -377,7 +377,7 @@ discard block |
||
| 377 | 377 | |
| 378 | 378 | $this->transaction('begin'); |
| 379 | 379 | |
| 380 | - try{ |
|
| 380 | + try { |
|
| 381 | 381 | $this->update( |
| 382 | 382 | $this->mountSave()['data'], |
| 383 | 383 | "{$this->primary}=:{$this->primary}", |
@@ -387,7 +387,7 @@ discard block |
||
| 387 | 387 | $this->check_fail(); |
| 388 | 388 | |
| 389 | 389 | $this->transaction('commit'); |
| 390 | - }catch(Exception $er){ |
|
| 390 | + } catch (Exception $er) { |
|
| 391 | 391 | $this->transaction('rollback'); |
| 392 | 392 | throw $er; |
| 393 | 393 | } |
@@ -402,11 +402,11 @@ discard block |
||
| 402 | 402 | $data = []; |
| 403 | 403 | |
| 404 | 404 | foreach ($this->data as $key => $value) { |
| 405 | - if(strstr($this->data[$key]['extra'],'auto_increment')){ |
|
| 405 | + if (strstr($this->data[$key]['extra'], 'auto_increment')) { |
|
| 406 | 406 | continue; |
| 407 | 407 | } |
| 408 | 408 | |
| 409 | - if(strlen($value['value']) > $value['maxlength']){ |
|
| 409 | + if (strlen($value['value']) > $value['maxlength']) { |
|
| 410 | 410 | throw new Exception("The information provided for column {$key} of table {$this->table} exceeded that allowed."); |
| 411 | 411 | } |
| 412 | 412 | |
@@ -416,7 +416,7 @@ discard block |
||
| 416 | 416 | } |
| 417 | 417 | |
| 418 | 418 | $this->transaction('begin'); |
| 419 | - try{ |
|
| 419 | + try { |
|
| 420 | 420 | |
| 421 | 421 | $id = $this->insert($data); |
| 422 | 422 | |
@@ -426,7 +426,7 @@ discard block |
||
| 426 | 426 | |
| 427 | 427 | $this->transaction('commit'); |
| 428 | 428 | |
| 429 | - }catch(Exception $er){ |
|
| 429 | + } catch (Exception $er) { |
|
| 430 | 430 | $this->transaction('rollback'); |
| 431 | 431 | throw $er; |
| 432 | 432 | } |
@@ -436,13 +436,13 @@ discard block |
||
| 436 | 436 | |
| 437 | 437 | public function toEntity() |
| 438 | 438 | { |
| 439 | - if($this->getCount() === 0){ |
|
| 439 | + if ($this->getCount() === 0) { |
|
| 440 | 440 | return null; |
| 441 | 441 | } |
| 442 | 442 | |
| 443 | 443 | $entity = $this->setByDatabase($this->result[0]); |
| 444 | 444 | |
| 445 | - if(count($this->result) > 1){ |
|
| 445 | + if (count($this->result) > 1) { |
|
| 446 | 446 | $entity = []; |
| 447 | 447 | foreach ($this->result as $key => $value) { |
| 448 | 448 | $entity[] = $this->setByDatabase($value); |
@@ -454,7 +454,7 @@ discard block |
||
| 454 | 454 | |
| 455 | 455 | public function findById($id): Datamanager |
| 456 | 456 | { |
| 457 | - $this->where([$this->primary,'=',$id]); |
|
| 457 | + $this->where([$this->primary, '=', $id]); |
|
| 458 | 458 | return $this; |
| 459 | 459 | } |
| 460 | 460 | |
@@ -466,7 +466,7 @@ discard block |
||
| 466 | 466 | |
| 467 | 467 | $key = (!$key) ? '' : " {$key} "; |
| 468 | 468 | |
| 469 | - if(is_array($value[0])){ |
|
| 469 | + if (is_array($value[0])) { |
|
| 470 | 470 | |
| 471 | 471 | foreach ($value as $k => $v) { |
| 472 | 472 | $return['where'] .= " {$key} {$v[0]} {$v[1]} :q_{$v[0]} "; |
@@ -485,7 +485,7 @@ discard block |
||
| 485 | 485 | |
| 486 | 486 | public function execute(): Datamanager |
| 487 | 487 | { |
| 488 | - if(!is_null($this->clause) && $this->clause == 'remove'){ |
|
| 488 | + if (!is_null($this->clause) && $this->clause == 'remove') { |
|
| 489 | 489 | return $this->remove(true); |
| 490 | 490 | } |
| 491 | 491 | |
@@ -493,7 +493,7 @@ discard block |
||
| 493 | 493 | |
| 494 | 494 | $this->mountSelect(); |
| 495 | 495 | |
| 496 | - $where = substr($this->mountWhereExec()['where'],0,-1); |
|
| 496 | + $where = substr($this->mountWhereExec()['where'], 0, -1); |
|
| 497 | 497 | $this->query .= " WHERE {$where} "; |
| 498 | 498 | |
| 499 | 499 | $this->query .= $this->order; |
@@ -513,21 +513,21 @@ discard block |
||
| 513 | 513 | |
| 514 | 514 | private function mountSelect() |
| 515 | 515 | { |
| 516 | - $select = substr(implode(',',array_keys($this->select)), 0, -1); |
|
| 516 | + $select = substr(implode(',', array_keys($this->select)), 0, -1); |
|
| 517 | 517 | |
| 518 | - $this->query = str_replace('*', $select,$this->query); |
|
| 518 | + $this->query = str_replace('*', $select, $this->query); |
|
| 519 | 519 | } |
| 520 | 520 | |
| 521 | 521 | private function mountLimit() |
| 522 | 522 | { |
| 523 | - if(!is_null($this->limit)){ |
|
| 523 | + if (!is_null($this->limit)) { |
|
| 524 | 524 | $this->query .= " LIMIT {$this->limit}"; |
| 525 | 525 | } |
| 526 | 526 | } |
| 527 | 527 | |
| 528 | 528 | private function mountOffset() |
| 529 | 529 | { |
| 530 | - if(!is_null($this->offset)){ |
|
| 530 | + if (!is_null($this->offset)) { |
|
| 531 | 531 | $this->query .= " OFFSET {$this->offset}"; |
| 532 | 532 | } |
| 533 | 533 | } |
@@ -536,7 +536,7 @@ discard block |
||
| 536 | 536 | { |
| 537 | 537 | $this->query = " SELECT * FROM {$this->table} "; |
| 538 | 538 | |
| 539 | - if(is_int($key)){ |
|
| 539 | + if (is_int($key)) { |
|
| 540 | 540 | return $this->findById($key); |
| 541 | 541 | } |
| 542 | 542 | |
@@ -387,7 +387,7 @@ discard block |
||
| 387 | 387 | $this->check_fail(); |
| 388 | 388 | |
| 389 | 389 | $this->transaction('commit'); |
| 390 | - }catch(Exception $er){ |
|
| 390 | + } catch(Exception $er){ |
|
| 391 | 391 | $this->transaction('rollback'); |
| 392 | 392 | throw $er; |
| 393 | 393 | } |
@@ -426,7 +426,7 @@ discard block |
||
| 426 | 426 | |
| 427 | 427 | $this->transaction('commit'); |
| 428 | 428 | |
| 429 | - }catch(Exception $er){ |
|
| 429 | + } catch(Exception $er){ |
|
| 430 | 430 | $this->transaction('rollback'); |
| 431 | 431 | throw $er; |
| 432 | 432 | } |