Completed
Push — master ( f3e554...ebd28e )
by Ivan
01:53
created
src/schema/TableQuery.php 1 patch
Spacing   +156 added lines, -159 removed lines patch added patch discarded remove patch
@@ -44,7 +44,7 @@  discard block
 block discarded – undo
44 44
     /**
45 45
      * @var int[]
46 46
      */
47
-    protected $li_of = [0,0,0];
47
+    protected $li_of = [0, 0, 0];
48 48
     /**
49 49
      * @var array
50 50
      */
@@ -74,7 +74,7 @@  discard block
 block discarded – undo
74 74
     public function __construct(DBInterface $db, $table)
75 75
     {
76 76
         $this->db = $db;
77
-        $this->definition = $table instanceof Table ? $table : $this->db->definition((string)$table);
77
+        $this->definition = $table instanceof Table ? $table : $this->db->definition((string) $table);
78 78
         $primary = $this->definition->getPrimaryKey();
79 79
         $columns = $this->definition->getColumns();
80 80
         $this->pkey = count($primary) ? $primary : $columns;
@@ -97,7 +97,7 @@  discard block
 block discarded – undo
97 97
     {
98 98
         $column = explode('.', $column);
99 99
         if (count($column) === 1) {
100
-            $column = [ $this->definition->getName(), $column[0] ];
100
+            $column = [$this->definition->getName(), $column[0]];
101 101
             $col = $this->definition->getColumn($column[1]);
102 102
             if (!$col) {
103 103
                 throw new DBException('Invalid column name in own table');
@@ -120,7 +120,7 @@  discard block
 block discarded – undo
120 120
                         throw new DBException('Invalid column name in related table');
121 121
                     }
122 122
                 } else {
123
-                    throw new DBException('Invalid foreign table name: ' . implode(',', $column));
123
+                    throw new DBException('Invalid foreign table name: '.implode(',', $column));
124 124
                 }
125 125
             }
126 126
         } else {
@@ -129,15 +129,15 @@  discard block
 block discarded – undo
129 129
             $table = $this->definition;
130 130
             $table = array_reduce(
131 131
                 $column,
132
-                function ($carry, $item) use (&$table) {
132
+                function($carry, $item) use (&$table) {
133 133
                     $table = $table->getRelation($item)->table;
134 134
                     return $table;
135 135
                 }
136 136
             );
137 137
             $col = $table->getColumn($name);
138
-            $column = [ implode(static::SEP, $column), $name ];
138
+            $column = [implode(static::SEP, $column), $name];
139 139
         }
140
-        return [ 'name' => implode('.', $column), 'data' => $col ];
140
+        return ['name' => implode('.', $column), 'data' => $col];
141 141
     }
142 142
     protected function normalizeValue(TableColumn $col, $value)
143 143
     {
@@ -184,9 +184,9 @@  discard block
 block discarded – undo
184 184
                 }
185 185
                 return $value;
186 186
             case 'int':
187
-                return (int)preg_replace('([^+\-0-9]+)', '', $value);
187
+                return (int) preg_replace('([^+\-0-9]+)', '', $value);
188 188
             case 'float':
189
-                return (int)preg_replace('([^+\-0-9.]+)', '', str_replace(',', '.', $value));
189
+                return (int) preg_replace('([^+\-0-9.]+)', '', str_replace(',', '.', $value));
190 190
             default:
191 191
                 return $value;
192 192
         }
@@ -204,25 +204,23 @@  discard block
 block discarded – undo
204 204
         list($name, $column) = array_values($this->getColumn($column));
205 205
         if (is_null($value)) {
206 206
             return $negate ?
207
-                $this->where($name . ' IS NOT NULL') :
208
-                $this->where($name . ' IS NULL');
207
+                $this->where($name.' IS NOT NULL') : $this->where($name.' IS NULL');
209 208
         }
210 209
         if (!is_array($value)) {
211 210
             return $negate ?
212 211
                 $this->where(
213
-                    $name . ' <> ?',
214
-                    [ $this->normalizeValue($column, $value) ]
215
-                ) :
216
-                $this->where(
217
-                    $name . ' = ?',
218
-                    [ $this->normalizeValue($column, $value) ]
212
+                    $name.' <> ?',
213
+                    [$this->normalizeValue($column, $value)]
214
+                ) : $this->where(
215
+                    $name.' = ?',
216
+                    [$this->normalizeValue($column, $value)]
219 217
                 );
220 218
         }
221 219
         if (isset($value['beg']) && strlen($value['beg']) && (!isset($value['end']) || !strlen($value['end']))) {
222
-            $value = [ 'gte' => $value['beg'] ];
220
+            $value = ['gte' => $value['beg']];
223 221
         }
224 222
         if (isset($value['end']) && strlen($value['end']) && (!isset($value['beg']) || !strlen($value['beg']))) {
225
-            $value = [ 'lte' => $value['end'] ];
223
+            $value = ['lte' => $value['end']];
226 224
         }
227 225
         if (isset($value['beg']) && isset($value['end'])) {
228 226
             return $negate ?
@@ -232,8 +230,7 @@  discard block
 block discarded – undo
232 230
                         $this->normalizeValue($column, $value['beg']),
233 231
                         $this->normalizeValue($column, $value['end'])
234 232
                     ]
235
-                ) :
236
-                $this->where(
233
+                ) : $this->where(
237 234
                     $name.' BETWEEN ? AND ?',
238 235
                     [
239 236
                         $this->normalizeValue($column, $value['beg']),
@@ -244,38 +241,38 @@  discard block
 block discarded – undo
244 241
         if (isset($value['gt']) || isset($value['lt']) || isset($value['gte']) || isset($value['lte'])) {
245 242
             if (isset($value['gt'])) {
246 243
                 $this->where(
247
-                    $name. ' ' . ($negate ? '<=' : '>') . ' ?',
248
-                    [ $this->normalizeValue($column, $value['gt']) ]
244
+                    $name.' '.($negate ? '<=' : '>').' ?',
245
+                    [$this->normalizeValue($column, $value['gt'])]
249 246
                 );
250 247
             }
251 248
             if (isset($value['gte'])) {
252 249
                 $this->where(
253
-                    $name. ' ' . ($negate ? '<' : '>=') . ' ?',
254
-                    [ $this->normalizeValue($column, $value['gte']) ]
250
+                    $name.' '.($negate ? '<' : '>=').' ?',
251
+                    [$this->normalizeValue($column, $value['gte'])]
255 252
                 );
256 253
             }
257 254
             if (isset($value['lt'])) {
258 255
                 $this->where(
259
-                    $name. ' ' . ($negate ? '>=' : '<') . ' ?',
260
-                    [ $this->normalizeValue($column, $value['lt']) ]
256
+                    $name.' '.($negate ? '>=' : '<').' ?',
257
+                    [$this->normalizeValue($column, $value['lt'])]
261 258
                 );
262 259
             }
263 260
             if (isset($value['lte'])) {
264 261
                 $this->where(
265
-                    $name. ' ' . ($negate ? '>' : '<=') . ' ?',
266
-                    [ $this->normalizeValue($column, $value['lte']) ]
262
+                    $name.' '.($negate ? '>' : '<=').' ?',
263
+                    [$this->normalizeValue($column, $value['lte'])]
267 264
                 );
268 265
             }
269 266
             return $this;
270 267
         }
271 268
         return $negate ?
272 269
             $this->where(
273
-                $name . ' NOT IN (??)',
274
-                [ array_map(function ($v) use ($column) { return $this->normalizeValue($column, $v); }, $value) ]
270
+                $name.' NOT IN (??)',
271
+                [array_map(function($v) use ($column) { return $this->normalizeValue($column, $v); }, $value)]
275 272
             ) :
276 273
             $this->where(
277
-                $name . ' IN (??)',
278
-                [ array_map(function ($v) use ($column) { return $this->normalizeValue($column, $v); }, $value) ]
274
+                $name.' IN (??)',
275
+                [array_map(function($v) use ($column) { return $this->normalizeValue($column, $v); }, $value)]
279 276
             );
280 277
     }
281 278
     /**
@@ -286,7 +283,7 @@  discard block
 block discarded – undo
286 283
      */
287 284
     public function sort(string $column, bool $desc = false) : TableQuery
288 285
     {
289
-        return $this->order($this->getColumn($column)['name'] . ' ' . ($desc ? 'DESC' : 'ASC'));
286
+        return $this->order($this->getColumn($column)['name'].' '.($desc ? 'DESC' : 'ASC'));
290 287
     }
291 288
     /**
292 289
      * Group by a column (or columns)
@@ -296,7 +293,7 @@  discard block
 block discarded – undo
296 293
     public function group($column) : TableQuery
297 294
     {
298 295
         if (!is_array($column)) {
299
-            $column = [ $column ];
296
+            $column = [$column];
300 297
         }
301 298
         foreach ($column as $k => $v) {
302 299
             $column[$k] = $this->getColumn($v)['name'];
@@ -338,7 +335,7 @@  discard block
 block discarded – undo
338 335
         $this->order = [];
339 336
         $this->having = [];
340 337
         $this->aliases = [];
341
-        $this->li_of = [0,0,0];
338
+        $this->li_of = [0, 0, 0];
342 339
         $this->qiterator = null;
343 340
         return $this;
344 341
     }
@@ -351,7 +348,7 @@  discard block
 block discarded – undo
351 348
     public function groupBy(string $sql, array $params = []) : TableQuery
352 349
     {
353 350
         $this->qiterator = null;
354
-        $this->group = [ $sql, $params ];
351
+        $this->group = [$sql, $params];
355 352
         return $this;
356 353
     }
357 354
     /**
@@ -364,7 +361,7 @@  discard block
 block discarded – undo
364 361
      */
365 362
     public function join($table, array $fields, string $name = null, bool $multiple = true)
366 363
     {
367
-        $table = $table instanceof Table ? $table : $this->db->definition((string)$table);
364
+        $table = $table instanceof Table ? $table : $this->db->definition((string) $table);
368 365
         $name = $name ?? $table->getName();
369 366
         if (isset($this->joins[$name]) || $this->definition->hasRelation($name)) {
370 367
             throw new DBException('Alias / table name already in use');
@@ -373,7 +370,7 @@  discard block
 block discarded – undo
373 370
         foreach ($fields as $k => $v) {
374 371
             $k = explode('.', $k, 2);
375 372
             $k = count($k) == 2 ? $k[1] : $k[0];
376
-            $this->joins[$name]->keymap[$this->getColumn($name . '.' . $k)['name']] = $this->getColumn($v)['name'];
373
+            $this->joins[$name]->keymap[$this->getColumn($name.'.'.$k)['name']] = $this->getColumn($v)['name'];
377 374
         }
378 375
         return $this;
379 376
     }
@@ -386,7 +383,7 @@  discard block
 block discarded – undo
386 383
     public function where(string $sql, array $params = []) : TableQuery
387 384
     {
388 385
         $this->qiterator = null;
389
-        $this->where[] = [ $sql, $params ];
386
+        $this->where[] = [$sql, $params];
390 387
         return $this;
391 388
     }
392 389
     /**
@@ -398,7 +395,7 @@  discard block
 block discarded – undo
398 395
     public function having(string $sql, array $params = []) : TableQuery
399 396
     {
400 397
         $this->qiterator = null;
401
-        $this->having[] = [ $sql, $params ];
398
+        $this->having[] = [$sql, $params];
402 399
         return $this;
403 400
     }
404 401
     /**
@@ -410,7 +407,7 @@  discard block
 block discarded – undo
410 407
     public function order(string $sql, array $params = []) : TableQuery
411 408
     {
412 409
         $this->qiterator = null;
413
-        $this->order = [ $sql, $params ];
410
+        $this->order = [$sql, $params];
414 411
         return $this;
415 412
     }
416 413
     /**
@@ -422,7 +419,7 @@  discard block
 block discarded – undo
422 419
     public function limit(int $limit, int $offset = 0, bool $limitOnMainTable = false) : TableQuery
423 420
     {
424 421
         $this->qiterator = null;
425
-        $this->li_of = [ $limit, $offset, $limitOnMainTable ? 1 : 0 ];
422
+        $this->li_of = [$limit, $offset, $limitOnMainTable ? 1 : 0];
426 423
         return $this;
427 424
     }
428 425
     /**
@@ -432,9 +429,9 @@  discard block
 block discarded – undo
432 429
     public function count() : int
433 430
     {
434 431
         $aliases = [];
435
-        $getAlias = function ($name) use (&$aliases) {
432
+        $getAlias = function($name) use (&$aliases) {
436 433
             // to bypass use: return $name;
437
-            return $aliases[$name] = $aliases[$name] ?? 'alias' . static::SEP . count($aliases);
434
+            return $aliases[$name] = $aliases[$name] ?? 'alias'.static::SEP.count($aliases);
438 435
         };
439 436
         $table = $this->definition->getName();
440 437
         $sql = 'SELECT COUNT(DISTINCT '.$table.'.'.implode(', '.$table.'.', $this->pkey).') FROM '.$table.' ';
@@ -449,44 +446,44 @@  discard block
 block discarded – undo
449 446
         $h = $this->having;
450 447
         $o = $this->order;
451 448
         $g = $this->group;
452
-        $j = array_map(function ($v) { return clone $v; }, $this->joins);
449
+        $j = array_map(function($v) { return clone $v; }, $this->joins);
453 450
         foreach ($this->definition->getRelations() as $k => $v) {
454 451
             foreach ($w as $kk => $vv) {
455
-                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv[0])) {
456
-                    $relations[$k] = [ $v, $table ];
457
-                    $w[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vv[0]);
452
+                if (preg_match('(\b'.preg_quote($k.'.').')i', $vv[0])) {
453
+                    $relations[$k] = [$v, $table];
454
+                    $w[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $vv[0]);
458 455
                 }
459 456
             }
460
-            if (isset($o[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $o[0])) {
461
-                $relations[$k] = [ $v, $table ];
457
+            if (isset($o[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $o[0])) {
458
+                $relations[$k] = [$v, $table];
462 459
             }
463 460
             foreach ($h as $kk => $vv) {
464
-                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv[0])) {
465
-                    $relations[$k] = [ $relation, $table ];
466
-                    $h[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vv[0]);
461
+                if (preg_match('(\b'.preg_quote($k.'.').')i', $vv[0])) {
462
+                    $relations[$k] = [$relation, $table];
463
+                    $h[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $vv[0]);
467 464
                 }
468 465
             }
469
-            if (isset($g[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $g[0])) {
470
-                $relations[$k] = [ $relation, $table ];
471
-                $g[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $g[0]);
466
+            if (isset($g[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $g[0])) {
467
+                $relations[$k] = [$relation, $table];
468
+                $g[0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $g[0]);
472 469
             }
473 470
             foreach ($j as $kk => $v) {
474 471
                 foreach ($v->keymap as $kkk => $vv) {
475
-                    if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv)) {
476
-                        $relations[$k] = [ $relation, $table ];
477
-                        $j[$k]->keymap[$kkk] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vv);
472
+                    if (preg_match('(\b'.preg_quote($k.'.').')i', $vv)) {
473
+                        $relations[$k] = [$relation, $table];
474
+                        $j[$k]->keymap[$kkk] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $vv);
478 475
                     }
479 476
                 }
480 477
             }
481 478
         }
482 479
 
483 480
         foreach ($j as $k => $v) {
484
-            $sql .= ($v->many ? 'LEFT ' : '' ) . 'JOIN '.$v->table->getName().' '.$k.' ON ';
481
+            $sql .= ($v->many ? 'LEFT ' : '').'JOIN '.$v->table->getName().' '.$k.' ON ';
485 482
             $tmp = [];
486 483
             foreach ($v->keymap as $kk => $vv) {
487 484
                 $tmp[] = $kk.' = '.$vv;
488 485
             }
489
-            $sql .= implode(' AND ', $tmp) . ' ';
486
+            $sql .= implode(' AND ', $tmp).' ';
490 487
         }
491 488
         foreach ($relations as $k => $v) {
492 489
             $table = $v[1] !== $this->definition->getName() ? $getAlias($v[1]) : $v[1];
@@ -498,13 +495,13 @@  discard block
 block discarded – undo
498 495
                 foreach ($v->keymap as $kk => $vv) {
499 496
                     $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' ';
500 497
                 }
501
-                $sql .= implode(' AND ', $tmp) . ' ';
498
+                $sql .= implode(' AND ', $tmp).' ';
502 499
                 $sql .= 'LEFT JOIN '.$v->table->getName().' '.$getAlias($k).' ON ';
503 500
                 $tmp = [];
504 501
                 foreach ($v->pivot_keymap as $kk => $vv) {
505 502
                     $tmp[] = $getAlias($k).'.'.$vv.' = '.$alias.'.'.$kk.' ';
506 503
                 }
507
-                $sql .= implode(' AND ', $tmp) . ' ';
504
+                $sql .= implode(' AND ', $tmp).' ';
508 505
             } else {
509 506
                 $alias = $getAlias($k);
510 507
                 $sql .= 'LEFT JOIN '.$v->table->getName().' '.$alias.' ON ';
@@ -513,30 +510,30 @@  discard block
 block discarded – undo
513 510
                     $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' ';
514 511
                 }
515 512
                 if ($v->sql) {
516
-                    $tmp[] = $v->sql . ' ';
513
+                    $tmp[] = $v->sql.' ';
517 514
                     $par = array_merge($par, $v->par ?? []);
518 515
                 }
519
-                $sql .= implode(' AND ', $tmp) . ' ';
516
+                $sql .= implode(' AND ', $tmp).' ';
520 517
             }
521 518
         }
522 519
         if (count($w)) {
523 520
             $sql .= 'WHERE ';
524 521
             $tmp = [];
525 522
             foreach ($w as $v) {
526
-                $tmp[] = '(' . $v[0] . ')';
523
+                $tmp[] = '('.$v[0].')';
527 524
                 $par = array_merge($par, $v[1]);
528 525
             }
529 526
             $sql .= implode(' AND ', $tmp).' ';
530 527
         }
531 528
         if (count($g)) {
532
-            $sql .= 'GROUP BY ' . $g[0] . ' ';
529
+            $sql .= 'GROUP BY '.$g[0].' ';
533 530
             $par = array_merge($par, $g[1]);
534 531
         }
535 532
         if (count($h)) {
536 533
             $sql .= 'HAVING ';
537 534
             $tmp = [];
538 535
             foreach ($h as $v) {
539
-                $tmp[] = '(' . $v[0] . ')';
536
+                $tmp[] = '('.$v[0].')';
540 537
                 $par = array_merge($par, $v[1]);
541 538
             }
542 539
             $sql .= implode(' AND ', $tmp).' ';
@@ -570,7 +567,7 @@  discard block
 block discarded – undo
570 567
                     $this->with(implode('.', $temp));
571 568
                     $table = array_reduce(
572 569
                         $temp,
573
-                        function ($carry, $item) use (&$table) {
570
+                        function($carry, $item) use (&$table) {
574 571
                             return $table->getRelation($item)->table;
575 572
                         }
576 573
                     );
@@ -579,7 +576,7 @@  discard block
 block discarded – undo
579 576
                 }
580 577
                 unset($fields[$k]);
581 578
                 foreach ($cols as $col) {
582
-                    $fields[] = $table . '.' . $col;
579
+                    $fields[] = $table.'.'.$col;
583 580
                 }
584 581
             }
585 582
         }
@@ -611,9 +608,9 @@  discard block
 block discarded – undo
611 608
             return $this->qiterator;
612 609
         }
613 610
         $aliases = [];
614
-        $getAlias = function ($name) use (&$aliases) {
611
+        $getAlias = function($name) use (&$aliases) {
615 612
             // to bypass use: return $name;
616
-            return $aliases[$name] = $aliases[$name] ?? 'alias' . static::SEP . count($aliases);
613
+            return $aliases[$name] = $aliases[$name] ?? 'alias'.static::SEP.count($aliases);
617 614
         };
618 615
         $table = $this->definition->getName();
619 616
         if ($fields !== null) {
@@ -629,7 +626,7 @@  discard block
 block discarded – undo
629 626
         $h = $this->having;
630 627
         $o = $this->order;
631 628
         $g = $this->group;
632
-        $j = array_map(function ($v) { return clone $v; }, $this->joins);
629
+        $j = array_map(function($v) { return clone $v; }, $this->joins);
633 630
 
634 631
         $porder = [];
635 632
         foreach ($this->definition->getPrimaryKey() as $field) {
@@ -642,9 +639,9 @@  discard block
 block discarded – undo
642 639
                 if (count($porder) > 1) {
643 640
                     $pkw = [];
644 641
                     foreach ($porder as $name) {
645
-                        $pkw[] = $name . ' = ?';
642
+                        $pkw[] = $name.' = ?';
646 643
                     }
647
-                    $pkw = '(' . implode(' AND ', $pkw) . ')';
644
+                    $pkw = '('.implode(' AND ', $pkw).')';
648 645
                     $pkp = [];
649 646
                     foreach ($ids as $id) {
650 647
                         foreach ($id as $p) {
@@ -656,67 +653,67 @@  discard block
 block discarded – undo
656 653
                         $pkp
657 654
                     ];
658 655
                 } else {
659
-                    $w[] = [ $porder[0] . ' IN ('.implode(',', array_fill(0, count($ids), '?')).')', $ids ];
656
+                    $w[] = [$porder[0].' IN ('.implode(',', array_fill(0, count($ids), '?')).')', $ids];
660 657
                 }
661 658
             } else {
662
-                $w[] = [ '1=0', [] ];
659
+                $w[] = ['1=0', []];
663 660
             }
664 661
         }
665 662
 
666 663
         foreach ($this->definition->getRelations() as $k => $relation) {
667 664
             foreach ($f as $kk => $field) {
668
-                if (strpos($field, $k . '.') === 0) {
669
-                    $relations[$k] = [ $relation, $table ];
670
-                    $f[$kk] = str_replace($k . '.', $getAlias($k) . '.', $field);
665
+                if (strpos($field, $k.'.') === 0) {
666
+                    $relations[$k] = [$relation, $table];
667
+                    $f[$kk] = str_replace($k.'.', $getAlias($k).'.', $field);
671 668
                 }
672 669
             }
673 670
             foreach ($w as $kk => $v) {
674
-                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $v[0])) {
675
-                    $relations[$k] = [ $relation, $table ];
676
-                    $w[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $v[0]);
671
+                if (preg_match('(\b'.preg_quote($k.'.').')i', $v[0])) {
672
+                    $relations[$k] = [$relation, $table];
673
+                    $w[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $v[0]);
677 674
                 }
678 675
             }
679 676
             foreach ($h as $kk => $v) {
680
-                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $v[0])) {
681
-                    $relations[$k] = [ $relation, $table ];
682
-                    $h[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $v[0]);
677
+                if (preg_match('(\b'.preg_quote($k.'.').')i', $v[0])) {
678
+                    $relations[$k] = [$relation, $table];
679
+                    $h[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $v[0]);
683 680
                 }
684 681
             }
685
-            if (isset($o[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $o[0])) {
686
-                $relations[$k] = [ $relation, $table ];
687
-                $o[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $o[0]);
682
+            if (isset($o[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $o[0])) {
683
+                $relations[$k] = [$relation, $table];
684
+                $o[0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $o[0]);
688 685
             }
689
-            if (isset($g[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $g[0])) {
690
-                $relations[$k] = [ $relation, $table ];
691
-                $g[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $g[0]);
686
+            if (isset($g[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $g[0])) {
687
+                $relations[$k] = [$relation, $table];
688
+                $g[0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $g[0]);
692 689
             }
693 690
             foreach ($j as $kk => $v) {
694 691
                 foreach ($v->keymap as $kkk => $vv) {
695
-                    if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv)) {
696
-                        $relations[$k] = [ $relation, $table ];
697
-                        $j[$k]->keymap[$kkk] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vv);
692
+                    if (preg_match('(\b'.preg_quote($k.'.').')i', $vv)) {
693
+                        $relations[$k] = [$relation, $table];
694
+                        $j[$k]->keymap[$kkk] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $vv);
698 695
                     }
699 696
                 }
700 697
             }
701 698
         }
702 699
         $select = [];
703 700
         foreach ($f as $k => $field) {
704
-            $select[] = $field . (!is_numeric($k) ? ' ' . $k : '');
701
+            $select[] = $field.(!is_numeric($k) ? ' '.$k : '');
705 702
         }
706 703
         foreach ($this->withr as $name => $relation) {
707 704
             foreach ($relation[0]->table->getColumns() as $column) {
708
-                $select[] = $getAlias($name) . '.' . $column . ' ' . $getAlias($name . static::SEP . $column);
705
+                $select[] = $getAlias($name).'.'.$column.' '.$getAlias($name.static::SEP.$column);
709 706
             }
710 707
         }
711 708
         $sql = 'SELECT '.implode(', ', $select).' FROM '.$table.' ';
712 709
         $par = [];
713 710
         foreach ($j as $k => $v) {
714
-            $sql .= ($v->many ? 'LEFT ' : '' ) . 'JOIN '.$v->table->getName().' '.$k.' ON ';
711
+            $sql .= ($v->many ? 'LEFT ' : '').'JOIN '.$v->table->getName().' '.$k.' ON ';
715 712
             $tmp = [];
716 713
             foreach ($v->keymap as $kk => $vv) {
717 714
                 $tmp[] = $kk.' = '.$vv;
718 715
             }
719
-            $sql .= implode(' AND ', $tmp) . ' ';
716
+            $sql .= implode(' AND ', $tmp).' ';
720 717
         }
721 718
         foreach ($relations as $relation => $v) {
722 719
             $table = $v[1] !== $this->definition->getName() ? $getAlias($v[1]) : $v[1];
@@ -728,13 +725,13 @@  discard block
 block discarded – undo
728 725
                 foreach ($v->keymap as $kk => $vv) {
729 726
                     $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' ';
730 727
                 }
731
-                $sql .= implode(' AND ', $tmp) . ' ';
728
+                $sql .= implode(' AND ', $tmp).' ';
732 729
                 $sql .= 'LEFT JOIN '.$v->table->getName().' '.$getAlias($relation).' ON ';
733 730
                 $tmp = [];
734 731
                 foreach ($v->pivot_keymap as $kk => $vv) {
735 732
                     $tmp[] = $getAlias($relation).'.'.$vv.' = '.$alias.'.'.$kk.' ';
736 733
                 }
737
-                $sql .= implode(' AND ', $tmp) . ' ';
734
+                $sql .= implode(' AND ', $tmp).' ';
738 735
             } else {
739 736
                 $alias = $getAlias($relation);
740 737
 
@@ -744,62 +741,62 @@  discard block
 block discarded – undo
744 741
                     $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' ';
745 742
                 }
746 743
                 if ($v->sql) {
747
-                    $tmp[] = $v->sql . ' ';
744
+                    $tmp[] = $v->sql.' ';
748 745
                     $par = array_merge($par, $v->par ?? []);
749 746
                 }
750
-                $sql .= implode(' AND ', $tmp) . ' ';
747
+                $sql .= implode(' AND ', $tmp).' ';
751 748
             }
752 749
         }
753 750
         if (count($w)) {
754 751
             $sql .= 'WHERE ';
755 752
             $tmp = [];
756 753
             foreach ($w as $v) {
757
-                $tmp[] = '(' . $v[0] . ')';
754
+                $tmp[] = '('.$v[0].')';
758 755
                 $par = array_merge($par, $v[1]);
759 756
             }
760 757
             $sql .= implode(' AND ', $tmp).' ';
761 758
         }
762 759
         if (count($g)) {
763
-            $sql .= 'GROUP BY ' . $g[0] . ' ';
760
+            $sql .= 'GROUP BY '.$g[0].' ';
764 761
             $par = array_merge($par, $g[1]);
765 762
         }
766 763
         if (count($h)) {
767 764
             $sql .= 'HAVING ';
768 765
             $tmp = [];
769 766
             foreach ($h as $v) {
770
-                $tmp[] = '(' . $v[0] . ')';
767
+                $tmp[] = '('.$v[0].')';
771 768
                 $par = array_merge($par, $v[1]);
772 769
             }
773 770
             $sql .= implode(' AND ', $tmp).' ';
774 771
         }
775 772
         if (count($o)) {
776
-            $sql .= 'ORDER BY ' . $o[0] . ' ';
773
+            $sql .= 'ORDER BY '.$o[0].' ';
777 774
             $par = array_merge($par, $o[1]);
778 775
         }
779 776
         if (count($porder)) {
780
-            $sql .= (count($o) ? ', ' : 'ORDER BY ') . implode(', ', $porder) . ' ';
777
+            $sql .= (count($o) ? ', ' : 'ORDER BY ').implode(', ', $porder).' ';
781 778
         }
782 779
         if (($this->li_of[2] === 0 || !count($porder)) && $this->li_of[0]) {
783 780
             if ($this->db->driverName() === 'oracle') {
784
-                if ((int)$this->db->driverOption('version', 0) >= 12) {
785
-                    $sql .= 'OFFSET ' . $this->li_of[1] . ' ROWS FETCH NEXT ' . $this->li_of[0] . ' ROWS ONLY';
781
+                if ((int) $this->db->driverOption('version', 0) >= 12) {
782
+                    $sql .= 'OFFSET '.$this->li_of[1].' ROWS FETCH NEXT '.$this->li_of[0].' ROWS ONLY';
786 783
                 } else {
787
-                    $f = array_map(function ($v) {
784
+                    $f = array_map(function($v) {
788 785
                         $v = explode(' ', trim($v), 2);
789 786
                         if (count($v) === 2) { return $v[1]; }
790 787
                         $v = explode('.', $v[0], 2);
791 788
                         return count($v) === 2 ? $v[1] : $v[0];
792 789
                     }, $select);
793
-                    $sql = "SELECT " . implode(', ', $f) . " 
790
+                    $sql = "SELECT ".implode(', ', $f)." 
794 791
                             FROM (
795 792
                                 SELECT tbl__.*, rownum rnum__ FROM (
796
-                                    " . $sql . "
793
+                                    " . $sql."
797 794
                                 ) tbl__ 
798
-                                WHERE rownum <= " . ($this->li_of[0] + $this->li_of[1]) . "
795
+                                WHERE rownum <= " . ($this->li_of[0] + $this->li_of[1])."
799 796
                             ) WHERE rnum__ > " . $this->li_of[1];
800 797
                 }
801 798
             } else {
802
-                $sql .= 'LIMIT ' . $this->li_of[0] . ' OFFSET ' . $this->li_of[1];
799
+                $sql .= 'LIMIT '.$this->li_of[0].' OFFSET '.$this->li_of[1];
803 800
             }
804 801
         }
805 802
         return $this->qiterator = new TableQueryIterator(
@@ -849,7 +846,7 @@  discard block
 block discarded – undo
849 846
                 $ret[$k] = str_repeat(' ', 255);
850 847
                 $par[] = &$ret[$k];
851 848
             }
852
-            $sql .= ' RETURNING ' . implode(',', $primary) . ' INTO ' . implode(',', array_fill(0, count($primary), '?'));
849
+            $sql .= ' RETURNING '.implode(',', $primary).' INTO '.implode(',', array_fill(0, count($primary), '?'));
853 850
             $this->db->query($sql, $par);
854 851
             return $ret;
855 852
         } else {
@@ -881,7 +878,7 @@  discard block
 block discarded – undo
881 878
         }
882 879
         $sql = 'UPDATE '.$table.' SET ';
883 880
         $par = [];
884
-        $sql .= implode(', ', array_map(function ($v) { return $v . ' = ?'; }, array_keys($update))) . ' ';
881
+        $sql .= implode(', ', array_map(function($v) { return $v.' = ?'; }, array_keys($update))).' ';
885 882
         $par = array_merge($par, array_values($update));
886 883
         if (count($this->where)) {
887 884
             $sql .= 'WHERE ';
@@ -890,7 +887,7 @@  discard block
 block discarded – undo
890 887
                 $tmp[] = $v[0];
891 888
                 $par = array_merge($par, $v[1]);
892 889
             }
893
-            $sql .= implode(' AND ', $tmp) . ' ';
890
+            $sql .= implode(' AND ', $tmp).' ';
894 891
         }
895 892
         if (count($this->order)) {
896 893
             $sql .= $this->order[0];
@@ -914,7 +911,7 @@  discard block
 block discarded – undo
914 911
                 $tmp[] = $v[0];
915 912
                 $par = array_merge($par, $v[1]);
916 913
             }
917
-            $sql .= implode(' AND ', $tmp) . ' ';
914
+            $sql .= implode(' AND ', $tmp).' ';
918 915
         }
919 916
         if (count($this->order)) {
920 917
             $sql .= $this->order[0];
@@ -934,13 +931,13 @@  discard block
 block discarded – undo
934 931
         $table = $this->definition;
935 932
         array_reduce(
936 933
             $parts,
937
-            function ($carry, $item) use (&$table) {
934
+            function($carry, $item) use (&$table) {
938 935
                 $relation = $table->getRelation($item);
939 936
                 if (!$relation) {
940 937
                     throw new DBException('Invalid relation name');
941 938
                 }
942
-                $name = $carry ? $carry . static::SEP . $item : $item;
943
-                $this->withr[$name] = [ $relation, $carry ?? $table->getName() ];
939
+                $name = $carry ? $carry.static::SEP.$item : $item;
940
+                $this->withr[$name] = [$relation, $carry ?? $table->getName()];
944 941
                 $table = $relation->table;
945 942
                 return $name;
946 943
             }
@@ -978,9 +975,9 @@  discard block
 block discarded – undo
978 975
     public function ids()
979 976
     {
980 977
         $aliases = [];
981
-        $getAlias = function ($name) use (&$aliases) {
978
+        $getAlias = function($name) use (&$aliases) {
982 979
             // to bypass use: return $name;
983
-            return $aliases[$name] = $aliases[$name] ?? 'alias' . static::SEP . count($aliases);
980
+            return $aliases[$name] = $aliases[$name] ?? 'alias'.static::SEP.count($aliases);
984 981
         };
985 982
         $table = $this->definition->getName();
986 983
         $sql = 'SELECT DISTINCT '.$table.'.'.implode(', '.$table.'.', $this->pkey).' FROM '.$table.' ';
@@ -995,45 +992,45 @@  discard block
 block discarded – undo
995 992
         $h = $this->having;
996 993
         $o = $this->order;
997 994
         $g = $this->group;
998
-        $j = array_map(function ($v) { return clone $v; }, $this->joins);
995
+        $j = array_map(function($v) { return clone $v; }, $this->joins);
999 996
         foreach ($this->definition->getRelations() as $k => $v) {
1000 997
             foreach ($w as $kk => $vv) {
1001
-                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv[0])) {
1002
-                    $relations[$k] = [ $v, $table ];
1003
-                    $w[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vv[0]);
998
+                if (preg_match('(\b'.preg_quote($k.'.').')i', $vv[0])) {
999
+                    $relations[$k] = [$v, $table];
1000
+                    $w[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $vv[0]);
1004 1001
                 }
1005 1002
             }
1006
-            if (isset($o[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $o[0])) {
1007
-                $relations[$k] = [ $v, $table ];
1008
-                $o[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $o[0]);
1003
+            if (isset($o[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $o[0])) {
1004
+                $relations[$k] = [$v, $table];
1005
+                $o[0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $o[0]);
1009 1006
             }
1010 1007
             foreach ($h as $kk => $vv) {
1011
-                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv[0])) {
1012
-                    $relations[$k] = [ $relation, $table ];
1013
-                    $h[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vv[0]);
1008
+                if (preg_match('(\b'.preg_quote($k.'.').')i', $vv[0])) {
1009
+                    $relations[$k] = [$relation, $table];
1010
+                    $h[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $vv[0]);
1014 1011
                 }
1015 1012
             }
1016
-            if (isset($g[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $g[0])) {
1017
-                $relations[$k] = [ $relation, $table ];
1018
-                $g[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $g[0]);
1013
+            if (isset($g[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $g[0])) {
1014
+                $relations[$k] = [$relation, $table];
1015
+                $g[0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $g[0]);
1019 1016
             }
1020 1017
             foreach ($j as $kk => $v) {
1021 1018
                 foreach ($v->keymap as $kkk => $vv) {
1022
-                    if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv)) {
1023
-                        $relations[$k] = [ $relation, $table ];
1024
-                        $j[$k]->keymap[$kkk] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vv);
1019
+                    if (preg_match('(\b'.preg_quote($k.'.').')i', $vv)) {
1020
+                        $relations[$k] = [$relation, $table];
1021
+                        $j[$k]->keymap[$kkk] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $vv);
1025 1022
                     }
1026 1023
                 }
1027 1024
             }
1028 1025
         }
1029 1026
 
1030 1027
         foreach ($j as $k => $v) {
1031
-            $sql .= ($v->many ? 'LEFT ' : '' ) . 'JOIN '.$v->table->getName().' '.$k.' ON ';
1028
+            $sql .= ($v->many ? 'LEFT ' : '').'JOIN '.$v->table->getName().' '.$k.' ON ';
1032 1029
             $tmp = [];
1033 1030
             foreach ($v->keymap as $kk => $vv) {
1034 1031
                 $tmp[] = $kk.' = '.$vv;
1035 1032
             }
1036
-            $sql .= implode(' AND ', $tmp) . ' ';
1033
+            $sql .= implode(' AND ', $tmp).' ';
1037 1034
         }
1038 1035
         foreach ($relations as $k => $v) {
1039 1036
             $table = $v[1] !== $this->definition->getName() ? $getAlias($v[1]) : $v[1];
@@ -1045,13 +1042,13 @@  discard block
 block discarded – undo
1045 1042
                 foreach ($v->keymap as $kk => $vv) {
1046 1043
                     $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' ';
1047 1044
                 }
1048
-                $sql .= implode(' AND ', $tmp) . ' ';
1045
+                $sql .= implode(' AND ', $tmp).' ';
1049 1046
                 $sql .= 'LEFT JOIN '.$v->table->getName().' '.$getAlias($k).' ON ';
1050 1047
                 $tmp = [];
1051 1048
                 foreach ($v->pivot_keymap as $kk => $vv) {
1052 1049
                     $tmp[] = $getAlias($k).'.'.$vv.' = '.$alias.'.'.$kk.' ';
1053 1050
                 }
1054
-                $sql .= implode(' AND ', $tmp) . ' ';
1051
+                $sql .= implode(' AND ', $tmp).' ';
1055 1052
             } else {
1056 1053
                 $alias = $getAlias($k);
1057 1054
                 $sql .= 'LEFT JOIN '.$v->table->getName().' '.$alias.' ON ';
@@ -1060,36 +1057,36 @@  discard block
 block discarded – undo
1060 1057
                     $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' ';
1061 1058
                 }
1062 1059
                 if ($v->sql) {
1063
-                    $tmp[] = $v->sql . ' ';
1060
+                    $tmp[] = $v->sql.' ';
1064 1061
                     $par = array_merge($par, $v->par ?? []);
1065 1062
                 }
1066
-                $sql .= implode(' AND ', $tmp) . ' ';
1063
+                $sql .= implode(' AND ', $tmp).' ';
1067 1064
             }
1068 1065
         }
1069 1066
         if (count($w)) {
1070 1067
             $sql .= 'WHERE ';
1071 1068
             $tmp = [];
1072 1069
             foreach ($w as $v) {
1073
-                $tmp[] = '(' . $v[0] . ')';
1070
+                $tmp[] = '('.$v[0].')';
1074 1071
                 $par = array_merge($par, $v[1]);
1075 1072
             }
1076 1073
             $sql .= implode(' AND ', $tmp).' ';
1077 1074
         }
1078 1075
         if (count($g)) {
1079
-            $sql .= 'GROUP BY ' . $g[0] . ' ';
1076
+            $sql .= 'GROUP BY '.$g[0].' ';
1080 1077
             $par = array_merge($par, $g[1]);
1081 1078
         }
1082 1079
         if (count($h)) {
1083 1080
             $sql .= 'HAVING ';
1084 1081
             $tmp = [];
1085 1082
             foreach ($h as $v) {
1086
-                $tmp[] = '(' . $v[0] . ')';
1083
+                $tmp[] = '('.$v[0].')';
1087 1084
                 $par = array_merge($par, $v[1]);
1088 1085
             }
1089 1086
             $sql .= implode(' AND ', $tmp).' ';
1090 1087
         }
1091 1088
         if (count($o)) {
1092
-            $sql .= 'ORDER BY ' . $o[0] . ' ';
1089
+            $sql .= 'ORDER BY '.$o[0].' ';
1093 1090
             $par = array_merge($par, $o[1]);
1094 1091
         }
1095 1092
         $porder = [];
@@ -1097,30 +1094,30 @@  discard block
 block discarded – undo
1097 1094
             $porder[] = $this->getColumn($field)['name'];
1098 1095
         }
1099 1096
         if (count($porder)) {
1100
-            $sql .= (count($o) ? ', ' : 'ORDER BY ') . implode(', ', $porder) . ' ';
1097
+            $sql .= (count($o) ? ', ' : 'ORDER BY ').implode(', ', $porder).' ';
1101 1098
         }
1102 1099
 
1103 1100
         if ($this->li_of[0]) {
1104 1101
             if ($this->db->driverName() === 'oracle') {
1105
-                if ((int)$this->db->driverOption('version', 0) >= 12) {
1106
-                    $sql .= 'OFFSET ' . $this->li_of[1] . ' ROWS FETCH NEXT ' . $this->li_of[0] . ' ROWS ONLY';
1102
+                if ((int) $this->db->driverOption('version', 0) >= 12) {
1103
+                    $sql .= 'OFFSET '.$this->li_of[1].' ROWS FETCH NEXT '.$this->li_of[0].' ROWS ONLY';
1107 1104
                 } else {
1108
-                    $f = array_map(function ($v) {
1105
+                    $f = array_map(function($v) {
1109 1106
                         $v = explode(' ', trim($v), 2);
1110 1107
                         if (count($v) === 2) { return $v[1]; }
1111 1108
                         $v = explode('.', $v[0], 2);
1112 1109
                         return count($v) === 2 ? $v[1] : $v[0];
1113 1110
                     }, $select);
1114
-                    $sql = "SELECT " . implode(', ', $f) . " 
1111
+                    $sql = "SELECT ".implode(', ', $f)." 
1115 1112
                             FROM (
1116 1113
                                 SELECT tbl__.*, rownum rnum__ FROM (
1117
-                                    " . $sql . "
1114
+                                    " . $sql."
1118 1115
                                 ) tbl__ 
1119
-                                WHERE rownum <= " . ($this->li_of[0] + $this->li_of[1]) . "
1116
+                                WHERE rownum <= " . ($this->li_of[0] + $this->li_of[1])."
1120 1117
                             ) WHERE rnum__ > " . $this->li_of[1];
1121 1118
                 }
1122 1119
             } else {
1123
-                $sql .= 'LIMIT ' . $this->li_of[0] . ' OFFSET ' . $this->li_of[1];
1120
+                $sql .= 'LIMIT '.$this->li_of[0].' OFFSET '.$this->li_of[1];
1124 1121
             }
1125 1122
         }
1126 1123
         return $this->db->all($sql, $par);
Please login to merge, or discard this patch.