Passed
Branch master (d22725)
by Henri
02:04 queued 45s
created
src/Datamanager.php 1 patch
Spacing   +50 added lines, -50 removed lines patch added patch discarded remove patch
@@ -17,7 +17,7 @@  discard block
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 
Please login to merge, or discard this patch.