Completed
Push — master ( 9315b5...f215bc )
by Ivan
04:32
created
src/schema/TableQuery.php 1 patch
Spacing   +177 added lines, -181 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,19 +129,19 @@  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
     {
144
-        $strict = (int)$this->db->driverOption('strict', 0) > 0;
144
+        $strict = (int) $this->db->driverOption('strict', 0) > 0;
145 145
         if ($value === null && $col->isNullable()) {
146 146
             return null;
147 147
         }
@@ -151,7 +151,7 @@  discard block
 block discarded – undo
151 151
                     $temp = strtotime($value);
152 152
                     if (!$temp) {
153 153
                         if ($strict) {
154
-                            throw new DBException('Invalid value for date column ' . $col->getName());
154
+                            throw new DBException('Invalid value for date column '.$col->getName());
155 155
                         }
156 156
                         return null;
157 157
                     }
@@ -164,7 +164,7 @@  discard block
 block discarded – undo
164 164
                     return $value->format('Y-m-d');
165 165
                 }
166 166
                 if ($strict) {
167
-                    throw new DBException('Invalid value (unknown data type) for date column ' . $col->getName());
167
+                    throw new DBException('Invalid value (unknown data type) for date column '.$col->getName());
168 168
                 }
169 169
                 return $value;
170 170
             case 'datetime':
@@ -172,7 +172,7 @@  discard block
 block discarded – undo
172 172
                     $temp = strtotime($value);
173 173
                     if (!$temp) {
174 174
                         if ($strict) {
175
-                            throw new DBException('Invalid value for datetime column ' . $col->getName());
175
+                            throw new DBException('Invalid value for datetime column '.$col->getName());
176 176
                         }
177 177
                         return null;
178 178
                     }
@@ -185,7 +185,7 @@  discard block
 block discarded – undo
185 185
                     return $value->format('Y-m-d H:i:s');
186 186
                 }
187 187
                 if ($strict) {
188
-                    throw new DBException('Invalid value (unknown data type) for datetime column ' . $col->getName());
188
+                    throw new DBException('Invalid value (unknown data type) for datetime column '.$col->getName());
189 189
                 }
190 190
                 return $value;
191 191
             case 'enum':
@@ -193,7 +193,7 @@  discard block
 block discarded – undo
193 193
                 if (is_int($value)) {
194 194
                     if (!isset($values[$value])) {
195 195
                         if ($strict) {
196
-                            throw new DBException('Invalid value (using integer) for enum ' . $col->getName());
196
+                            throw new DBException('Invalid value (using integer) for enum '.$col->getName());
197 197
                         }
198 198
                         return $value;
199 199
                     }
@@ -201,21 +201,21 @@  discard block
 block discarded – undo
201 201
                 }
202 202
                 if (!in_array($value, $col->getValues())) {
203 203
                     if ($strict) {
204
-                        throw new DBException('Invalid value for enum ' . $col->getName());
204
+                        throw new DBException('Invalid value for enum '.$col->getName());
205 205
                     }
206 206
                     return 0;
207 207
                 }
208 208
                 return $value;
209 209
             case 'int':
210
-                return (int)preg_replace('([^+\-0-9]+)', '', $value);
210
+                return (int) preg_replace('([^+\-0-9]+)', '', $value);
211 211
             case 'float':
212
-                return (float)preg_replace('([^+\-0-9.]+)', '', str_replace(',', '.', $value));
212
+                return (float) preg_replace('([^+\-0-9.]+)', '', str_replace(',', '.', $value));
213 213
             case 'text':
214 214
                 // check using strlen first, in order to avoid hitting mb_ functions which might be polyfilled
215 215
                 // because the polyfill is quite slow
216 216
                 if ($col->hasLength() && strlen($value) > $col->getLength() && mb_strlen($value) > $col->getLength()) {
217 217
                     if ($strict) {
218
-                        throw new DBException('Invalid value for text column ' . $col->getName());
218
+                        throw new DBException('Invalid value for text column '.$col->getName());
219 219
                     }
220 220
                     return mb_substr($value, 0, $col->getLength());
221 221
                 }
@@ -237,35 +237,32 @@  discard block
 block discarded – undo
237 237
             // str_replace(['%', '_'], ['\\%','\\_'], $q)
238 238
             return $negate ?
239 239
                 [
240
-                    $name . ' NOT LIKE ?',
241
-                    [ $this->normalizeValue($column, $value) ]
242
-                ] :
243
-                [
244
-                    $name . ' LIKE ?',
245
-                    [ $this->normalizeValue($column, $value) ]
240
+                    $name.' NOT LIKE ?',
241
+                    [$this->normalizeValue($column, $value)]
242
+                ] : [
243
+                    $name.' LIKE ?',
244
+                    [$this->normalizeValue($column, $value)]
246 245
                 ];
247 246
         }
248 247
         if (is_null($value)) {
249 248
             return $negate ?
250
-                [ $name . ' IS NOT NULL', [] ]:
251
-                [ $name . ' IS NULL', [] ];
249
+                [$name.' IS NOT NULL', []] : [$name.' IS NULL', []];
252 250
         }
253 251
         if (!is_array($value)) {
254 252
             return $negate ?
255 253
                 [
256
-                    $name . ' <> ?',
257
-                    [ $this->normalizeValue($column, $value) ]
258
-                ] :
259
-                [
260
-                    $name . ' = ?',
261
-                    [ $this->normalizeValue($column, $value) ]
254
+                    $name.' <> ?',
255
+                    [$this->normalizeValue($column, $value)]
256
+                ] : [
257
+                    $name.' = ?',
258
+                    [$this->normalizeValue($column, $value)]
262 259
                 ];
263 260
         }
264 261
         if (isset($value['beg']) && strlen($value['beg']) && (!isset($value['end']) || !strlen($value['end']))) {
265
-            $value = [ 'gte' => $value['beg'] ];
262
+            $value = ['gte' => $value['beg']];
266 263
         }
267 264
         if (isset($value['end']) && strlen($value['end']) && (!isset($value['beg']) || !strlen($value['beg']))) {
268
-            $value = [ 'lte' => $value['end'] ];
265
+            $value = ['lte' => $value['end']];
269 266
         }
270 267
         if (isset($value['beg']) && isset($value['end'])) {
271 268
             return $negate ?
@@ -275,8 +272,7 @@  discard block
 block discarded – undo
275 272
                         $this->normalizeValue($column, $value['beg']),
276 273
                         $this->normalizeValue($column, $value['end'])
277 274
                     ]
278
-                ] :
279
-                [
275
+                ] : [
280 276
                     $name.' BETWEEN ? AND ?',
281 277
                     [
282 278
                         $this->normalizeValue($column, $value['beg']),
@@ -288,34 +284,34 @@  discard block
 block discarded – undo
288 284
             $sql = [];
289 285
             $par = [];
290 286
             if (isset($value['gt'])) {
291
-                $sql[] = $name. ' ' . ($negate ? '<=' : '>') . ' ?';
287
+                $sql[] = $name.' '.($negate ? '<=' : '>').' ?';
292 288
                 $par[] = $this->normalizeValue($column, $value['gt']);
293 289
             }
294 290
             if (isset($value['gte'])) {
295
-                $sql[] = $name. ' ' . ($negate ? '<' : '>=') . ' ?';
291
+                $sql[] = $name.' '.($negate ? '<' : '>=').' ?';
296 292
                 $par[] = $this->normalizeValue($column, $value['gte']);
297 293
             }
298 294
             if (isset($value['lt'])) {
299
-                $sql[] = $name. ' ' . ($negate ? '>=' : '<') . ' ?';
295
+                $sql[] = $name.' '.($negate ? '>=' : '<').' ?';
300 296
                 $par[] = $this->normalizeValue($column, $value['lt']);
301 297
             }
302 298
             if (isset($value['lte'])) {
303
-                $sql[] = $name. ' ' . ($negate ? '>' : '<=') . ' ?';
299
+                $sql[] = $name.' '.($negate ? '>' : '<=').' ?';
304 300
                 $par[] = $this->normalizeValue($column, $value['lte']);
305 301
             }
306 302
             return [
307
-                '(' . implode(' AND ', $sql) . ')',
303
+                '('.implode(' AND ', $sql).')',
308 304
                 $par
309 305
             ];
310 306
         }
311 307
         return $negate ?
312 308
             [
313
-                $name . ' NOT IN (??)',
314
-                [ array_map(function ($v) use ($column) { return $this->normalizeValue($column, $v); }, $value) ]
309
+                $name.' NOT IN (??)',
310
+                [array_map(function($v) use ($column) { return $this->normalizeValue($column, $v); }, $value)]
315 311
             ] :
316 312
             [
317
-                $name . ' IN (??)',
318
-                [ array_map(function ($v) use ($column) { return $this->normalizeValue($column, $v); }, $value) ]
313
+                $name.' IN (??)',
314
+                [array_map(function($v) use ($column) { return $this->normalizeValue($column, $v); }, $value)]
319 315
             ];
320 316
     }
321 317
     /**
@@ -346,7 +342,7 @@  discard block
 block discarded – undo
346 342
                 $par = array_merge($par, $temp[1]);
347 343
             }
348 344
         }
349
-        return $this->where('(' . implode(' OR ', $sql) . ')', $par);
345
+        return $this->where('('.implode(' OR ', $sql).')', $par);
350 346
     }
351 347
     /**
352 348
      * Filter the results matching all of the criteria
@@ -364,7 +360,7 @@  discard block
 block discarded – undo
364 360
                 $par = array_merge($par, $temp[1]);
365 361
             }
366 362
         }
367
-        return $this->where('(' . implode(' AND ', $sql) . ')', $par);
363
+        return $this->where('('.implode(' AND ', $sql).')', $par);
368 364
     }
369 365
     /**
370 366
      * Sort by a column
@@ -374,7 +370,7 @@  discard block
 block discarded – undo
374 370
      */
375 371
     public function sort(string $column, bool $desc = false) : self
376 372
     {
377
-        return $this->order($this->getColumn($column)['name'] . ' ' . ($desc ? 'DESC' : 'ASC'));
373
+        return $this->order($this->getColumn($column)['name'].' '.($desc ? 'DESC' : 'ASC'));
378 374
     }
379 375
     /**
380 376
      * Group by a column (or columns)
@@ -384,7 +380,7 @@  discard block
 block discarded – undo
384 380
     public function group($column) : self
385 381
     {
386 382
         if (!is_array($column)) {
387
-            $column = [ $column ];
383
+            $column = [$column];
388 384
         }
389 385
         foreach ($column as $k => $v) {
390 386
             $column[$k] = $this->getColumn($v)['name'];
@@ -426,7 +422,7 @@  discard block
 block discarded – undo
426 422
         $this->order = [];
427 423
         $this->having = [];
428 424
         $this->aliases = [];
429
-        $this->li_of = [0,0,0];
425
+        $this->li_of = [0, 0, 0];
430 426
         $this->qiterator = null;
431 427
         return $this;
432 428
     }
@@ -439,7 +435,7 @@  discard block
 block discarded – undo
439 435
     public function groupBy(string $sql, array $params = []) : self
440 436
     {
441 437
         $this->qiterator = null;
442
-        $this->group = [ $sql, $params ];
438
+        $this->group = [$sql, $params];
443 439
         return $this;
444 440
     }
445 441
     /**
@@ -452,7 +448,7 @@  discard block
 block discarded – undo
452 448
      */
453 449
     public function join($table, array $fields, string $name = null, bool $multiple = true)
454 450
     {
455
-        $table = $table instanceof Table ? $table : $this->db->definition((string)$table);
451
+        $table = $table instanceof Table ? $table : $this->db->definition((string) $table);
456 452
         $name = $name ?? $table->getName();
457 453
         if (isset($this->joins[$name]) || $this->definition->hasRelation($name)) {
458 454
             throw new DBException('Alias / table name already in use');
@@ -461,7 +457,7 @@  discard block
 block discarded – undo
461 457
         foreach ($fields as $k => $v) {
462 458
             $k = explode('.', $k, 2);
463 459
             $k = count($k) == 2 ? $k[1] : $k[0];
464
-            $this->joins[$name]->keymap[$this->getColumn($name . '.' . $k)['name']] = $this->getColumn($v)['name'];
460
+            $this->joins[$name]->keymap[$this->getColumn($name.'.'.$k)['name']] = $this->getColumn($v)['name'];
465 461
         }
466 462
         return $this;
467 463
     }
@@ -474,7 +470,7 @@  discard block
 block discarded – undo
474 470
     public function where(string $sql, array $params = []) : self
475 471
     {
476 472
         $this->qiterator = null;
477
-        $this->where[] = [ $sql, $params ];
473
+        $this->where[] = [$sql, $params];
478 474
         return $this;
479 475
     }
480 476
     /**
@@ -486,7 +482,7 @@  discard block
 block discarded – undo
486 482
     public function having(string $sql, array $params = []) : self
487 483
     {
488 484
         $this->qiterator = null;
489
-        $this->having[] = [ $sql, $params ];
485
+        $this->having[] = [$sql, $params];
490 486
         return $this;
491 487
     }
492 488
     /**
@@ -507,7 +503,7 @@  discard block
 block discarded – undo
507 503
                 $name = null;
508 504
             }
509 505
         }
510
-        $this->order = [ $sql, $params, $name ];
506
+        $this->order = [$sql, $params, $name];
511 507
         return $this;
512 508
     }
513 509
     /**
@@ -519,7 +515,7 @@  discard block
 block discarded – undo
519 515
     public function limit(int $limit, int $offset = 0, bool $limitOnMainTable = false) : self
520 516
     {
521 517
         $this->qiterator = null;
522
-        $this->li_of = [ $limit, $offset, $limitOnMainTable ? 1 : 0 ];
518
+        $this->li_of = [$limit, $offset, $limitOnMainTable ? 1 : 0];
523 519
         return $this;
524 520
     }
525 521
     /**
@@ -529,9 +525,9 @@  discard block
 block discarded – undo
529 525
     public function count() : int
530 526
     {
531 527
         $aliases = [];
532
-        $getAlias = function ($name) use (&$aliases) {
528
+        $getAlias = function($name) use (&$aliases) {
533 529
             // to bypass use: return $name;
534
-            return $aliases[$name] = $aliases[$name] ?? 'alias' . static::SEP . count($aliases);
530
+            return $aliases[$name] = $aliases[$name] ?? 'alias'.static::SEP.count($aliases);
535 531
         };
536 532
         $table = $this->definition->getName();
537 533
         $sql = 'SELECT COUNT(DISTINCT '.$table.'.'.implode(', '.$table.'.', $this->pkey).') FROM '.$table.' ';
@@ -545,44 +541,44 @@  discard block
 block discarded – undo
545 541
         $h = $this->having;
546 542
         $o = $this->order;
547 543
         $g = $this->group;
548
-        $j = array_map(function ($v) { return clone $v; }, $this->joins);
544
+        $j = array_map(function($v) { return clone $v; }, $this->joins);
549 545
         foreach ($this->definition->getRelations() as $k => $v) {
550 546
             foreach ($w as $kk => $vv) {
551
-                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv[0])) {
552
-                    $relations[$k] = [ $v, $table ];
553
-                    $w[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vv[0]);
547
+                if (preg_match('(\b'.preg_quote($k.'.').')i', $vv[0])) {
548
+                    $relations[$k] = [$v, $table];
549
+                    $w[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $vv[0]);
554 550
                 }
555 551
             }
556
-            if (isset($o[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $o[0])) {
557
-                $relations[$k] = [ $v, $table ];
552
+            if (isset($o[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $o[0])) {
553
+                $relations[$k] = [$v, $table];
558 554
             }
559 555
             foreach ($h as $kk => $vv) {
560
-                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv[0])) {
561
-                    $relations[$k] = [ $v, $table ];
562
-                    $h[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vv[0]);
556
+                if (preg_match('(\b'.preg_quote($k.'.').')i', $vv[0])) {
557
+                    $relations[$k] = [$v, $table];
558
+                    $h[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $vv[0]);
563 559
                 }
564 560
             }
565
-            if (isset($g[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $g[0])) {
566
-                $relations[$k] = [ $v, $table ];
567
-                $g[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $g[0]);
561
+            if (isset($g[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $g[0])) {
562
+                $relations[$k] = [$v, $table];
563
+                $g[0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $g[0]);
568 564
             }
569 565
             foreach ($j as $kk => $vv) {
570 566
                 foreach ($vv->keymap as $kkk => $vvv) {
571
-                    if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vvv)) {
572
-                        $relations[$k] = [ $v, $table ];
573
-                        $j[$kk]->keymap[$kkk] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vvv);
567
+                    if (preg_match('(\b'.preg_quote($k.'.').')i', $vvv)) {
568
+                        $relations[$k] = [$v, $table];
569
+                        $j[$kk]->keymap[$kkk] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $vvv);
574 570
                     }
575 571
                 }
576 572
             }
577 573
         }
578 574
 
579 575
         foreach ($j as $k => $v) {
580
-            $sql .= ($v->many ? 'LEFT ' : '' ) . 'JOIN '.$v->table->getName().' '.$k.' ON ';
576
+            $sql .= ($v->many ? 'LEFT ' : '').'JOIN '.$v->table->getName().' '.$k.' ON ';
581 577
             $tmp = [];
582 578
             foreach ($v->keymap as $kk => $vv) {
583 579
                 $tmp[] = $kk.' = '.$vv;
584 580
             }
585
-            $sql .= implode(' AND ', $tmp) . ' ';
581
+            $sql .= implode(' AND ', $tmp).' ';
586 582
         }
587 583
         foreach ($relations as $k => $v) {
588 584
             $table = $v[1] !== $this->definition->getName() ? $getAlias($v[1]) : $v[1];
@@ -594,13 +590,13 @@  discard block
 block discarded – undo
594 590
                 foreach ($v->keymap as $kk => $vv) {
595 591
                     $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' ';
596 592
                 }
597
-                $sql .= implode(' AND ', $tmp) . ' ';
593
+                $sql .= implode(' AND ', $tmp).' ';
598 594
                 $sql .= 'LEFT JOIN '.$v->table->getName().' '.$getAlias($k).' ON ';
599 595
                 $tmp = [];
600 596
                 foreach ($v->pivot_keymap as $kk => $vv) {
601 597
                     $tmp[] = $getAlias($k).'.'.$vv.' = '.$alias.'.'.$kk.' ';
602 598
                 }
603
-                $sql .= implode(' AND ', $tmp) . ' ';
599
+                $sql .= implode(' AND ', $tmp).' ';
604 600
             } else {
605 601
                 $alias = $getAlias($k);
606 602
                 $sql .= 'LEFT JOIN '.$v->table->getName().' '.$alias.' ON ';
@@ -609,30 +605,30 @@  discard block
 block discarded – undo
609 605
                     $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' ';
610 606
                 }
611 607
                 if ($v->sql) {
612
-                    $tmp[] = $v->sql . ' ';
608
+                    $tmp[] = $v->sql.' ';
613 609
                     $par = array_merge($par, $v->par ?? []);
614 610
                 }
615
-                $sql .= implode(' AND ', $tmp) . ' ';
611
+                $sql .= implode(' AND ', $tmp).' ';
616 612
             }
617 613
         }
618 614
         if (count($w)) {
619 615
             $sql .= 'WHERE ';
620 616
             $tmp = [];
621 617
             foreach ($w as $v) {
622
-                $tmp[] = '(' . $v[0] . ')';
618
+                $tmp[] = '('.$v[0].')';
623 619
                 $par = array_merge($par, $v[1]);
624 620
             }
625 621
             $sql .= implode(' AND ', $tmp).' ';
626 622
         }
627 623
         if (count($g)) {
628
-            $sql .= 'GROUP BY ' . $g[0] . ' ';
624
+            $sql .= 'GROUP BY '.$g[0].' ';
629 625
             $par = array_merge($par, $g[1]);
630 626
         }
631 627
         if (count($h)) {
632 628
             $sql .= 'HAVING ';
633 629
             $tmp = [];
634 630
             foreach ($h as $v) {
635
-                $tmp[] = '(' . $v[0] . ')';
631
+                $tmp[] = '('.$v[0].')';
636 632
                 $par = array_merge($par, $v[1]);
637 633
             }
638 634
             $sql .= implode(' AND ', $tmp).' ';
@@ -666,7 +662,7 @@  discard block
 block discarded – undo
666 662
                     $this->with(implode('.', $temp));
667 663
                     $table = array_reduce(
668 664
                         $temp,
669
-                        function ($carry, $item) use (&$table) {
665
+                        function($carry, $item) use (&$table) {
670 666
                             return $table->getRelation($item)->table;
671 667
                         }
672 668
                     );
@@ -675,7 +671,7 @@  discard block
 block discarded – undo
675 671
                 }
676 672
                 unset($fields[$k]);
677 673
                 foreach ($cols as $col) {
678
-                    $fields[] = $table . '.' . $col;
674
+                    $fields[] = $table.'.'.$col;
679 675
                 }
680 676
             }
681 677
         }
@@ -707,9 +703,9 @@  discard block
 block discarded – undo
707 703
             return $this->qiterator;
708 704
         }
709 705
         $aliases = [];
710
-        $getAlias = function ($name) use (&$aliases) {
706
+        $getAlias = function($name) use (&$aliases) {
711 707
             // to bypass use: return $name;
712
-            return $aliases[$name] = $aliases[$name] ?? 'alias' . static::SEP . count($aliases);
708
+            return $aliases[$name] = $aliases[$name] ?? 'alias'.static::SEP.count($aliases);
713 709
         };
714 710
         $table = $this->definition->getName();
715 711
         if ($fields !== null) {
@@ -725,7 +721,7 @@  discard block
 block discarded – undo
725 721
         $h = $this->having;
726 722
         $o = $this->order;
727 723
         $g = $this->group;
728
-        $j = array_map(function ($v) { return clone $v; }, $this->joins);
724
+        $j = array_map(function($v) { return clone $v; }, $this->joins);
729 725
 
730 726
         $porder = [];
731 727
         foreach ($this->definition->getPrimaryKey() as $field) {
@@ -734,47 +730,47 @@  discard block
 block discarded – undo
734 730
 
735 731
         foreach ($this->definition->getRelations() as $k => $relation) {
736 732
             foreach ($f as $kk => $field) {
737
-                if (strpos($field, $k . '.') === 0) {
738
-                    $relations[$k] = [ $relation, $table ];
739
-                    $f[$kk] = str_replace($k . '.', $getAlias($k) . '.', $field);
733
+                if (strpos($field, $k.'.') === 0) {
734
+                    $relations[$k] = [$relation, $table];
735
+                    $f[$kk] = str_replace($k.'.', $getAlias($k).'.', $field);
740 736
                 }
741 737
             }
742 738
             foreach ($w as $kk => $v) {
743
-                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $v[0])) {
744
-                    $relations[$k] = [ $relation, $table ];
745
-                    $w[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $v[0]);
739
+                if (preg_match('(\b'.preg_quote($k.'.').')i', $v[0])) {
740
+                    $relations[$k] = [$relation, $table];
741
+                    $w[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $v[0]);
746 742
                 }
747 743
             }
748 744
             foreach ($h as $kk => $v) {
749
-                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $v[0])) {
750
-                    $relations[$k] = [ $relation, $table ];
751
-                    $h[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $v[0]);
745
+                if (preg_match('(\b'.preg_quote($k.'.').')i', $v[0])) {
746
+                    $relations[$k] = [$relation, $table];
747
+                    $h[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $v[0]);
752 748
                 }
753 749
             }
754
-            if (isset($o[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $o[0])) {
755
-                $relations[$k] = [ $relation, $table ];
756
-                $o[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $o[0]);
750
+            if (isset($o[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $o[0])) {
751
+                $relations[$k] = [$relation, $table];
752
+                $o[0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $o[0]);
757 753
             }
758
-            if (isset($g[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $g[0])) {
759
-                $relations[$k] = [ $relation, $table ];
760
-                $g[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $g[0]);
754
+            if (isset($g[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $g[0])) {
755
+                $relations[$k] = [$relation, $table];
756
+                $g[0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $g[0]);
761 757
             }
762 758
             foreach ($j as $kk => $v) {
763 759
                 foreach ($v->keymap as $kkk => $vv) {
764
-                    if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv)) {
765
-                        $relations[$k] = [ $relation, $table ];
766
-                        $j[$k]->keymap[$kkk] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vv);
760
+                    if (preg_match('(\b'.preg_quote($k.'.').')i', $vv)) {
761
+                        $relations[$k] = [$relation, $table];
762
+                        $j[$k]->keymap[$kkk] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $vv);
767 763
                     }
768 764
                 }
769 765
             }
770 766
         }
771 767
         $select = [];
772 768
         foreach ($f as $k => $field) {
773
-            $select[] = $field . (!is_numeric($k) ? ' ' . $k : '');
769
+            $select[] = $field.(!is_numeric($k) ? ' '.$k : '');
774 770
         }
775 771
         foreach ($this->withr as $name => $relation) {
776 772
             foreach ($relation[0]->table->getColumns() as $column) {
777
-                $select[] = $getAlias($name) . '.' . $column . ' ' . $getAlias($name . static::SEP . $column);
773
+                $select[] = $getAlias($name).'.'.$column.' '.$getAlias($name.static::SEP.$column);
778 774
             }
779 775
         }
780 776
         $sql = 'SELECT '.implode(', ', $select).' FROM '.$table.' ';
@@ -784,12 +780,12 @@  discard block
 block discarded – undo
784 780
             if ($v->many) {
785 781
                 $many = true;
786 782
             }
787
-            $sql .= ($v->many ? 'LEFT ' : '' ) . 'JOIN '.$v->table->getName().' '.$k.' ON ';
783
+            $sql .= ($v->many ? 'LEFT ' : '').'JOIN '.$v->table->getName().' '.$k.' ON ';
788 784
             $tmp = [];
789 785
             foreach ($v->keymap as $kk => $vv) {
790 786
                 $tmp[] = $kk.' = '.$vv;
791 787
             }
792
-            $sql .= implode(' AND ', $tmp) . ' ';
788
+            $sql .= implode(' AND ', $tmp).' ';
793 789
         }
794 790
         foreach ($relations as $relation => $v) {
795 791
             $table = $v[1] !== $this->definition->getName() ? $getAlias($v[1]) : $v[1];
@@ -804,13 +800,13 @@  discard block
 block discarded – undo
804 800
                 foreach ($v->keymap as $kk => $vv) {
805 801
                     $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' ';
806 802
                 }
807
-                $sql .= implode(' AND ', $tmp) . ' ';
803
+                $sql .= implode(' AND ', $tmp).' ';
808 804
                 $sql .= 'LEFT JOIN '.$v->table->getName().' '.$getAlias($relation).' ON ';
809 805
                 $tmp = [];
810 806
                 foreach ($v->pivot_keymap as $kk => $vv) {
811 807
                     $tmp[] = $getAlias($relation).'.'.$vv.' = '.$alias.'.'.$kk.' ';
812 808
                 }
813
-                $sql .= implode(' AND ', $tmp) . ' ';
809
+                $sql .= implode(' AND ', $tmp).' ';
814 810
             } else {
815 811
                 $alias = $getAlias($relation);
816 812
 
@@ -820,10 +816,10 @@  discard block
 block discarded – undo
820 816
                     $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' ';
821 817
                 }
822 818
                 if ($v->sql) {
823
-                    $tmp[] = $v->sql . ' ';
819
+                    $tmp[] = $v->sql.' ';
824 820
                     $par = array_merge($par, $v->par ?? []);
825 821
                 }
826
-                $sql .= implode(' AND ', $tmp) . ' ';
822
+                $sql .= implode(' AND ', $tmp).' ';
827 823
             }
828 824
         }
829 825
 
@@ -833,9 +829,9 @@  discard block
 block discarded – undo
833 829
                 if (count($porder) > 1) {
834 830
                     $pkw = [];
835 831
                     foreach ($porder as $name) {
836
-                        $pkw[] = $name . ' = ?';
832
+                        $pkw[] = $name.' = ?';
837 833
                     }
838
-                    $pkw = '(' . implode(' AND ', $pkw) . ')';
834
+                    $pkw = '('.implode(' AND ', $pkw).')';
839 835
                     $pkp = [];
840 836
                     foreach ($ids as $id) {
841 837
                         foreach ($id as $p) {
@@ -847,64 +843,64 @@  discard block
 block discarded – undo
847 843
                         $pkp
848 844
                     ];
849 845
                 } else {
850
-                    $w[] = [ $porder[0] . ' IN ('.implode(',', array_fill(0, count($ids), '?')).')', $ids ];
846
+                    $w[] = [$porder[0].' IN ('.implode(',', array_fill(0, count($ids), '?')).')', $ids];
851 847
                 }
852 848
             } else {
853
-                $w[] = [ '1=0', [] ];
849
+                $w[] = ['1=0', []];
854 850
             }
855 851
         }
856 852
         if (count($w)) {
857 853
             $sql .= 'WHERE ';
858 854
             $tmp = [];
859 855
             foreach ($w as $v) {
860
-                $tmp[] = '(' . $v[0] . ')';
856
+                $tmp[] = '('.$v[0].')';
861 857
                 $par = array_merge($par, $v[1]);
862 858
             }
863 859
             $sql .= implode(' AND ', $tmp).' ';
864 860
         }
865 861
         if (count($g)) {
866
-            $sql .= 'GROUP BY ' . $g[0] . ' ';
862
+            $sql .= 'GROUP BY '.$g[0].' ';
867 863
             $par = array_merge($par, $g[1]);
868 864
         }
869 865
         if (count($h)) {
870 866
             $sql .= 'HAVING ';
871 867
             $tmp = [];
872 868
             foreach ($h as $v) {
873
-                $tmp[] = '(' . $v[0] . ')';
869
+                $tmp[] = '('.$v[0].')';
874 870
                 $par = array_merge($par, $v[1]);
875 871
             }
876 872
             $sql .= implode(' AND ', $tmp).' ';
877 873
         }
878 874
         if (count($o)) {
879
-            $sql .= 'ORDER BY ' . $o[0] . ' ';
875
+            $sql .= 'ORDER BY '.$o[0].' ';
880 876
             $par = array_merge($par, $o[1]);
881 877
         }
882 878
         if (count($porder)) {
883 879
             $pdir = (count($o) && strpos($o[0], 'DESC') !== false) ? 'DESC' : 'ASC';
884
-            $porder = array_map(function ($v) use ($pdir) { return $v . ' ' . $pdir; }, $porder);
885
-            $sql .= (count($o) ? ', ' : 'ORDER BY ') . implode(', ', $porder) . ' ';
880
+            $porder = array_map(function($v) use ($pdir) { return $v.' '.$pdir; }, $porder);
881
+            $sql .= (count($o) ? ', ' : 'ORDER BY ').implode(', ', $porder).' ';
886 882
         }
887 883
         if ((!$many || $this->li_of[2] === 0 || !count($porder)) && $this->li_of[0]) {
888 884
             if ($this->db->driverName() === 'oracle') {
889
-                if ((int)$this->db->driverOption('version', 0) >= 12) {
890
-                    $sql .= 'OFFSET ' . $this->li_of[1] . ' ROWS FETCH NEXT ' . $this->li_of[0] . ' ROWS ONLY';
885
+                if ((int) $this->db->driverOption('version', 0) >= 12) {
886
+                    $sql .= 'OFFSET '.$this->li_of[1].' ROWS FETCH NEXT '.$this->li_of[0].' ROWS ONLY';
891 887
                 } else {
892
-                    $f = array_map(function ($v) {
888
+                    $f = array_map(function($v) {
893 889
                         $v = explode(' ', trim($v), 2);
894 890
                         if (count($v) === 2) { return $v[1]; }
895 891
                         $v = explode('.', $v[0], 2);
896 892
                         return count($v) === 2 ? $v[1] : $v[0];
897 893
                     }, $select);
898
-                    $sql = "SELECT " . implode(', ', $f) . " 
894
+                    $sql = "SELECT ".implode(', ', $f)." 
899 895
                             FROM (
900 896
                                 SELECT tbl__.*, rownum rnum__ FROM (
901
-                                    " . $sql . "
897
+                                    " . $sql."
902 898
                                 ) tbl__ 
903
-                                WHERE rownum <= " . ($this->li_of[0] + $this->li_of[1]) . "
899
+                                WHERE rownum <= " . ($this->li_of[0] + $this->li_of[1])."
904 900
                             ) WHERE rnum__ > " . $this->li_of[1];
905 901
                 }
906 902
             } else {
907
-                $sql .= 'LIMIT ' . $this->li_of[0] . ' OFFSET ' . $this->li_of[1];
903
+                $sql .= 'LIMIT '.$this->li_of[0].' OFFSET '.$this->li_of[1];
908 904
             }
909 905
         }
910 906
         return $this->qiterator = new TableQueryIterator(
@@ -954,7 +950,7 @@  discard block
 block discarded – undo
954 950
                 $ret[$k] = str_repeat(' ', 255);
955 951
                 $par[] = &$ret[$k];
956 952
             }
957
-            $sql .= ' RETURNING ' . implode(',', $primary) . ' INTO ' . implode(',', array_fill(0, count($primary), '?'));
953
+            $sql .= ' RETURNING '.implode(',', $primary).' INTO '.implode(',', array_fill(0, count($primary), '?'));
958 954
             $this->db->query($sql, $par);
959 955
             return $ret;
960 956
         } else {
@@ -986,7 +982,7 @@  discard block
 block discarded – undo
986 982
         }
987 983
         $sql = 'UPDATE '.$table.' SET ';
988 984
         $par = [];
989
-        $sql .= implode(', ', array_map(function ($v) { return $v . ' = ?'; }, array_keys($update))) . ' ';
985
+        $sql .= implode(', ', array_map(function($v) { return $v.' = ?'; }, array_keys($update))).' ';
990 986
         $par = array_merge($par, array_values($update));
991 987
         if (count($this->where)) {
992 988
             $sql .= 'WHERE ';
@@ -995,7 +991,7 @@  discard block
 block discarded – undo
995 991
                 $tmp[] = $v[0];
996 992
                 $par = array_merge($par, $v[1]);
997 993
             }
998
-            $sql .= implode(' AND ', $tmp) . ' ';
994
+            $sql .= implode(' AND ', $tmp).' ';
999 995
         }
1000 996
         if (count($this->order)) {
1001 997
             $sql .= $this->order[0];
@@ -1019,7 +1015,7 @@  discard block
 block discarded – undo
1019 1015
                 $tmp[] = $v[0];
1020 1016
                 $par = array_merge($par, $v[1]);
1021 1017
             }
1022
-            $sql .= implode(' AND ', $tmp) . ' ';
1018
+            $sql .= implode(' AND ', $tmp).' ';
1023 1019
         }
1024 1020
         if (count($this->order)) {
1025 1021
             $sql .= $this->order[0];
@@ -1039,13 +1035,13 @@  discard block
 block discarded – undo
1039 1035
         $table = $this->definition;
1040 1036
         array_reduce(
1041 1037
             $parts,
1042
-            function ($carry, $item) use (&$table) {
1038
+            function($carry, $item) use (&$table) {
1043 1039
                 $relation = $table->getRelation($item);
1044 1040
                 if (!$relation) {
1045 1041
                     throw new DBException('Invalid relation name');
1046 1042
                 }
1047
-                $name = $carry ? $carry . static::SEP . $item : $item;
1048
-                $this->withr[$name] = [ $relation, $carry ?? $table->getName() ];
1043
+                $name = $carry ? $carry.static::SEP.$item : $item;
1044
+                $this->withr[$name] = [$relation, $carry ?? $table->getName()];
1049 1045
                 $table = $relation->table;
1050 1046
                 return $name;
1051 1047
             }
@@ -1090,9 +1086,9 @@  discard block
 block discarded – undo
1090 1086
         }
1091 1087
 
1092 1088
         $aliases = [];
1093
-        $getAlias = function ($name) use (&$aliases) {
1089
+        $getAlias = function($name) use (&$aliases) {
1094 1090
             // to bypass use: return $name;
1095
-            return $aliases[$name] = $aliases[$name] ?? 'alias' . static::SEP . count($aliases);
1091
+            return $aliases[$name] = $aliases[$name] ?? 'alias'.static::SEP.count($aliases);
1096 1092
         };
1097 1093
         
1098 1094
         $table = $this->definition->getName();
@@ -1104,65 +1100,65 @@  discard block
 block discarded – undo
1104 1100
         $h = $this->having;
1105 1101
         $o = $this->order;
1106 1102
         $g = $this->group;
1107
-        $j = array_map(function ($v) { return clone $v; }, $this->joins);
1103
+        $j = array_map(function($v) { return clone $v; }, $this->joins);
1108 1104
         foreach ($this->definition->getRelations() as $k => $v) {
1109 1105
             foreach ($w as $kk => $vv) {
1110
-                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv[0])) {
1111
-                    $relations[$k] = [ $v, $table ];
1112
-                    $w[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vv[0]);
1106
+                if (preg_match('(\b'.preg_quote($k.'.').')i', $vv[0])) {
1107
+                    $relations[$k] = [$v, $table];
1108
+                    $w[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $vv[0]);
1113 1109
                 }
1114 1110
             }
1115
-            if (isset($o[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $o[0])) {
1116
-                $relations[$k] = [ $v, $table ];
1117
-                $o[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $o[0]);
1118
-                $o[2] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $o[2]);
1111
+            if (isset($o[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $o[0])) {
1112
+                $relations[$k] = [$v, $table];
1113
+                $o[0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $o[0]);
1114
+                $o[2] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $o[2]);
1119 1115
             }
1120 1116
             foreach ($h as $kk => $vv) {
1121
-                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv[0])) {
1122
-                    $relations[$k] = [ $v, $table ];
1123
-                    $h[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vv[0]);
1117
+                if (preg_match('(\b'.preg_quote($k.'.').')i', $vv[0])) {
1118
+                    $relations[$k] = [$v, $table];
1119
+                    $h[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $vv[0]);
1124 1120
                 }
1125 1121
             }
1126
-            if (isset($g[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $g[0])) {
1127
-                $relations[$k] = [ $v, $table ];
1128
-                $g[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $g[0]);
1122
+            if (isset($g[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $g[0])) {
1123
+                $relations[$k] = [$v, $table];
1124
+                $g[0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $g[0]);
1129 1125
             }
1130 1126
             foreach ($j as $kk => $vv) {
1131 1127
                 foreach ($vv->keymap as $kkk => $vvv) {
1132
-                    if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vvv)) {
1133
-                        $relations[$k] = [ $v, $table ];
1134
-                        $j[$kk]->keymap[$kkk] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vvv);
1128
+                    if (preg_match('(\b'.preg_quote($k.'.').')i', $vvv)) {
1129
+                        $relations[$k] = [$v, $table];
1130
+                        $j[$kk]->keymap[$kkk] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $vvv);
1135 1131
                     }
1136 1132
                 }
1137 1133
             }
1138 1134
         }
1139 1135
 
1140
-        $key = array_map(function ($v) use ($table) { return $table . '.' . $v; }, $this->pkey);
1136
+        $key = array_map(function($v) use ($table) { return $table.'.'.$v; }, $this->pkey);
1141 1137
         $own = false;
1142 1138
         $dir = 'ASC';
1143 1139
         if (count($o)) {
1144 1140
             $dir = strpos($o[0], ' DESC') ? 'DESC' : 'ASC';
1145
-            $own = strpos($o[2], $table . '.') === 0;
1141
+            $own = strpos($o[2], $table.'.') === 0;
1146 1142
         }
1147 1143
 
1148 1144
         $dst = $key;
1149 1145
         if ($own) {
1150 1146
             // if using own table - do not use max/min in order - that will prevent index usage
1151
-            $dst[] = $o[2] . ' orderbyfix___';
1147
+            $dst[] = $o[2].' orderbyfix___';
1152 1148
         } else {
1153
-            $dst[] = 'MAX(' . $o[2] . ') orderbyfix___';
1149
+            $dst[] = 'MAX('.$o[2].') orderbyfix___';
1154 1150
         }
1155 1151
         $dst = array_unique($dst);
1156 1152
 
1157 1153
         $par = [];
1158
-        $sql  = 'SELECT DISTINCT '.implode(', ', $dst).' FROM '.$table.' ';
1154
+        $sql = 'SELECT DISTINCT '.implode(', ', $dst).' FROM '.$table.' ';
1159 1155
         foreach ($j as $k => $v) {
1160
-            $sql .= ($v->many ? 'LEFT ' : '' ) . 'JOIN '.$v->table->getName().' '.$k.' ON ';
1156
+            $sql .= ($v->many ? 'LEFT ' : '').'JOIN '.$v->table->getName().' '.$k.' ON ';
1161 1157
             $tmp = [];
1162 1158
             foreach ($v->keymap as $kk => $vv) {
1163 1159
                 $tmp[] = $kk.' = '.$vv;
1164 1160
             }
1165
-            $sql .= implode(' AND ', $tmp) . ' ';
1161
+            $sql .= implode(' AND ', $tmp).' ';
1166 1162
         }
1167 1163
         foreach ($relations as $k => $v) {
1168 1164
             $table = $v[1] !== $this->definition->getName() ? $getAlias($v[1]) : $v[1];
@@ -1174,13 +1170,13 @@  discard block
 block discarded – undo
1174 1170
                 foreach ($v->keymap as $kk => $vv) {
1175 1171
                     $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' ';
1176 1172
                 }
1177
-                $sql .= implode(' AND ', $tmp) . ' ';
1173
+                $sql .= implode(' AND ', $tmp).' ';
1178 1174
                 $sql .= 'LEFT JOIN '.$v->table->getName().' '.$getAlias($k).' ON ';
1179 1175
                 $tmp = [];
1180 1176
                 foreach ($v->pivot_keymap as $kk => $vv) {
1181 1177
                     $tmp[] = $getAlias($k).'.'.$vv.' = '.$alias.'.'.$kk.' ';
1182 1178
                 }
1183
-                $sql .= implode(' AND ', $tmp) . ' ';
1179
+                $sql .= implode(' AND ', $tmp).' ';
1184 1180
             } else {
1185 1181
                 $alias = $getAlias($k);
1186 1182
                 $sql .= 'LEFT JOIN '.$v->table->getName().' '.$alias.' ON ';
@@ -1189,29 +1185,29 @@  discard block
 block discarded – undo
1189 1185
                     $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' ';
1190 1186
                 }
1191 1187
                 if ($v->sql) {
1192
-                    $tmp[] = $v->sql . ' ';
1188
+                    $tmp[] = $v->sql.' ';
1193 1189
                     $par = array_merge($par, $v->par ?? []);
1194 1190
                 }
1195
-                $sql .= implode(' AND ', $tmp) . ' ';
1191
+                $sql .= implode(' AND ', $tmp).' ';
1196 1192
             }
1197 1193
         }
1198 1194
         if (count($w)) {
1199 1195
             $sql .= 'WHERE ';
1200 1196
             $tmp = [];
1201 1197
             foreach ($w as $v) {
1202
-                $tmp[] = '(' . $v[0] . ')';
1198
+                $tmp[] = '('.$v[0].')';
1203 1199
                 $par = array_merge($par, $v[1]);
1204 1200
             }
1205 1201
             $sql .= implode(' AND ', $tmp).' ';
1206 1202
         }
1207 1203
         if (!$own) {
1208
-            $sql .= 'GROUP BY ' . implode(', ', $key) . ' ';
1204
+            $sql .= 'GROUP BY '.implode(', ', $key).' ';
1209 1205
         }
1210 1206
         if (count($h)) {
1211 1207
             $sql .= 'HAVING ';
1212 1208
             $tmp = [];
1213 1209
             foreach ($h as $v) {
1214
-                $tmp[] = '(' . $v[0] . ')';
1210
+                $tmp[] = '('.$v[0].')';
1215 1211
                 $par = array_merge($par, $v[1]);
1216 1212
             }
1217 1213
             $sql .= implode(' AND ', $tmp).' ';
@@ -1219,38 +1215,38 @@  discard block
 block discarded – undo
1219 1215
         if (count($o)) {
1220 1216
             $sql .= 'ORDER BY ';
1221 1217
             if ($own) {
1222
-                $sql .= $o[2] . ' ' . $dir;
1218
+                $sql .= $o[2].' '.$dir;
1223 1219
             } else {
1224
-                $sql .= 'MAX('.$o[2].') ' . $dir;
1220
+                $sql .= 'MAX('.$o[2].') '.$dir;
1225 1221
             }
1226 1222
         }
1227 1223
         $porder = [];
1228 1224
         $pdir = (count($o) && strpos($o[0], 'DESC') !== false) ? 'DESC' : 'ASC';
1229 1225
         foreach ($this->definition->getPrimaryKey() as $field) {
1230
-            $porder[] = $this->getColumn($field)['name'] . ' ' . $pdir;
1226
+            $porder[] = $this->getColumn($field)['name'].' '.$pdir;
1231 1227
         }
1232 1228
         if (count($porder)) {
1233
-            $sql .= (count($o) ? ', ' : 'ORDER BY ') . implode(', ', $porder) . ' ';
1229
+            $sql .= (count($o) ? ', ' : 'ORDER BY ').implode(', ', $porder).' ';
1234 1230
         }
1235 1231
 
1236 1232
         if ($this->li_of[0]) {
1237 1233
             if ($this->db->driverName() === 'oracle') {
1238
-                if ((int)$this->db->driverOption('version', 0) >= 12) {
1239
-                    $sql .= 'OFFSET ' . $this->li_of[1] . ' ROWS FETCH NEXT ' . $this->li_of[0] . ' ROWS ONLY';
1234
+                if ((int) $this->db->driverOption('version', 0) >= 12) {
1235
+                    $sql .= 'OFFSET '.$this->li_of[1].' ROWS FETCH NEXT '.$this->li_of[0].' ROWS ONLY';
1240 1236
                 } else {
1241
-                    $sql = "SELECT " . implode(', ', $dst) . " 
1237
+                    $sql = "SELECT ".implode(', ', $dst)." 
1242 1238
                             FROM (
1243 1239
                                 SELECT tbl__.*, rownum rnum__ FROM (
1244
-                                    " . $sql . "
1240
+                                    " . $sql."
1245 1241
                                 ) tbl__ 
1246
-                                WHERE rownum <= " . ($this->li_of[0] + $this->li_of[1]) . "
1242
+                                WHERE rownum <= " . ($this->li_of[0] + $this->li_of[1])."
1247 1243
                             ) WHERE rnum__ > " . $this->li_of[1];
1248 1244
                 }
1249 1245
             } else {
1250
-                $sql .= 'LIMIT ' . $this->li_of[0] . ' OFFSET ' . $this->li_of[1];
1246
+                $sql .= 'LIMIT '.$this->li_of[0].' OFFSET '.$this->li_of[1];
1251 1247
             }
1252 1248
         }
1253
-        return array_map(function ($v) {
1249
+        return array_map(function($v) {
1254 1250
             if (isset($v['orderbyfix___'])) {
1255 1251
                 unset($v['orderbyfix___']);
1256 1252
             }
Please login to merge, or discard this patch.
src/schema/Mapper.php 1 patch
Spacing   +7 added lines, -9 removed lines patch added patch discarded remove patch
@@ -80,8 +80,7 @@  discard block
 block discarded – undo
80 80
                 if (isset($this->definition->getRelations()[$method])) {
81 81
                     if (array_key_exists($method, $this->fetched)) {
82 82
                         return is_callable($this->fetched[$method]) ?
83
-                            $this->fetched[$method] = call_user_func($this->fetched[$method], $args[0] ?? null) :
84
-                            $this->fetched[$method];
83
+                            $this->fetched[$method] = call_user_func($this->fetched[$method], $args[0] ?? null) : $this->fetched[$method];
85 84
                     }
86 85
                 }
87 86
                 return null;
@@ -165,7 +164,7 @@  discard block
 block discarded – undo
165 164
     public function collection(TableQueryIterator $iterator, Table $definition) : Collection
166 165
     {
167 166
         return Collection::from($iterator)
168
-            ->map(function ($v) use ($definition) {
167
+            ->map(function($v) use ($definition) {
169 168
                 return $this->entity($definition, $v);
170 169
             });
171 170
     }
@@ -240,7 +239,7 @@  discard block
 block discarded – undo
240 239
         $definition = $entity->definition();
241 240
         foreach ($definition->getColumns() as $column) {
242 241
             if (!array_key_exists($column, $data)) {
243
-                $entity->__lazyProperty($column, function () use ($definition, $primary, $column) {
242
+                $entity->__lazyProperty($column, function() use ($definition, $primary, $column) {
244 243
                     $query = $this->db->table($definition->getName());
245 244
                     foreach ($primary as $k => $v) {
246 245
                         $query->filter($k, $v);
@@ -254,12 +253,12 @@  discard block
 block discarded – undo
254 253
                 $name,
255 254
                 array_key_exists($name, $data) ?
256 255
                     ($relation->many ? 
257
-                        array_map(function ($v) use ($relation) {
256
+                        array_map(function($v) use ($relation) {
258 257
                             return $this->entity($relation->table, $v);
259 258
                         }, $data[$name]) :
260 259
                         $this->entity($relation->table, $data[$name])
261 260
                     ) :
262
-                    function (array $columns = null) use ($entity, $definition, $relation, $data) {
261
+                    function(array $columns = null) use ($entity, $definition, $relation, $data) {
263 262
                         $query = $this->db->table($relation->table->getName(), true);
264 263
                         if ($columns !== null) {
265 264
                             $query->columns($columns);
@@ -285,7 +284,7 @@  discard block
 block discarded – undo
285 284
                                 );
286 285
                             }
287 286
                             foreach ($definition->getPrimaryKey() as $v) {
288
-                                $query->filter($nm . '.' . $v, $data[$v] ?? null);
287
+                                $query->filter($nm.'.'.$v, $data[$v] ?? null);
289 288
                             }
290 289
                         } else {
291 290
                             foreach ($relation->keymap as $k => $v) {
@@ -293,8 +292,7 @@  discard block
 block discarded – undo
293 292
                             }
294 293
                         }
295 294
                         return $relation->many ?
296
-                            $query->iterator() :
297
-                            $query[0];
295
+                            $query->iterator() : $query[0];
298 296
                     }
299 297
             );
300 298
         }
Please login to merge, or discard this patch.