Passed
Push — master ( 07a307...bf6366 )
by Henri
01:19
created
src/Datamanager.php 1 patch
Spacing   +52 added lines, -52 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;
@@ -50,16 +50,16 @@  discard block
 block discarded – undo
50 50
         $type = $value;
51 51
         $maxlength = null;
52 52
 
53
-        if(strpos($value,'(')){
54
-            $type = (array_key_exists( substr($value,0,strpos($value,'(')) , ['varchar','char','text'])) ? 'string' : $type;
55
-            $type = (array_key_exists( substr($value,0,strpos($value,'(')) , ['tinyint','mediumint','smallint','bigtint','int'])) ? 'int' : $type;
56
-            $type = (array_key_exists( substr($value,0,strpos($value,'(')) , ['decimal','float','double','real'])) ? 'float' : $type;
53
+        if (strpos($value, '(')) {
54
+            $type = (array_key_exists(substr($value, 0, strpos($value, '(')), ['varchar', 'char', 'text'])) ? 'string' : $type;
55
+            $type = (array_key_exists(substr($value, 0, strpos($value, '(')), ['tinyint', 'mediumint', 'smallint', 'bigtint', 'int'])) ? 'int' : $type;
56
+            $type = (array_key_exists(substr($value, 0, strpos($value, '(')), ['decimal', 'float', 'double', 'real'])) ? 'float' : $type;
57 57
         }
58 58
 
59
-        $maxlength = (array_key_exists( $type , ['string','float','int'])) ? substr($value,(strpos($value,'(')+1),-1) : $maxlength;
60
-        $maxlength = (array_key_exists( $type , ['date'])) ? 10 : $maxlength;
61
-        $maxlength = (array_key_exists( $type , ['datetime'])) ? 19 : $maxlength;
62
-        $maxlength = (array_key_exists( $type , ['boolean'])) ? 1 : $maxlength;
59
+        $maxlength = (array_key_exists($type, ['string', 'float', 'int'])) ? substr($value, (strpos($value, '(') + 1), -1) : $maxlength;
60
+        $maxlength = (array_key_exists($type, ['date'])) ? 10 : $maxlength;
61
+        $maxlength = (array_key_exists($type, ['datetime'])) ? 19 : $maxlength;
62
+        $maxlength = (array_key_exists($type, ['boolean'])) ? 1 : $maxlength;
63 63
 
64 64
         $this->$field = ['maxlength' => $maxlength];
65 65
         $this->$field = ['type' => $type];
@@ -100,16 +100,16 @@  discard block
 block discarded – undo
100 100
         return $this;
101 101
     }
102 102
 
103
-    public function __set(string $prop,$value): Datamanager
103
+    public function __set(string $prop, $value): Datamanager
104 104
     {
105 105
 
106
-        if(is_array($value)){
106
+        if (is_array($value)) {
107 107
             $attr = array_keys($value)[0];
108 108
             $this->data[$prop][$attr] = $value[$attr];
109 109
             return $this;
110 110
         }
111 111
 
112
-        if($this->full && !array_key_exists($prop,$this->data)){
112
+        if ($this->full && !array_key_exists($prop, $this->data)) {
113 113
             throw new Exception("{$prop} field does not exist in the table {$this->table}.");
114 114
         }
115 115
 
@@ -126,7 +126,7 @@  discard block
 block discarded – undo
126 126
 
127 127
     public function __get(string $field)
128 128
     {
129
-        if($this->full && !array_key_exists($field,$this->data)){
129
+        if ($this->full && !array_key_exists($field, $this->data)) {
130 130
             throw new Exception("{$field} field does not exist in the table {$this->table}.");
131 131
         }
132 132
 
@@ -143,7 +143,7 @@  discard block
 block discarded – undo
143 143
         $deniable = (is_array($deniable)) ? $deniable : [$deniable];
144 144
 
145 145
         foreach ($deniable as $field) {
146
-            if(!array_key_exists($field,$this->data)){
146
+            if (!array_key_exists($field, $this->data)) {
147 147
                 throw new Exception("{$field} field does not exist in the table {$this->table}.");
148 148
             }
149 149
 
@@ -163,11 +163,11 @@  discard block
 block discarded – undo
163 163
 
164 164
     public function orderBy(string $field, string $ord = 'ASC'): Datamanager
165 165
     {
166
-        if(!array_key_exists(str_replace(['asc','ASC','desc','DESC',' '],'',$field),$this->data) && $this->full){
166
+        if (!array_key_exists(str_replace(['asc', 'ASC', 'desc', 'DESC', ' '], '', $field), $this->data) && $this->full) {
167 167
             throw new Exception("{$field} field does not exist in the table {$this->table}.");
168 168
         }
169 169
 
170
-        if(strpos(strtolower($field),'asc') || strpos(strtolower($field),'desc')){
170
+        if (strpos(strtolower($field), 'asc') || strpos(strtolower($field), 'desc')) {
171 171
             $ord = '';
172 172
         }
173 173
 
@@ -182,7 +182,7 @@  discard block
 block discarded – undo
182 182
 
183 183
         foreach ($params as $field) {
184 184
 
185
-            if(!array_key_exists($field,$this->data) && $this->full){
185
+            if (!array_key_exists($field, $this->data) && $this->full) {
186 186
                 throw new Exception("{$field} field does not exist in the table {$this->table}.");
187 187
             }
188 188
 
@@ -195,11 +195,11 @@  discard block
 block discarded – undo
195 195
 
196 196
     public function where(array $where): Datamanager
197 197
     {
198
-        $this->where['AND'] = (array_key_exists('AND',$this->where)) ?? '';
198
+        $this->where['AND'] = (array_key_exists('AND', $this->where)) ?? '';
199 199
         $w = [];
200 200
         foreach ($where as $condition => $values) {
201 201
 
202
-            if(!is_array($values)){
202
+            if (!is_array($values)) {
203 203
                 $w['AND'][] = $values;
204 204
                 continue;
205 205
             }
@@ -210,18 +210,18 @@  discard block
 block discarded – undo
210 210
                        
211 211
         }
212 212
 
213
-        $this->where = array_merge($this->where,$w);
213
+        $this->where = array_merge($this->where, $w);
214 214
 
215 215
         return $this;
216 216
     }
217 217
 
218 218
     public function check_where_array(array $where)
219 219
     {
220
-        if(count($where) != 3){
221
-            throw new Exception("Condition where set incorrectly: ".implode(' ',$where));
220
+        if (count($where) != 3) {
221
+            throw new Exception("Condition where set incorrectly: ".implode(' ', $where));
222 222
         }
223 223
 
224
-        if(!array_key_exists($where[0],$this->data) && $this->full){
224
+        if (!array_key_exists($where[0], $this->data) && $this->full) {
225 225
             throw new Exception("{$where[0]} field does not exist in the table {$this->table}.");
226 226
         }
227 227
     }
@@ -234,7 +234,7 @@  discard block
 block discarded – undo
234 234
 
235 235
     public function offset(int $offset): Datamanager
236 236
     {
237
-        if(is_null($this->limit)){
237
+        if (is_null($this->limit)) {
238 238
             throw new Exception("The limit must be set before the offset.");
239 239
         }
240 240
 
@@ -264,7 +264,7 @@  discard block
 block discarded – undo
264 264
 
265 265
         foreach ($arrayValues as $key => $value) {
266 266
 
267
-            if(!array_key_exists($key,$this->data)){
267
+            if (!array_key_exists($key, $this->data)) {
268 268
                 throw new Exception("{$key} field does not exist in the table {$this->table}.");
269 269
             }
270 270
 
@@ -278,7 +278,7 @@  discard block
 block discarded – undo
278 278
         $string = '';
279 279
         foreach ($this->data as $key => $value) {
280 280
 
281
-            if(gettype($value)==='object'){
281
+            if (gettype($value) === 'object') {
282 282
                 $value = $value->getData()[$this->primary]['value'];
283 283
             }
284 284
 
@@ -290,14 +290,14 @@  discard block
 block discarded – undo
290 290
     private function mountRemove(): array
291 291
     {
292 292
         $return = ['data' => null, 'where' => null];
293
-        foreach($this->where as $clause => $condition){
294
-            if(strlen($clause) === 0){
293
+        foreach ($this->where as $clause => $condition) {
294
+            if (strlen($clause) === 0) {
295 295
                 $return['where'] .= " {$clause} {$condition[0]} {$condition[1]} :q_{$condition[0]} ";
296 296
                 $return['data'] .= "q_{$condition[0]}={$condition[2]}&";
297 297
                 continue;
298 298
             }
299 299
                 
300
-            foreach($condition as $value){
300
+            foreach ($condition as $value) {
301 301
                 $return['where'] .= " {$clause} {$value[0]} {$value[1]} :q_{$value[0]} ";
302 302
                 $return['data'] .= "q_{$value[0]}={$value[2]}&";
303 303
             }
@@ -307,19 +307,19 @@  discard block
 block discarded – undo
307 307
 
308 308
     public function remove(?bool $exec = false): Datamanager
309 309
     {
310
-        if(!$exec){
310
+        if (!$exec) {
311 311
             $this->clause = 'remove';    
312 312
             return $this;
313 313
         }
314 314
 
315 315
         $this->clause = null;
316 316
 
317
-        if(count($this->where) == 1){
317
+        if (count($this->where) == 1) {
318 318
             $this->removeById();
319 319
             return $this;
320 320
         }
321 321
 
322
-        $this->delete($this->mountRemove()['where'], substr( $this->mountRemove()['data'] ,0,-1) );
322
+        $this->delete($this->mountRemove()['where'], substr($this->mountRemove()['data'], 0, -1));
323 323
 
324 324
         $this->check_fail();
325 325
             
@@ -328,7 +328,7 @@  discard block
 block discarded – undo
328 328
 
329 329
     private function removeById(): bool
330 330
     {
331
-        $delete = $this->delete("{$this->primary}=:{$this->primary}","{$this->primary}={$this->getData()[$this->primary]['value']}");
331
+        $delete = $this->delete("{$this->primary}=:{$this->primary}", "{$this->primary}={$this->getData()[$this->primary]['value']}");
332 332
 
333 333
         $this->check_fail();
334 334
 
@@ -340,11 +340,11 @@  discard block
 block discarded – undo
340 340
         $return = ['data' => null];
341 341
 
342 342
         foreach ($this->data as $key => $value) {
343
-            if(strstr($this->data[$key]['extra'],'auto_increment') && $key !== $this->primary){
343
+            if (strstr($this->data[$key]['extra'], 'auto_increment') && $key !== $this->primary) {
344 344
                 continue;
345 345
             }
346 346
 
347
-            if(($this->data[$key]['changed'] && $this->data[$key]['upgradeable']) || $this->primary === $key){
347
+            if (($this->data[$key]['changed'] && $this->data[$key]['upgradeable']) || $this->primary === $key) {
348 348
                 $return['data'][$key] = $this->data[$key]['value'];
349 349
             }
350 350
         }
@@ -357,7 +357,7 @@  discard block
 block discarded – undo
357 357
         
358 358
         $this->transaction('begin');
359 359
 
360
-        try{
360
+        try {
361 361
             $this->update(
362 362
                 $this->mountSave()['data'],
363 363
                 "{$this->primary}=:{$this->primary}", 
@@ -367,7 +367,7 @@  discard block
 block discarded – undo
367 367
             $this->check_fail();
368 368
 
369 369
             $this->transaction('commit');
370
-        }catch(Exception $er){
370
+        } catch (Exception $er) {
371 371
             $this->transaction('rollback');
372 372
             throw $er;
373 373
         }
@@ -382,11 +382,11 @@  discard block
 block discarded – undo
382 382
         $data = [];
383 383
 
384 384
         foreach ($this->data as $key => $value) {
385
-            if(strstr($this->data[$key]['extra'],'auto_increment')){
385
+            if (strstr($this->data[$key]['extra'], 'auto_increment')) {
386 386
                 continue;
387 387
             }
388 388
 
389
-            if(strlen($value['value']) > $value['maxlength']){
389
+            if (strlen($value['value']) > $value['maxlength']) {
390 390
                 throw new Exception("The information provided for column {$key} of table {$this->table} exceeded that allowed.");
391 391
             }
392 392
 
@@ -396,7 +396,7 @@  discard block
 block discarded – undo
396 396
         }
397 397
 
398 398
         $this->transaction('begin');
399
-        try{
399
+        try {
400 400
            
401 401
             $id = $this->insert($data);
402 402
 
@@ -406,7 +406,7 @@  discard block
 block discarded – undo
406 406
             
407 407
             $this->transaction('commit');
408 408
 
409
-        }catch(Exception $er){
409
+        } catch (Exception $er) {
410 410
             $this->transaction('rollback');
411 411
             throw $er;
412 412
         }
@@ -416,13 +416,13 @@  discard block
 block discarded – undo
416 416
 
417 417
     public function toEntity()
418 418
     {
419
-        if($this->getCount() === 0){
419
+        if ($this->getCount() === 0) {
420 420
             return null;
421 421
         }
422 422
 
423 423
         $entity = $this->setByDatabase($this->result[0]);
424 424
 
425
-        if(count($this->result) > 1){
425
+        if (count($this->result) > 1) {
426 426
             $entity = [];
427 427
             foreach ($this->result as $key => $value) {
428 428
                 $entity[] = $this->setByDatabase($value);
@@ -434,7 +434,7 @@  discard block
 block discarded – undo
434 434
 
435 435
     public function findById($id): Datamanager
436 436
     {
437
-        $this->where([$this->primary,'=',$id]);
437
+        $this->where([$this->primary, '=', $id]);
438 438
         return $this;
439 439
     }
440 440
 
@@ -446,7 +446,7 @@  discard block
 block discarded – undo
446 446
 
447 447
             $key = (!$key) ? '' : " {$key} ";
448 448
 
449
-            if(is_array($value[0])){
449
+            if (is_array($value[0])) {
450 450
 
451 451
                 foreach ($value as $k => $v) {
452 452
                     $return['where'] .= " {$key} {$v[0]} {$v[1]} :q_{$v[0]} ";
@@ -465,7 +465,7 @@  discard block
 block discarded – undo
465 465
 
466 466
     public function execute(): Datamanager
467 467
     {
468
-        if(!is_null($this->clause) && $this->clause == 'remove'){
468
+        if (!is_null($this->clause) && $this->clause == 'remove') {
469 469
             return $this->remove(true);
470 470
         }
471 471
 
@@ -473,7 +473,7 @@  discard block
 block discarded – undo
473 473
         
474 474
         $this->mountSelect();
475 475
         
476
-        $where = substr($this->mountWhereExec()['where'],0,-1);
476
+        $where = substr($this->mountWhereExec()['where'], 0, -1);
477 477
         $this->query .= " WHERE {$where} ";
478 478
 
479 479
         $this->query .= $this->order;
@@ -493,21 +493,21 @@  discard block
 block discarded – undo
493 493
 
494 494
     private function mountSelect()
495 495
     {
496
-        $select = implode(',',array_keys($this->select));
496
+        $select = implode(',', array_keys($this->select));
497 497
 
498
-        $this->query = str_replace('*', $select,$this->query);
498
+        $this->query = str_replace('*', $select, $this->query);
499 499
     }
500 500
 
501 501
     private function mountLimit()
502 502
     {
503
-        if(!is_null($this->limit)){
503
+        if (!is_null($this->limit)) {
504 504
             $this->query .= " LIMIT {$this->limit}";
505 505
         }
506 506
     }
507 507
 
508 508
     private function mountOffset()
509 509
     {
510
-        if(!is_null($this->offset)){
510
+        if (!is_null($this->offset)) {
511 511
             $this->query .= " OFFSET {$this->offset}";
512 512
         }
513 513
     }
@@ -516,7 +516,7 @@  discard block
 block discarded – undo
516 516
     {
517 517
         $this->query = " SELECT * FROM {$this->table} ";
518 518
 
519
-        if(is_int($key)){
519
+        if (is_int($key)) {
520 520
             return $this->findById($key);
521 521
         }
522 522
 
Please login to merge, or discard this patch.
examples/index.php 2 patches
Spacing   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -10,7 +10,7 @@  discard block
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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("Code Error: {$er->getCode()}, Line: {$er->getLine()}, File: {$er->getFile()}, Message: {$er->getMessage()}.");
52 52
 
Please login to merge, or discard this patch.
Braces   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -46,7 +46,7 @@
 block discarded – undo
46 46
         ['name','=','Other Name'],
47 47
         'OR' => ['email','LIKE','[email protected]']
48 48
     ])->execute();
49
-}catch(Exception $er){
49
+} catch(Exception $er){
50 50
 
51 51
     die("Code Error: {$er->getCode()}, Line: {$er->getLine()}, File: {$er->getFile()}, Message: {$er->getMessage()}.");
52 52
 
Please login to merge, or discard this patch.