Completed
Push — master ( 6b3a05...83dc72 )
by Ivan
19:51 queued 04:50
created
src/schema/MapperInterface.php 1 patch
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -58,12 +58,12 @@
 block discarded – undo
58 58
      * @param array|T $entity
59 59
      * @return bool
60 60
      */
61
-    public function deleted(array|object $entity): bool;
61
+    public function deleted(array | object $entity): bool;
62 62
     /**
63 63
      * @param array|T $entity
64 64
      * @return bool
65 65
      */
66
-    public function exists(array|object $entity): bool;
66
+    public function exists(array | object $entity): bool;
67 67
     /**
68 68
      * @return array<int,T>
69 69
      */
Please login to merge, or discard this patch.
src/schema/Mapper.php 2 patches
Spacing   +18 added lines, -19 removed lines patch added patch discarded remove patch
@@ -27,7 +27,7 @@  discard block
 block discarded – undo
27 27
      * @param class-string<T> $clss
28 28
      * @return void
29 29
      */
30
-    public function __construct(DBInterface $db, string|Table|null $table = '', string $clss = Entity::class)
30
+    public function __construct(DBInterface $db, string | Table | null $table = '', string $clss = Entity::class)
31 31
     {
32 32
         $this->db = $db;
33 33
         if (!$table) {
@@ -63,7 +63,7 @@  discard block
 block discarded – undo
63 63
         $temp = [];
64 64
         foreach ($this->table->getPrimaryKey() as $column) {
65 65
             try {
66
-                $temp[(string)$column] = $entity->{$column} ?? null;
66
+                $temp[(string) $column] = $entity->{$column} ?? null;
67 67
                 /** @phpstan-ignore-next-line */
68 68
             } catch (\Throwable $ignore) {
69 69
             }
@@ -93,7 +93,7 @@  discard block
 block discarded – undo
93 93
 
94 94
         // BEG: ugly hack to get relations changed directly on the object (not hydrated)
95 95
         $hack = [];
96
-        foreach ((array)$entity as $k => $v) {
96
+        foreach ((array) $entity as $k => $v) {
97 97
             $hack[$k[0] === "\0" ? substr($k, strrpos($k, "\0", 1) + 1) : $k] = $v;
98 98
         }
99 99
         $hack = $hack['changed'] ?? [];
@@ -104,7 +104,7 @@  discard block
 block discarded – undo
104 104
         foreach ($columns as $column) {
105 105
             try {
106 106
                 if (in_array($column, $fetched) || array_key_exists($column, $hack) || $fetch) {
107
-                    $temp[(string)$column] = $entity->{$column};
107
+                    $temp[(string) $column] = $entity->{$column};
108 108
                 }
109 109
             } catch (\Throwable $ignore) {
110 110
             }
@@ -114,7 +114,7 @@  discard block
 block discarded – undo
114 114
         foreach ($relations as $relation) {
115 115
             try {
116 116
                 if (array_key_exists($relation, $fetched) || array_key_exists($relation, $hack) || $fetch) {
117
-                    $temp[(string)$relation] = $entity->{$relation};
117
+                    $temp[(string) $relation] = $entity->{$relation};
118 118
                 }
119 119
             } catch (\Throwable $ignore) {
120 120
             }
@@ -153,10 +153,10 @@  discard block
 block discarded – undo
153 153
         $temp = [];
154 154
         foreach ($this->table->getColumns() as $column) {
155 155
             if (array_key_exists($column, $data)) {
156
-                $temp[(string)$column] = $data[$column];
156
+                $temp[(string) $column] = $data[$column];
157 157
             }
158 158
             if ($empty) {
159
-                $temp[(string)$column] = null;
159
+                $temp[(string) $column] = null;
160 160
             }
161 161
         }
162 162
         $entity = $this->instance(
@@ -183,7 +183,7 @@  discard block
 block discarded – undo
183 183
         $lazy = [];
184 184
         foreach ($this->table->getColumns() as $column) {
185 185
             if (!array_key_exists($column, $data)) {
186
-                $lazy[$column] = function ($entity) use ($column) {
186
+                $lazy[$column] = function($entity) use ($column) {
187 187
                     $query = $this->db->table($this->table->getFullName());
188 188
                     foreach ($this->id($entity) as $k => $v) {
189 189
                         $query->filter($k, $v);
@@ -206,7 +206,7 @@  discard block
 block discarded – undo
206 206
     {
207 207
         $relations = [];
208 208
         foreach ($this->table->getRelations() as $name => $relation) {
209
-            $relations[$name] = function (
209
+            $relations[$name] = function(
210 210
                 $entity,
211 211
                 bool $queryOnly = false
212 212
             ) use (
@@ -220,10 +220,10 @@  discard block
 block discarded – undo
220 220
                         return $this->objects[spl_object_hash($entity)][4][$name] = null;
221 221
                     }
222 222
                     $value = $relation->many ?
223
-                        Collection::from(array_map(function ($v) use ($mapper) {
223
+                        Collection::from(array_map(function($v) use ($mapper) {
224 224
                             return $mapper->entity($v);
225 225
                         }, $data[$name]))
226
-                            ->filter(function ($v) use ($mapper) {
226
+                            ->filter(function($v) use ($mapper) {
227 227
                                 return !$mapper->deleted($v);
228 228
                             }) :
229 229
                         ($mapper->deleted($data[$name]) ? null : $mapper->entity($data[$name]));
@@ -232,7 +232,7 @@  discard block
 block discarded – undo
232 232
                 }
233 233
                 $query = $this->db->tableMapped($relation->table->getFullName());
234 234
                 if ($relation->sql) {
235
-                    $query->where($relation->sql, $relation->par?:[]);
235
+                    $query->where($relation->sql, $relation->par ?: []);
236 236
                 }
237 237
                 if ($relation->pivot) {
238 238
                     $nm = null;
@@ -253,7 +253,7 @@  discard block
 block discarded – undo
253 253
                     }
254 254
                     $pk = $this->id($entity);
255 255
                     foreach ($pk as $k => $v) {
256
-                        $query->filter($nm . '.' . $k, $v);
256
+                        $query->filter($nm.'.'.$k, $v);
257 257
                     }
258 258
                 } else {
259 259
                     $temp = $this->toArray($entity, array_keys($relation->keymap));
@@ -265,10 +265,9 @@  discard block
 block discarded – undo
265 265
                     return $query;
266 266
                 }
267 267
                 $value = $relation->many ?
268
-                    $query->iterator() :
269
-                    ($query[0] ?? null);
268
+                    $query->iterator() : ($query[0] ?? null);
270 269
                 if ($value instanceof Collection) {
271
-                    $value->filter(function ($v) use ($mapper) {
270
+                    $value->filter(function($v) use ($mapper) {
272 271
                         return !$mapper->deleted($v);
273 272
                     });
274 273
                 } elseif (isset($value) && $mapper->deleted($value)) {
@@ -501,7 +500,7 @@  discard block
 block discarded – undo
501 500
             }
502 501
         }
503 502
     }
504
-    public function exists(array|object $entity): bool
503
+    public function exists(array | object $entity): bool
505 504
     {
506 505
         if (is_array($entity)) {
507 506
             $primary = [];
@@ -515,7 +514,7 @@  discard block
 block discarded – undo
515 514
         return isset($this->objects[spl_object_hash($entity)]) &&
516 515
             $this->objects[spl_object_hash($entity)][5] === false;
517 516
     }
518
-    public function deleted(array|object $entity): bool
517
+    public function deleted(array | object $entity): bool
519 518
     {
520 519
         if (is_array($entity)) {
521 520
             $primary = [];
@@ -587,7 +586,7 @@  discard block
 block discarded – undo
587 586
         return array_filter(
588 587
             array_values(
589 588
                 array_map(
590
-                    function ($v) {
589
+                    function($v) {
591 590
                         return $v[5] ? null : ($v[1] ?? null);
592 591
                     },
593 592
                     $this->objects
Please login to merge, or discard this patch.
Indentation   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -473,7 +473,7 @@
 block discarded – undo
473 473
                                         (isset($value) && is_array($value) && !in_array($e, $value)) ||
474 474
                                         (($value instanceof Collection) && !$value->contains($e))
475 475
                                     )
476
-                                 ) {
476
+                                    ) {
477 477
                                     $mapper->delete($e);
478 478
                                 }
479 479
                             }
Please login to merge, or discard this patch.
src/schema/Table.php 1 patch
Spacing   +9 added lines, -9 removed lines patch added patch discarded remove patch
@@ -79,10 +79,10 @@  discard block
 block discarded – undo
79 79
      * @param  array|string        $column either a single column name or an array of column names
80 80
      * @return  static
81 81
      */
82
-    public function setPrimaryKey(array|string $column): static
82
+    public function setPrimaryKey(array | string $column): static
83 83
     {
84 84
         if (!is_array($column)) {
85
-            $column = [ $column ];
85
+            $column = [$column];
86 86
         }
87 87
         $this->data['primary'] = array_values($column);
88 88
         return $this;
@@ -109,7 +109,7 @@  discard block
 block discarded – undo
109 109
      */
110 110
     public function getFullName(): string
111 111
     {
112
-        return ($this->data['schema'] ? $this->data['schema'] . '.' : '') . $this->data['name'];
112
+        return ($this->data['schema'] ? $this->data['schema'].'.' : '').$this->data['name'];
113 113
     }
114 114
     /**
115 115
      * Get a column definition
@@ -156,7 +156,7 @@  discard block
 block discarded – undo
156 156
     public function hasOne(
157 157
         Table $toTable,
158 158
         string $name = null,
159
-        string|array|null $toTableColumn = null,
159
+        string | array | null $toTableColumn = null,
160 160
         string $sql = null,
161 161
         array $par = []
162 162
     ) : static {
@@ -184,7 +184,7 @@  discard block
 block discarded – undo
184 184
         }
185 185
 
186 186
         if (!isset($name)) {
187
-            $name = $toTable->getName() . '_' . implode('_', array_keys($keymap));
187
+            $name = $toTable->getName().'_'.implode('_', array_keys($keymap));
188 188
         }
189 189
         $this->addRelation(new TableRelation(
190 190
             $this,
@@ -211,7 +211,7 @@  discard block
 block discarded – undo
211 211
     public function hasMany(
212 212
         Table $toTable,
213 213
         string $name = null,
214
-        string|array|null $toTableColumn = null,
214
+        string | array | null $toTableColumn = null,
215 215
         ?string $sql = null,
216 216
         array $par = []
217 217
     ): static {
@@ -266,7 +266,7 @@  discard block
 block discarded – undo
266 266
     public function belongsTo(
267 267
         Table $toTable,
268 268
         string $name = null,
269
-        string|array|null $localColumn = null,
269
+        string | array | null $localColumn = null,
270 270
         ?string $sql = null,
271 271
         array $par = []
272 272
     ): static {
@@ -322,8 +322,8 @@  discard block
 block discarded – undo
322 322
         Table $toTable,
323 323
         Table $pivot,
324 324
         ?string $name = null,
325
-        string|array|null $toTableColumn = null,
326
-        string|array|null $localColumn = null
325
+        string | array | null $toTableColumn = null,
326
+        string | array | null $localColumn = null
327 327
     ): static {
328 328
         $pivotColumns = $pivot->getColumns();
329 329
 
Please login to merge, or discard this patch.
src/schema/TableQuery.php 1 patch
Spacing   +256 added lines, -260 removed lines patch added patch discarded remove patch
@@ -22,7 +22,7 @@  discard block
 block discarded – undo
22 22
     protected array $order = [];
23 23
     protected array $group = [];
24 24
     protected array $having = [];
25
-    protected array $li_of = [0,0,0];
25
+    protected array $li_of = [0, 0, 0];
26 26
     protected array $fields = [];
27 27
     protected array $withr = [];
28 28
     protected array $joins = [];
@@ -36,11 +36,11 @@  discard block
 block discarded – undo
36 36
      * @param  Table|string   $table           the name or definition of the main table in the query
37 37
      * @param  bool           $findRelations   should the query builder try to find missing joins
38 38
      */
39
-    public function __construct(DBInterface $db, Table|string $table, bool $findRelations = false)
39
+    public function __construct(DBInterface $db, Table | string $table, bool $findRelations = false)
40 40
     {
41 41
         $this->db = $db;
42 42
         $this->findRelations = $findRelations;
43
-        $this->definition = $table instanceof Table ? $table : $this->db->definition((string)$table);
43
+        $this->definition = $table instanceof Table ? $table : $this->db->definition((string) $table);
44 44
         $primary = $this->definition->getPrimaryKey();
45 45
         $columns = $this->definition->getColumns();
46 46
         $this->pkey = count($primary) ? $primary : $columns;
@@ -63,7 +63,7 @@  discard block
 block discarded – undo
63 63
     {
64 64
         $column = explode('.', $column);
65 65
         if (count($column) === 1) {
66
-            $column = [ $this->definition->getFullName(), $column[0] ];
66
+            $column = [$this->definition->getFullName(), $column[0]];
67 67
             $col = $this->definition->getColumn($column[1]);
68 68
             if (!$col) {
69 69
                 throw new DBException('Invalid column name in own table');
@@ -102,7 +102,7 @@  discard block
 block discarded – undo
102 102
                             $path = $this->db->findRelation($this->definition->getName(), $column[0]);
103 103
                         }
104 104
                         if (!count($path)) {
105
-                            throw new DBException('Invalid foreign table / column name: ' . implode(',', $column));
105
+                            throw new DBException('Invalid foreign table / column name: '.implode(',', $column));
106 106
                         }
107 107
                         unset($path[0]);
108 108
                         $this->with(implode('.', $path), false);
@@ -115,26 +115,26 @@  discard block
 block discarded – undo
115 115
             if ($this->definition->hasRelation(implode('.', $column))) {
116 116
                 $this->with(implode('.', $column), false);
117 117
                 $col = $this->definition->getRelation(implode('.', $column))?->table?->getColumn($name);
118
-                $column = [ implode('.', $column), $name ];
118
+                $column = [implode('.', $column), $name];
119 119
             } else {
120 120
                 $this->with(implode('.', $column), false);
121 121
                 $table = $this->definition;
122 122
                 $table = array_reduce(
123 123
                     $column,
124
-                    function ($carry, $item) use (&$table) {
124
+                    function($carry, $item) use (&$table) {
125 125
                         $table = $table->getRelation($item)->table;
126 126
                         return $table;
127 127
                     }
128 128
                 );
129 129
                 $col = $table->getColumn($name);
130
-                $column = [ implode(static::SEP, $column), $name ];
130
+                $column = [implode(static::SEP, $column), $name];
131 131
             }
132 132
         }
133
-        return [ 'name' => implode('.', $column), 'data' => $col ];
133
+        return ['name' => implode('.', $column), 'data' => $col];
134 134
     }
135 135
     protected function normalizeValue(TableColumn $col, mixed $value): mixed
136 136
     {
137
-        $strict = (int)$this->db->driverOption('strict', 0) > 0;
137
+        $strict = (int) $this->db->driverOption('strict', 0) > 0;
138 138
         if ($value === null && $col->isNullable()) {
139 139
             return null;
140 140
         }
@@ -144,7 +144,7 @@  discard block
 block discarded – undo
144 144
                     $temp = strtotime($value);
145 145
                     if (!$temp) {
146 146
                         if ($strict) {
147
-                            throw new DBException('Invalid value for date column ' . $col->getName());
147
+                            throw new DBException('Invalid value for date column '.$col->getName());
148 148
                         }
149 149
                         return null;
150 150
                     }
@@ -157,7 +157,7 @@  discard block
 block discarded – undo
157 157
                     return $value->format('Y-m-d');
158 158
                 }
159 159
                 if ($strict) {
160
-                    throw new DBException('Invalid value (unknown data type) for date column ' . $col->getName());
160
+                    throw new DBException('Invalid value (unknown data type) for date column '.$col->getName());
161 161
                 }
162 162
                 return $value;
163 163
             case 'datetime':
@@ -165,7 +165,7 @@  discard block
 block discarded – undo
165 165
                     $temp = strtotime($value);
166 166
                     if (!$temp) {
167 167
                         if ($strict) {
168
-                            throw new DBException('Invalid value for datetime column ' . $col->getName());
168
+                            throw new DBException('Invalid value for datetime column '.$col->getName());
169 169
                         }
170 170
                         return null;
171 171
                     }
@@ -178,7 +178,7 @@  discard block
 block discarded – undo
178 178
                     return $value->format('Y-m-d H:i:s');
179 179
                 }
180 180
                 if ($strict) {
181
-                    throw new DBException('Invalid value (unknown data type) for datetime column ' . $col->getName());
181
+                    throw new DBException('Invalid value (unknown data type) for datetime column '.$col->getName());
182 182
                 }
183 183
                 return $value;
184 184
             case 'enum':
@@ -186,7 +186,7 @@  discard block
 block discarded – undo
186 186
                 if (is_int($value)) {
187 187
                     if (!isset($values[$value])) {
188 188
                         if ($strict) {
189
-                            throw new DBException('Invalid value (using integer) for enum ' . $col->getName());
189
+                            throw new DBException('Invalid value (using integer) for enum '.$col->getName());
190 190
                         }
191 191
                         return $value;
192 192
                     }
@@ -194,23 +194,23 @@  discard block
 block discarded – undo
194 194
                 }
195 195
                 if (!in_array($value, $col->getValues())) {
196 196
                     if ($strict) {
197
-                        throw new DBException('Invalid value for enum ' . $col->getName());
197
+                        throw new DBException('Invalid value for enum '.$col->getName());
198 198
                     }
199 199
                     return 0;
200 200
                 }
201 201
                 return $value;
202 202
             case 'int':
203
-                $temp = preg_replace('([^+\-0-9]+)', '', (string)$value);
204
-                return is_string($temp) ? (int)$temp : 0;
203
+                $temp = preg_replace('([^+\-0-9]+)', '', (string) $value);
204
+                return is_string($temp) ? (int) $temp : 0;
205 205
             case 'float':
206
-                $temp = preg_replace('([^+\-0-9.]+)', '', str_replace(',', '.', (string)$value));
207
-                return is_string($temp) ? (float)$temp : 0;
206
+                $temp = preg_replace('([^+\-0-9.]+)', '', str_replace(',', '.', (string) $value));
207
+                return is_string($temp) ? (float) $temp : 0;
208 208
             case 'text':
209 209
                 // check using strlen first, in order to avoid hitting mb_ functions which might be polyfilled
210 210
                 // because the polyfill is quite slow
211 211
                 if ($col->hasLength() && strlen($value) > $col->getLength() && mb_strlen($value) > $col->getLength()) {
212 212
                     if ($strict) {
213
-                        throw new DBException('Invalid value for text column ' . $col->getName());
213
+                        throw new DBException('Invalid value for text column '.$col->getName());
214 214
                     }
215 215
                     return mb_substr($value, 0, $col->getLength());
216 216
                 }
@@ -234,7 +234,7 @@  discard block
 block discarded – undo
234 234
             if ($column->getBasicType() !== 'text') {
235 235
                 switch ($this->db->driverName()) {
236 236
                     case 'oracle':
237
-                        $name = 'CAST(' . $name . ' AS NVARCHAR(500))';
237
+                        $name = 'CAST('.$name.' AS NVARCHAR(500))';
238 238
                         break;
239 239
                     case 'postgre':
240 240
                         $name = $name.'::text';
@@ -242,45 +242,42 @@  discard block
 block discarded – undo
242 242
                 }
243 243
             }
244 244
             $mode = array_keys($value)[0];
245
-            $value = str_replace(['%', '_'], ['\\%','\\_'], array_values($value)[0]) . '%';
245
+            $value = str_replace(['%', '_'], ['\\%', '\\_'], array_values($value)[0]).'%';
246 246
             if ($mode === 'contains' || $mode === 'icontains') {
247
-                $value = '%' . $value;
247
+                $value = '%'.$value;
248 248
             }
249 249
             if ($mode === 'icontains' || $mode === 'ilike') {
250 250
                 $value = mb_strtoupper($value);
251
-                $name = 'UPPER(' . $name . ')';
251
+                $name = 'UPPER('.$name.')';
252 252
             }
253 253
             return $negate ?
254 254
                 [
255
-                    $name . ' NOT LIKE ?',
256
-                    [ (string)$value ]
257
-                ] :
258
-                [
259
-                    $name . ' LIKE ?',
260
-                    [ (string)$value ]
255
+                    $name.' NOT LIKE ?',
256
+                    [(string) $value]
257
+                ] : [
258
+                    $name.' LIKE ?',
259
+                    [(string) $value]
261 260
                 ];
262 261
         }
263 262
         if (is_null($value)) {
264 263
             return $negate ?
265
-                [ $name . ' IS NOT NULL', [] ]:
266
-                [ $name . ' IS NULL', [] ];
264
+                [$name.' IS NOT NULL', []] : [$name.' IS NULL', []];
267 265
         }
268 266
         if (!is_array($value)) {
269 267
             return $negate ?
270 268
                 [
271
-                    $name . ' <> ?',
272
-                    [ $this->normalizeValue($column, $value) ]
273
-                ] :
274
-                [
275
-                    $name . ' = ?',
276
-                    [ $this->normalizeValue($column, $value) ]
269
+                    $name.' <> ?',
270
+                    [$this->normalizeValue($column, $value)]
271
+                ] : [
272
+                    $name.' = ?',
273
+                    [$this->normalizeValue($column, $value)]
277 274
                 ];
278 275
         }
279 276
         if (isset($value['beg']) && strlen($value['beg']) && (!isset($value['end']) || !strlen($value['end']))) {
280
-            $value = [ 'gte' => $value['beg'] ];
277
+            $value = ['gte' => $value['beg']];
281 278
         }
282 279
         if (isset($value['end']) && strlen($value['end']) && (!isset($value['beg']) || !strlen($value['beg']))) {
283
-            $value = [ 'lte' => $value['end'] ];
280
+            $value = ['lte' => $value['end']];
284 281
         }
285 282
         if (isset($value['beg']) && isset($value['end'])) {
286 283
             return $negate ?
@@ -290,8 +287,7 @@  discard block
 block discarded – undo
290 287
                         $this->normalizeValue($column, $value['beg']),
291 288
                         $this->normalizeValue($column, $value['end'])
292 289
                     ]
293
-                ] :
294
-                [
290
+                ] : [
295 291
                     $name.' BETWEEN ? AND ?',
296 292
                     [
297 293
                         $this->normalizeValue($column, $value['beg']),
@@ -303,42 +299,42 @@  discard block
 block discarded – undo
303 299
             $sql = [];
304 300
             $par = [];
305 301
             if (isset($value['gt'])) {
306
-                $sql[] = $name. ' ' . ($negate ? '<=' : '>') . ' ?';
302
+                $sql[] = $name.' '.($negate ? '<=' : '>').' ?';
307 303
                 $par[] = $this->normalizeValue($column, $value['gt']);
308 304
             }
309 305
             if (isset($value['gte'])) {
310
-                $sql[] = $name. ' ' . ($negate ? '<' : '>=') . ' ?';
306
+                $sql[] = $name.' '.($negate ? '<' : '>=').' ?';
311 307
                 $par[] = $this->normalizeValue($column, $value['gte']);
312 308
             }
313 309
             if (isset($value['lt'])) {
314
-                $sql[] = $name. ' ' . ($negate ? '>=' : '<') . ' ?';
310
+                $sql[] = $name.' '.($negate ? '>=' : '<').' ?';
315 311
                 $par[] = $this->normalizeValue($column, $value['lt']);
316 312
             }
317 313
             if (isset($value['lte'])) {
318
-                $sql[] = $name. ' ' . ($negate ? '>' : '<=') . ' ?';
314
+                $sql[] = $name.' '.($negate ? '>' : '<=').' ?';
319 315
                 $par[] = $this->normalizeValue($column, $value['lte']);
320 316
             }
321 317
             return [
322
-                '(' . implode(' AND ', $sql) . ')',
318
+                '('.implode(' AND ', $sql).')',
323 319
                 $par
324 320
             ];
325 321
         }
326 322
 
327
-        $value = array_values(array_map(function ($v) use ($column) {
323
+        $value = array_values(array_map(function($v) use ($column) {
328 324
             return $this->normalizeValue($column, $v);
329 325
         }, $value));
330 326
         if ($this->db->driverName() === 'oracle') {
331 327
             $sql = [];
332 328
             $par = [];
333 329
             for ($i = 0; $i < count($value); $i += 500) {
334
-                $sql[] = $negate ? $name . ' NOT IN (??)' : $name . ' IN (??)';
330
+                $sql[] = $negate ? $name.' NOT IN (??)' : $name.' IN (??)';
335 331
                 $par[] = array_slice($value, $i, 500);
336 332
             }
337
-            $sql = '(' . implode($negate ? ' AND ' : ' OR ', $sql) . ')';
338
-            return [ $sql, $par ];
333
+            $sql = '('.implode($negate ? ' AND ' : ' OR ', $sql).')';
334
+            return [$sql, $par];
339 335
         }
340 336
         return [
341
-            $negate ? $name . ' NOT IN (??)' : $name . ' IN (??)',
337
+            $negate ? $name.' NOT IN (??)' : $name.' IN (??)',
342 338
             [$value]
343 339
         ];
344 340
     }
@@ -370,7 +366,7 @@  discard block
 block discarded – undo
370 366
                 $par = array_merge($par, $temp[1]);
371 367
             }
372 368
         }
373
-        return $this->where('(' . implode(' OR ', $sql) . ')', $par);
369
+        return $this->where('('.implode(' OR ', $sql).')', $par);
374 370
     }
375 371
     /**
376 372
      * Filter the results matching all of the criteria
@@ -388,7 +384,7 @@  discard block
 block discarded – undo
388 384
                 $par = array_merge($par, $temp[1]);
389 385
             }
390 386
         }
391
-        return $this->where('(' . implode(' AND ', $sql) . ')', $par);
387
+        return $this->where('('.implode(' AND ', $sql).')', $par);
392 388
     }
393 389
     /**
394 390
      * Sort by a column
@@ -403,7 +399,7 @@  discard block
 block discarded – undo
403 399
         } catch (DBException $e) {
404 400
             throw new DBException('Invalid sort column');
405 401
         }
406
-        return $this->order($column . ' ' . ($desc ? 'DESC' : 'ASC'));
402
+        return $this->order($column.' '.($desc ? 'DESC' : 'ASC'));
407 403
     }
408 404
     /**
409 405
      * Group by a column (or columns)
@@ -413,7 +409,7 @@  discard block
 block discarded – undo
413 409
     public function group($column) : static
414 410
     {
415 411
         if (!is_array($column)) {
416
-            $column = [ $column ];
412
+            $column = [$column];
417 413
         }
418 414
         foreach ($column as $k => $v) {
419 415
             $column[$k] = $this->getColumn($v)['name'];
@@ -456,7 +452,7 @@  discard block
 block discarded – undo
456 452
         $this->order = [];
457 453
         $this->having = [];
458 454
         $this->aliases = [];
459
-        $this->li_of = [0,0,0];
455
+        $this->li_of = [0, 0, 0];
460 456
         $this->qiterator = null;
461 457
         return $this;
462 458
     }
@@ -469,7 +465,7 @@  discard block
 block discarded – undo
469 465
     public function groupBy(string $sql, array $params = []) : static
470 466
     {
471 467
         $this->qiterator = null;
472
-        $this->group = [ $sql, $params ];
468
+        $this->group = [$sql, $params];
473 469
         return $this;
474 470
     }
475 471
     /**
@@ -483,7 +479,7 @@  discard block
 block discarded – undo
483 479
     public function join($table, array $fields, string $name = null, bool $multiple = true)
484 480
     {
485 481
         $this->qiterator = null;
486
-        $table = $table instanceof Table ? $table : $this->db->definition((string)$table);
482
+        $table = $table instanceof Table ? $table : $this->db->definition((string) $table);
487 483
         $name = $name ?? $table->getName();
488 484
         if (isset($this->joins[$name]) || $this->definition->hasRelation($name)) {
489 485
             throw new DBException('Alias / table name already in use');
@@ -492,7 +488,7 @@  discard block
 block discarded – undo
492 488
         foreach ($fields as $k => $v) {
493 489
             $k = explode('.', $k, 2);
494 490
             $k = count($k) == 2 ? $k[1] : $k[0];
495
-            $this->joins[$name]->keymap[$this->getColumn($name . '.' . $k)['name']] = $this->getColumn($v)['name'];
491
+            $this->joins[$name]->keymap[$this->getColumn($name.'.'.$k)['name']] = $this->getColumn($v)['name'];
496 492
         }
497 493
         return $this;
498 494
     }
@@ -505,7 +501,7 @@  discard block
 block discarded – undo
505 501
     public function where(string $sql, array $params = []) : static
506 502
     {
507 503
         $this->qiterator = null;
508
-        $this->where[] = [ $sql, $params ];
504
+        $this->where[] = [$sql, $params];
509 505
         return $this;
510 506
     }
511 507
     /**
@@ -517,7 +513,7 @@  discard block
 block discarded – undo
517 513
     public function having(string $sql, array $params = []) : static
518 514
     {
519 515
         $this->qiterator = null;
520
-        $this->having[] = [ $sql, $params ];
516
+        $this->having[] = [$sql, $params];
521 517
         return $this;
522 518
     }
523 519
     /**
@@ -537,12 +533,12 @@  discard block
 block discarded – undo
537 533
                     throw new \Exception();
538 534
                 }
539 535
                 $name = $this->getColumn(trim($name))['name'];
540
-                $sql = $name . ' ' . (strpos(strtolower($sql), ' desc') ? 'DESC' : 'ASC');
536
+                $sql = $name.' '.(strpos(strtolower($sql), ' desc') ? 'DESC' : 'ASC');
541 537
             } catch (\Exception $e) {
542 538
                 $name = null;
543 539
             }
544 540
         }
545
-        $this->order = [ $sql, $params, $name ];
541
+        $this->order = [$sql, $params, $name];
546 542
         return $this;
547 543
     }
548 544
     /**
@@ -554,7 +550,7 @@  discard block
 block discarded – undo
554 550
     public function limit(int $limit, int $offset = 0, bool $limitOnMainTable = false) : static
555 551
     {
556 552
         $this->qiterator = null;
557
-        $this->li_of = [ $limit, $offset, $limitOnMainTable ? 1 : 0 ];
553
+        $this->li_of = [$limit, $offset, $limitOnMainTable ? 1 : 0];
558 554
         return $this;
559 555
     }
560 556
     /**
@@ -565,9 +561,9 @@  discard block
 block discarded – undo
565 561
     {
566 562
         $aliases = [];
567 563
         $aliases_ext = [];
568
-        $getAlias = function ($name) use (&$aliases, &$aliases_ext) {
564
+        $getAlias = function($name) use (&$aliases, &$aliases_ext) {
569 565
             // to bypass use: return $name;
570
-            $aliases[$name] = $aliases[$name] ?? 'alias' . static::SEP . count($aliases);
566
+            $aliases[$name] = $aliases[$name] ?? 'alias'.static::SEP.count($aliases);
571 567
             if (isset($aliases_ext[$name])) {
572 568
                 unset($aliases_ext[$name]);
573 569
             }
@@ -590,7 +586,7 @@  discard block
 block discarded – undo
590 586
         $h = $this->having;
591 587
         $o = $this->order;
592 588
         $g = $this->group;
593
-        $j = array_map(function ($v) {
589
+        $j = array_map(function($v) {
594 590
             return clone $v;
595 591
         }, $this->joins);
596 592
 
@@ -600,28 +596,28 @@  discard block
 block discarded – undo
600 596
                 continue;
601 597
             }
602 598
             foreach ($w as $kk => $v) {
603
-                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $v[0])) {
599
+                if (preg_match('(\b'.preg_quote($k.'.').')i', $v[0])) {
604 600
                     $used_relations[] = $k;
605
-                    $w[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $v[0]);
601
+                    $w[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $v[0]);
606 602
                 }
607 603
             }
608 604
             foreach ($h as $kk => $v) {
609
-                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $v[0])) {
605
+                if (preg_match('(\b'.preg_quote($k.'.').')i', $v[0])) {
610 606
                     $used_relations[] = $k;
611
-                    $h[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $v[0]);
607
+                    $h[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $v[0]);
612 608
                 }
613 609
             }
614
-            if (isset($g[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $g[0])) {
610
+            if (isset($g[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $g[0])) {
615 611
                 $used_relations[] = $k;
616
-                $g[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $g[0]);
612
+                $g[0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $g[0]);
617 613
             }
618 614
             foreach ($j as $kk => $v) {
619 615
                 foreach ($v->keymap as $kkk => $vv) {
620
-                    if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv)) {
616
+                    if (preg_match('(\b'.preg_quote($k.'.').')i', $vv)) {
621 617
                         $used_relations[] = $k;
622 618
                         $j[$kk]->keymap[$kkk] = preg_replace(
623
-                            '(\b'.preg_quote($k . '.'). ')i',
624
-                            $getAlias($k) . '.',
619
+                            '(\b'.preg_quote($k.'.').')i',
620
+                            $getAlias($k).'.',
625 621
                             $vv
626 622
                         );
627 623
                     }
@@ -630,65 +626,65 @@  discard block
 block discarded – undo
630 626
         }
631 627
         foreach ($this->definition->getRelations() as $k => $v) {
632 628
             foreach ($w as $kk => $vv) {
633
-                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv[0])) {
634
-                    $relations[$k] = [ $v, $table ];
629
+                if (preg_match('(\b'.preg_quote($k.'.').')i', $vv[0])) {
630
+                    $relations[$k] = [$v, $table];
635 631
                     $used_relations[] = $k;
636
-                    $w[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vv[0]);
632
+                    $w[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $vv[0]);
637 633
                 }
638 634
             }
639
-            if (isset($o[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $o[0])) {
640
-                $relations[$k] = [ $v, $table ];
635
+            if (isset($o[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $o[0])) {
636
+                $relations[$k] = [$v, $table];
641 637
             }
642 638
             foreach ($h as $kk => $vv) {
643
-                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv[0])) {
644
-                    $relations[$k] = [ $v, $table ];
639
+                if (preg_match('(\b'.preg_quote($k.'.').')i', $vv[0])) {
640
+                    $relations[$k] = [$v, $table];
645 641
                     $used_relations[] = $k;
646
-                    $h[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vv[0]);
642
+                    $h[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $vv[0]);
647 643
                 }
648 644
             }
649
-            if (isset($g[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $g[0])) {
650
-                $relations[$k] = [ $v, $table ];
645
+            if (isset($g[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $g[0])) {
646
+                $relations[$k] = [$v, $table];
651 647
                 $used_relations[] = $k;
652
-                $g[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $g[0]);
648
+                $g[0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $g[0]);
653 649
             }
654 650
             foreach ($j as $kk => $vv) {
655 651
                 foreach ($vv->keymap as $kkk => $vvv) {
656
-                    if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vvv)) {
657
-                        $relations[$k] = [ $v, $table ];
652
+                    if (preg_match('(\b'.preg_quote($k.'.').')i', $vvv)) {
653
+                        $relations[$k] = [$v, $table];
658 654
                         $used_relations[] = $k;
659 655
                         $j[$kk]->keymap[$kkk] =
660
-                            preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vvv);
656
+                            preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $vvv);
661 657
                     }
662 658
                 }
663 659
             }
664 660
         }
665 661
         foreach ($aliases_ext as $k => $alias) {
666 662
             foreach ($w as $kk => $v) {
667
-                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $v[0])) {
668
-                    $w[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $alias . '.', $v[0]);
663
+                if (preg_match('(\b'.preg_quote($k.'.').')i', $v[0])) {
664
+                    $w[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $alias.'.', $v[0]);
669 665
                     $used_relations[] = $k;
670 666
                 }
671 667
             }
672 668
             foreach ($h as $kk => $v) {
673
-                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $v[0])) {
674
-                    $h[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $alias . '.', $v[0]);
669
+                if (preg_match('(\b'.preg_quote($k.'.').')i', $v[0])) {
670
+                    $h[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $alias.'.', $v[0]);
675 671
                     $used_relations[] = $k;
676 672
                 }
677 673
             }
678
-            if (isset($o[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $o[0])) {
674
+            if (isset($o[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $o[0])) {
679 675
                 $used_relations[] = $k;
680 676
             }
681
-            if (isset($g[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $g[0])) {
682
-                $g[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $alias . '.', $g[0]);
677
+            if (isset($g[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $g[0])) {
678
+                $g[0] = preg_replace('(\b'.preg_quote($k.'.').')i', $alias.'.', $g[0]);
683 679
                 $used_relations[] = $k;
684 680
             }
685 681
             foreach ($j as $kk => $v) {
686 682
                 foreach ($v->keymap as $kkk => $vv) {
687
-                    if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv)) {
683
+                    if (preg_match('(\b'.preg_quote($k.'.').')i', $vv)) {
688 684
                         $used_relations[] = $k;
689 685
                         $j[$kk]->keymap[$kkk] = preg_replace(
690
-                            '(\b'.preg_quote($k . '.'). ')i',
691
-                            $alias . '.',
686
+                            '(\b'.preg_quote($k.'.').')i',
687
+                            $alias.'.',
692 688
                             $vv
693 689
                         );
694 690
                     }
@@ -707,13 +703,13 @@  discard block
 block discarded – undo
707 703
                     foreach ($v->keymap as $kk => $vv) {
708 704
                         $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' ';
709 705
                     }
710
-                    $sql .= implode(' AND ', $tmp) . ' ';
706
+                    $sql .= implode(' AND ', $tmp).' ';
711 707
                     $sql .= 'LEFT JOIN '.$v->table->getFullName().' '.$getAlias($k).' ON ';
712 708
                     $tmp = [];
713 709
                     foreach ($v->pivot_keymap as $kk => $vv) {
714 710
                         $tmp[] = $getAlias($k).'.'.$vv.' = '.$alias.'.'.$kk.' ';
715 711
                     }
716
-                    $sql .= implode(' AND ', $tmp) . ' ';
712
+                    $sql .= implode(' AND ', $tmp).' ';
717 713
                 } else {
718 714
                     $alias = $getAlias($k);
719 715
                     $sql .= 'LEFT JOIN '.$v->table->getFullName().' '.$alias.' ON ';
@@ -722,10 +718,10 @@  discard block
 block discarded – undo
722 718
                         $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' ';
723 719
                     }
724 720
                     if ($v->sql) {
725
-                        $tmp[] = $v->sql . ' ';
721
+                        $tmp[] = $v->sql.' ';
726 722
                         $par = array_merge($par, $v->par ?? []);
727 723
                     }
728
-                    $sql .= implode(' AND ', $tmp) . ' ';
724
+                    $sql .= implode(' AND ', $tmp).' ';
729 725
                 }
730 726
             }
731 727
         }
@@ -734,12 +730,12 @@  discard block
 block discarded – undo
734 730
             if ($v->many) {
735 731
                 $jMany = true;
736 732
             }
737
-            $sql .= ($v->many ? 'LEFT ' : '' ) . 'JOIN '.$v->table->getFullName().' '.$k.' ON ';
733
+            $sql .= ($v->many ? 'LEFT ' : '').'JOIN '.$v->table->getFullName().' '.$k.' ON ';
738 734
             $tmp = [];
739 735
             foreach ($v->keymap as $kk => $vv) {
740 736
                 $tmp[] = $kk.' = '.$vv;
741 737
             }
742
-            $sql .= implode(' AND ', $tmp) . ' ';
738
+            $sql .= implode(' AND ', $tmp).' ';
743 739
         }
744 740
         if (!$jMany && !count($used_relations)) {
745 741
             $sql = str_replace('COUNT(DISTINCT ', 'COUNT(', $sql);
@@ -748,20 +744,20 @@  discard block
 block discarded – undo
748 744
             $sql .= 'WHERE ';
749 745
             $tmp = [];
750 746
             foreach ($w as $v) {
751
-                $tmp[] = '(' . $v[0] . ')';
747
+                $tmp[] = '('.$v[0].')';
752 748
                 $par = array_merge($par, $v[1]);
753 749
             }
754 750
             $sql .= implode(' AND ', $tmp).' ';
755 751
         }
756 752
         if (count($g)) {
757
-            $sql .= 'GROUP BY ' . $g[0] . ' ';
753
+            $sql .= 'GROUP BY '.$g[0].' ';
758 754
             $par = array_merge($par, $g[1]);
759 755
         }
760 756
         if (count($h)) {
761 757
             $sql .= 'HAVING ';
762 758
             $tmp = [];
763 759
             foreach ($h as $v) {
764
-                $tmp[] = '(' . $v[0] . ')';
760
+                $tmp[] = '('.$v[0].')';
765 761
                 $par = array_merge($par, $v[1]);
766 762
             }
767 763
             $sql .= implode(' AND ', $tmp).' ';
@@ -796,7 +792,7 @@  discard block
 block discarded – undo
796 792
                     $this->with(implode('.', $temp));
797 793
                     $table = array_reduce(
798 794
                         $temp,
799
-                        function ($carry, $item) use (&$table) {
795
+                        function($carry, $item) use (&$table) {
800 796
                             return $table->getRelation($item)->table;
801 797
                         }
802 798
                     );
@@ -805,7 +801,7 @@  discard block
 block discarded – undo
805 801
                 }
806 802
                 unset($fields[$k]);
807 803
                 foreach ($cols as $col) {
808
-                    $fields[] = $table . '.' . $col;
804
+                    $fields[] = $table.'.'.$col;
809 805
                 }
810 806
             }
811 807
         }
@@ -840,9 +836,9 @@  discard block
 block discarded – undo
840 836
         }
841 837
         $aliases = [];
842 838
         $aliases_ext = [];
843
-        $getAlias = function ($name) use (&$aliases, &$aliases_ext) {
839
+        $getAlias = function($name) use (&$aliases, &$aliases_ext) {
844 840
             // to bypass use: return $name;
845
-            $aliases[$name] = $aliases[$name] ?? 'alias' . static::SEP . count($aliases);
841
+            $aliases[$name] = $aliases[$name] ?? 'alias'.static::SEP.count($aliases);
846 842
             if (isset($aliases_ext[$name])) {
847 843
                 unset($aliases_ext[$name]);
848 844
             }
@@ -867,7 +863,7 @@  discard block
 block discarded – undo
867 863
         $h = $this->having;
868 864
         $o = $this->order;
869 865
         $g = $this->group;
870
-        $j = array_map(function ($v) {
866
+        $j = array_map(function($v) {
871 867
             return clone $v;
872 868
         }, $this->joins);
873 869
 
@@ -881,32 +877,32 @@  discard block
 block discarded – undo
881 877
                 continue;
882 878
             }
883 879
             foreach ($f as $kk => $field) {
884
-                if (strpos($field, $k . '.') === 0) {
885
-                    $f[$kk] = str_replace($k . '.', $getAlias($k) . '.', $field);
880
+                if (strpos($field, $k.'.') === 0) {
881
+                    $f[$kk] = str_replace($k.'.', $getAlias($k).'.', $field);
886 882
                 }
887 883
             }
888 884
             foreach ($w as $kk => $v) {
889
-                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $v[0])) {
890
-                    $w[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $v[0]);
885
+                if (preg_match('(\b'.preg_quote($k.'.').')i', $v[0])) {
886
+                    $w[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $v[0]);
891 887
                 }
892 888
             }
893 889
             foreach ($h as $kk => $v) {
894
-                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $v[0])) {
895
-                    $h[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $v[0]);
890
+                if (preg_match('(\b'.preg_quote($k.'.').')i', $v[0])) {
891
+                    $h[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $v[0]);
896 892
                 }
897 893
             }
898
-            if (isset($o[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $o[0])) {
899
-                $o[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $o[0]);
894
+            if (isset($o[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $o[0])) {
895
+                $o[0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $o[0]);
900 896
             }
901
-            if (isset($g[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $g[0])) {
902
-                $g[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $g[0]);
897
+            if (isset($g[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $g[0])) {
898
+                $g[0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $g[0]);
903 899
             }
904 900
             foreach ($j as $kk => $v) {
905 901
                 foreach ($v->keymap as $kkk => $vv) {
906
-                    if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv)) {
902
+                    if (preg_match('(\b'.preg_quote($k.'.').')i', $vv)) {
907 903
                         $j[$kk]->keymap[$kkk] = preg_replace(
908
-                            '(\b'.preg_quote($k . '.'). ')i',
909
-                            $getAlias($k) . '.',
904
+                            '(\b'.preg_quote($k.'.').')i',
905
+                            $getAlias($k).'.',
910 906
                             $vv
911 907
                         );
912 908
                     }
@@ -915,38 +911,38 @@  discard block
 block discarded – undo
915 911
         }
916 912
         foreach ($this->definition->getRelations() as $k => $relation) {
917 913
             foreach ($f as $kk => $field) {
918
-                if (strpos($field, $k . '.') === 0) {
919
-                    $relations[$k] = [ $relation, $table ];
920
-                    $f[$kk] = str_replace($k . '.', $getAlias($k) . '.', $field);
914
+                if (strpos($field, $k.'.') === 0) {
915
+                    $relations[$k] = [$relation, $table];
916
+                    $f[$kk] = str_replace($k.'.', $getAlias($k).'.', $field);
921 917
                 }
922 918
             }
923 919
             foreach ($w as $kk => $v) {
924
-                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $v[0])) {
925
-                    $relations[$k] = [ $relation, $table ];
926
-                    $w[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $v[0]);
920
+                if (preg_match('(\b'.preg_quote($k.'.').')i', $v[0])) {
921
+                    $relations[$k] = [$relation, $table];
922
+                    $w[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $v[0]);
927 923
                 }
928 924
             }
929 925
             foreach ($h as $kk => $v) {
930
-                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $v[0])) {
931
-                    $relations[$k] = [ $relation, $table ];
932
-                    $h[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $v[0]);
926
+                if (preg_match('(\b'.preg_quote($k.'.').')i', $v[0])) {
927
+                    $relations[$k] = [$relation, $table];
928
+                    $h[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $v[0]);
933 929
                 }
934 930
             }
935
-            if (isset($o[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $o[0])) {
936
-                $relations[$k] = [ $relation, $table ];
937
-                $o[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $o[0]);
931
+            if (isset($o[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $o[0])) {
932
+                $relations[$k] = [$relation, $table];
933
+                $o[0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $o[0]);
938 934
             }
939
-            if (isset($g[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $g[0])) {
940
-                $relations[$k] = [ $relation, $table ];
941
-                $g[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $g[0]);
935
+            if (isset($g[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $g[0])) {
936
+                $relations[$k] = [$relation, $table];
937
+                $g[0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $g[0]);
942 938
             }
943 939
             foreach ($j as $kk => $v) {
944 940
                 foreach ($v->keymap as $kkk => $vv) {
945
-                    if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv)) {
946
-                        $relations[$k] = [ $relation, $table ];
941
+                    if (preg_match('(\b'.preg_quote($k.'.').')i', $vv)) {
942
+                        $relations[$k] = [$relation, $table];
947 943
                         $j[$kk]->keymap[$kkk] = preg_replace(
948
-                            '(\b'.preg_quote($k . '.'). ')i',
949
-                            $getAlias($k) . '.',
944
+                            '(\b'.preg_quote($k.'.').')i',
945
+                            $getAlias($k).'.',
950 946
                             $vv
951 947
                         );
952 948
                     }
@@ -955,32 +951,32 @@  discard block
 block discarded – undo
955 951
         }
956 952
         foreach ($aliases_ext as $k => $alias) {
957 953
             foreach ($f as $kk => $field) {
958
-                if (strpos($field, $k . '.') === 0) {
959
-                    $f[$kk] = str_replace($k . '.', $alias . '.', $field);
954
+                if (strpos($field, $k.'.') === 0) {
955
+                    $f[$kk] = str_replace($k.'.', $alias.'.', $field);
960 956
                 }
961 957
             }
962 958
             foreach ($w as $kk => $v) {
963
-                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $v[0])) {
964
-                    $w[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $alias . '.', $v[0]);
959
+                if (preg_match('(\b'.preg_quote($k.'.').')i', $v[0])) {
960
+                    $w[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $alias.'.', $v[0]);
965 961
                 }
966 962
             }
967 963
             foreach ($h as $kk => $v) {
968
-                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $v[0])) {
969
-                    $h[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $alias . '.', $v[0]);
964
+                if (preg_match('(\b'.preg_quote($k.'.').')i', $v[0])) {
965
+                    $h[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $alias.'.', $v[0]);
970 966
                 }
971 967
             }
972
-            if (isset($o[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $o[0])) {
973
-                $o[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $alias . '.', $o[0]);
968
+            if (isset($o[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $o[0])) {
969
+                $o[0] = preg_replace('(\b'.preg_quote($k.'.').')i', $alias.'.', $o[0]);
974 970
             }
975
-            if (isset($g[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $g[0])) {
976
-                $g[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $alias . '.', $g[0]);
971
+            if (isset($g[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $g[0])) {
972
+                $g[0] = preg_replace('(\b'.preg_quote($k.'.').')i', $alias.'.', $g[0]);
977 973
             }
978 974
             foreach ($j as $kk => $v) {
979 975
                 foreach ($v->keymap as $kkk => $vv) {
980
-                    if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv)) {
976
+                    if (preg_match('(\b'.preg_quote($k.'.').')i', $vv)) {
981 977
                         $j[$kk]->keymap[$kkk] = preg_replace(
982
-                            '(\b'.preg_quote($k . '.'). ')i',
983
-                            $alias . '.',
978
+                            '(\b'.preg_quote($k.'.').')i',
979
+                            $alias.'.',
984 980
                             $vv
985 981
                         );
986 982
                     }
@@ -989,12 +985,12 @@  discard block
 block discarded – undo
989 985
         }
990 986
         $select = [];
991 987
         foreach ($f as $k => $field) {
992
-            $select[] = $field . (!is_numeric($k) ? ' ' . $k : '');
988
+            $select[] = $field.(!is_numeric($k) ? ' '.$k : '');
993 989
         }
994 990
         foreach ($this->withr as $name => $relation) {
995 991
             if ($relation[2]) {
996 992
                 foreach ($relation[0]->table->getColumns() as $column) {
997
-                    $select[] = $getAlias($name) . '.' . $column . ' ' . $getAlias($name . static::SEP . $column);
993
+                    $select[] = $getAlias($name).'.'.$column.' '.$getAlias($name.static::SEP.$column);
998 994
                 }
999 995
             }
1000 996
         }
@@ -1015,13 +1011,13 @@  discard block
 block discarded – undo
1015 1011
                 foreach ($v->keymap as $kk => $vv) {
1016 1012
                     $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' ';
1017 1013
                 }
1018
-                $sql .= implode(' AND ', $tmp) . ' ';
1014
+                $sql .= implode(' AND ', $tmp).' ';
1019 1015
                 $sql .= 'LEFT JOIN '.$v->table->getFullName().' '.$getAlias($relation).' ON ';
1020 1016
                 $tmp = [];
1021 1017
                 foreach ($v->pivot_keymap as $kk => $vv) {
1022 1018
                     $tmp[] = $getAlias($relation).'.'.$vv.' = '.$alias.'.'.$kk.' ';
1023 1019
                 }
1024
-                $sql .= implode(' AND ', $tmp) . ' ';
1020
+                $sql .= implode(' AND ', $tmp).' ';
1025 1021
             } else {
1026 1022
                 $alias = $getAlias($relation);
1027 1023
 
@@ -1031,22 +1027,22 @@  discard block
 block discarded – undo
1031 1027
                     $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' ';
1032 1028
                 }
1033 1029
                 if ($v->sql) {
1034
-                    $tmp[] = $v->sql . ' ';
1030
+                    $tmp[] = $v->sql.' ';
1035 1031
                     $par = array_merge($par, $v->par ?? []);
1036 1032
                 }
1037
-                $sql .= implode(' AND ', $tmp) . ' ';
1033
+                $sql .= implode(' AND ', $tmp).' ';
1038 1034
             }
1039 1035
         }
1040 1036
         foreach ($j as $k => $v) {
1041 1037
             if ($v->many) {
1042 1038
                 $many = true;
1043 1039
             }
1044
-            $sql .= ($v->many ? 'LEFT ' : '' ) . 'JOIN '.$v->table->getFullName().' '.$k.' ON ';
1040
+            $sql .= ($v->many ? 'LEFT ' : '').'JOIN '.$v->table->getFullName().' '.$k.' ON ';
1045 1041
             $tmp = [];
1046 1042
             foreach ($v->keymap as $kk => $vv) {
1047 1043
                 $tmp[] = $kk.' = '.$vv;
1048 1044
             }
1049
-            $sql .= implode(' AND ', $tmp) . ' ';
1045
+            $sql .= implode(' AND ', $tmp).' ';
1050 1046
         }
1051 1047
         if ($many && count($porder) && $this->li_of[2] === 1) {
1052 1048
             $ids = $this->ids();
@@ -1054,9 +1050,9 @@  discard block
 block discarded – undo
1054 1050
                 if (count($porder) > 1) {
1055 1051
                     $pkw = [];
1056 1052
                     foreach ($porder as $name) {
1057
-                        $pkw[] = $name . ' = ?';
1053
+                        $pkw[] = $name.' = ?';
1058 1054
                     }
1059
-                    $pkw = '(' . implode(' AND ', $pkw) . ')';
1055
+                    $pkw = '('.implode(' AND ', $pkw).')';
1060 1056
                     $pkp = [];
1061 1057
                     foreach ($ids as $id) {
1062 1058
                         foreach ($id as $p) {
@@ -1068,60 +1064,60 @@  discard block
 block discarded – undo
1068 1064
                         $pkp
1069 1065
                     ];
1070 1066
                 } else {
1071
-                    $w[] = [ $porder[0] . ' IN ('.implode(',', array_fill(0, count($ids), '?')).')', $ids ];
1067
+                    $w[] = [$porder[0].' IN ('.implode(',', array_fill(0, count($ids), '?')).')', $ids];
1072 1068
                 }
1073 1069
             } else {
1074
-                $w[] = [ '1=0', [] ];
1070
+                $w[] = ['1=0', []];
1075 1071
             }
1076 1072
         }
1077 1073
         if (count($w)) {
1078 1074
             $sql .= 'WHERE ';
1079 1075
             $tmp = [];
1080 1076
             foreach ($w as $v) {
1081
-                $tmp[] = '(' . $v[0] . ')';
1077
+                $tmp[] = '('.$v[0].')';
1082 1078
                 $par = array_merge($par, $v[1]);
1083 1079
             }
1084 1080
             $sql .= implode(' AND ', $tmp).' ';
1085 1081
         }
1086 1082
         if (count($g)) {
1087
-            $sql .= 'GROUP BY ' . $g[0] . ' ';
1083
+            $sql .= 'GROUP BY '.$g[0].' ';
1088 1084
             $par = array_merge($par, $g[1]);
1089 1085
         }
1090 1086
         if (count($h)) {
1091 1087
             $sql .= 'HAVING ';
1092 1088
             $tmp = [];
1093 1089
             foreach ($h as $v) {
1094
-                $tmp[] = '(' . $v[0] . ')';
1090
+                $tmp[] = '('.$v[0].')';
1095 1091
                 $par = array_merge($par, $v[1]);
1096 1092
             }
1097 1093
             $sql .= implode(' AND ', $tmp).' ';
1098 1094
         }
1099 1095
         $ordered = false;
1100 1096
         if (count($o)) {
1101
-            $sql .= 'ORDER BY ' . $o[0] . ' ';
1097
+            $sql .= 'ORDER BY '.$o[0].' ';
1102 1098
             $par = array_merge($par, $o[1]);
1103 1099
             $ordered = true;
1104 1100
         }
1105 1101
         if (!count($g) && count($porder)) {
1106 1102
             $pdir = (count($o) && strpos($o[0], 'DESC') !== false) ? 'DESC' : 'ASC';
1107
-            $porder = array_map(function ($v) use ($pdir) {
1108
-                return $v . ' ' . $pdir;
1103
+            $porder = array_map(function($v) use ($pdir) {
1104
+                return $v.' '.$pdir;
1109 1105
             }, $porder);
1110
-            $sql .= ($ordered ? ', ' : 'ORDER BY ') . implode(', ', $porder) . ' ';
1106
+            $sql .= ($ordered ? ', ' : 'ORDER BY ').implode(', ', $porder).' ';
1111 1107
             $ordered = true;
1112 1108
         }
1113 1109
         foreach ($this->withr as $k => $v) {
1114 1110
             if (isset($v[3])) {
1115
-                $sql .= ($ordered ? ', ' : 'ORDER BY ') . $getAlias($k) . '.' . $v[3] . ' ' . ($v[4] ? 'DESC' : 'ASC');
1111
+                $sql .= ($ordered ? ', ' : 'ORDER BY ').$getAlias($k).'.'.$v[3].' '.($v[4] ? 'DESC' : 'ASC');
1116 1112
                 $ordered = true;
1117 1113
             }
1118 1114
         }
1119 1115
         if ((!$many || $this->li_of[2] === 0 || !count($porder)) && $this->li_of[0]) {
1120 1116
             if ($this->db->driverName() === 'oracle') {
1121
-                if ((int)$this->db->driverOption('version', 0) >= 12) {
1122
-                    $sql .= 'OFFSET ' . $this->li_of[1] . ' ROWS FETCH NEXT ' . $this->li_of[0] . ' ROWS ONLY';
1117
+                if ((int) $this->db->driverOption('version', 0) >= 12) {
1118
+                    $sql .= 'OFFSET '.$this->li_of[1].' ROWS FETCH NEXT '.$this->li_of[0].' ROWS ONLY';
1123 1119
                 } else {
1124
-                    $f = array_map(function ($v) {
1120
+                    $f = array_map(function($v) {
1125 1121
                         $v = explode(' ', trim($v), 2);
1126 1122
                         if (count($v) === 2) {
1127 1123
                             return $v[1];
@@ -1129,16 +1125,16 @@  discard block
 block discarded – undo
1129 1125
                         $v = explode('.', $v[0], 2);
1130 1126
                         return count($v) === 2 ? $v[1] : $v[0];
1131 1127
                     }, $select);
1132
-                    $sql = "SELECT " . implode(', ', $f) . " 
1128
+                    $sql = "SELECT ".implode(', ', $f)." 
1133 1129
                             FROM (
1134 1130
                                 SELECT tbl__.*, rownum rnum__ FROM (
1135
-                                    " . $sql . "
1131
+                                    " . $sql."
1136 1132
                                 ) tbl__ 
1137
-                                WHERE rownum <= " . ($this->li_of[0] + $this->li_of[1]) . "
1133
+                                WHERE rownum <= " . ($this->li_of[0] + $this->li_of[1])."
1138 1134
                             ) WHERE rnum__ > " . $this->li_of[1];
1139 1135
                 }
1140 1136
             } else {
1141
-                $sql .= 'LIMIT ' . $this->li_of[0] . ' OFFSET ' . $this->li_of[1];
1137
+                $sql .= 'LIMIT '.$this->li_of[0].' OFFSET '.$this->li_of[1];
1142 1138
             }
1143 1139
         }
1144 1140
         return $this->qiterator = new TableQueryIterator(
@@ -1188,12 +1184,12 @@  discard block
 block discarded – undo
1188 1184
                 $ret[$k] = str_repeat(' ', 255);
1189 1185
                 $par[] = &$ret[$k];
1190 1186
             }
1191
-            $sql .= ' RETURNING ' . implode(',', $primary) .
1192
-                ' INTO ' . implode(',', array_fill(0, count($primary), '?'));
1187
+            $sql .= ' RETURNING '.implode(',', $primary).
1188
+                ' INTO '.implode(',', array_fill(0, count($primary), '?'));
1193 1189
             $this->db->query($sql, $par);
1194 1190
             return $ret;
1195 1191
         } elseif ($this->db->driverName() === 'postgre') {
1196
-            $sql .= ' RETURNING ' . implode(',', $primary);
1192
+            $sql .= ' RETURNING '.implode(',', $primary);
1197 1193
             return $this->db->one($sql, $par, false);
1198 1194
         } else {
1199 1195
             $ret = [];
@@ -1224,9 +1220,9 @@  discard block
 block discarded – undo
1224 1220
         }
1225 1221
         $sql = 'UPDATE '.$table.' SET ';
1226 1222
         $par = [];
1227
-        $sql .= implode(', ', array_map(function ($v) {
1228
-            return $v . ' = ?';
1229
-        }, array_keys($update))) . ' ';
1223
+        $sql .= implode(', ', array_map(function($v) {
1224
+            return $v.' = ?';
1225
+        }, array_keys($update))).' ';
1230 1226
         $par = array_merge($par, array_values($update));
1231 1227
         if (count($this->where)) {
1232 1228
             $sql .= 'WHERE ';
@@ -1235,7 +1231,7 @@  discard block
 block discarded – undo
1235 1231
                 $tmp[] = $v[0];
1236 1232
                 $par = array_merge($par, $v[1]);
1237 1233
             }
1238
-            $sql .= implode(' AND ', $tmp) . ' ';
1234
+            $sql .= implode(' AND ', $tmp).' ';
1239 1235
         }
1240 1236
         if (count($this->order)) {
1241 1237
             $sql .= $this->order[0];
@@ -1259,7 +1255,7 @@  discard block
 block discarded – undo
1259 1255
                 $tmp[] = $v[0];
1260 1256
                 $par = array_merge($par, $v[1]);
1261 1257
             }
1262
-            $sql .= implode(' AND ', $tmp) . ' ';
1258
+            $sql .= implode(' AND ', $tmp).' ';
1263 1259
         }
1264 1260
         if (count($this->order)) {
1265 1261
             $sql .= $this->order[0];
@@ -1290,15 +1286,15 @@  discard block
 block discarded – undo
1290 1286
             try {
1291 1287
                 $name = array_reduce(
1292 1288
                     $parts,
1293
-                    function ($carry, $item) use (&$table, $select) {
1289
+                    function($carry, $item) use (&$table, $select) {
1294 1290
                         if (!$table->hasRelation($item)) {
1295
-                            throw new DBException('Invalid relation name: '.$table->getName().' -> ' . $item);
1291
+                            throw new DBException('Invalid relation name: '.$table->getName().' -> '.$item);
1296 1292
                         }
1297 1293
                         $relation = $table->getRelation($item);
1298 1294
                         if (!$relation) {
1299
-                            throw new DBException('Invalid relation name: '.$table->getName().' -> ' . $item);
1295
+                            throw new DBException('Invalid relation name: '.$table->getName().' -> '.$item);
1300 1296
                         }
1301
-                        $name = $carry ? $carry . static::SEP . $item : $item;
1297
+                        $name = $carry ? $carry.static::SEP.$item : $item;
1302 1298
                         $this->withr[$name] = [
1303 1299
                             $relation,
1304 1300
                             $carry ?? $table->getName(),
@@ -1367,9 +1363,9 @@  discard block
 block discarded – undo
1367 1363
 
1368 1364
         $aliases = [];
1369 1365
         $aliases_ext = [];
1370
-        $getAlias = function ($name) use (&$aliases, &$aliases_ext) {
1366
+        $getAlias = function($name) use (&$aliases, &$aliases_ext) {
1371 1367
             // to bypass use: return $name;
1372
-            $aliases[$name] = $aliases[$name] ?? 'alias' . static::SEP . count($aliases);
1368
+            $aliases[$name] = $aliases[$name] ?? 'alias'.static::SEP.count($aliases);
1373 1369
             if (isset($aliases_ext[$name])) {
1374 1370
                 unset($aliases_ext[$name]);
1375 1371
             }
@@ -1389,7 +1385,7 @@  discard block
 block discarded – undo
1389 1385
         $w = $this->where;
1390 1386
         $h = $this->having;
1391 1387
         $o = $this->order;
1392
-        $j = array_map(function ($v) {
1388
+        $j = array_map(function($v) {
1393 1389
             return clone $v;
1394 1390
         }, $this->joins);
1395 1391
 
@@ -1398,24 +1394,24 @@  discard block
 block discarded – undo
1398 1394
                 continue;
1399 1395
             }
1400 1396
             foreach ($w as $kk => $v) {
1401
-                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $v[0])) {
1402
-                    $w[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $v[0]);
1397
+                if (preg_match('(\b'.preg_quote($k.'.').')i', $v[0])) {
1398
+                    $w[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $v[0]);
1403 1399
                 }
1404 1400
             }
1405 1401
             foreach ($h as $kk => $v) {
1406
-                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $v[0])) {
1407
-                    $h[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $v[0]);
1402
+                if (preg_match('(\b'.preg_quote($k.'.').')i', $v[0])) {
1403
+                    $h[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $v[0]);
1408 1404
                 }
1409 1405
             }
1410
-            if (isset($o[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $o[0])) {
1411
-                $o[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $o[0]);
1406
+            if (isset($o[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $o[0])) {
1407
+                $o[0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $o[0]);
1412 1408
             }
1413 1409
             foreach ($j as $kk => $v) {
1414 1410
                 foreach ($v->keymap as $kkk => $vv) {
1415
-                    if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv)) {
1411
+                    if (preg_match('(\b'.preg_quote($k.'.').')i', $vv)) {
1416 1412
                         $j[$kk]->keymap[$kkk] = preg_replace(
1417
-                            '(\b'.preg_quote($k . '.'). ')i',
1418
-                            $getAlias($k) . '.',
1413
+                            '(\b'.preg_quote($k.'.').')i',
1414
+                            $getAlias($k).'.',
1419 1415
                             $vv
1420 1416
                         );
1421 1417
                     }
@@ -1424,52 +1420,52 @@  discard block
 block discarded – undo
1424 1420
         }
1425 1421
         foreach ($this->definition->getRelations() as $k => $v) {
1426 1422
             foreach ($w as $kk => $vv) {
1427
-                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv[0])) {
1428
-                    $relations[$k] = [ $v, $table ];
1429
-                    $w[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vv[0]);
1423
+                if (preg_match('(\b'.preg_quote($k.'.').')i', $vv[0])) {
1424
+                    $relations[$k] = [$v, $table];
1425
+                    $w[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $vv[0]);
1430 1426
                 }
1431 1427
             }
1432
-            if (isset($o[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $o[0])) {
1433
-                $relations[$k] = [ $v, $table ];
1434
-                $o[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $o[0]);
1435
-                $o[2] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $o[2]);
1428
+            if (isset($o[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $o[0])) {
1429
+                $relations[$k] = [$v, $table];
1430
+                $o[0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $o[0]);
1431
+                $o[2] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $o[2]);
1436 1432
             }
1437 1433
             foreach ($h as $kk => $vv) {
1438
-                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv[0])) {
1439
-                    $relations[$k] = [ $v, $table ];
1440
-                    $h[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vv[0]);
1434
+                if (preg_match('(\b'.preg_quote($k.'.').')i', $vv[0])) {
1435
+                    $relations[$k] = [$v, $table];
1436
+                    $h[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $vv[0]);
1441 1437
                 }
1442 1438
             }
1443 1439
             foreach ($j as $kk => $vv) {
1444 1440
                 foreach ($vv->keymap as $kkk => $vvv) {
1445
-                    if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vvv)) {
1446
-                        $relations[$k] = [ $v, $table ];
1441
+                    if (preg_match('(\b'.preg_quote($k.'.').')i', $vvv)) {
1442
+                        $relations[$k] = [$v, $table];
1447 1443
                         $j[$kk]->keymap[$kkk] =
1448
-                            preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vvv);
1444
+                            preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $vvv);
1449 1445
                     }
1450 1446
                 }
1451 1447
             }
1452 1448
         }
1453 1449
         foreach ($aliases_ext as $k => $alias) {
1454 1450
             foreach ($w as $kk => $v) {
1455
-                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $v[0])) {
1456
-                    $w[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $alias . '.', $v[0]);
1451
+                if (preg_match('(\b'.preg_quote($k.'.').')i', $v[0])) {
1452
+                    $w[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $alias.'.', $v[0]);
1457 1453
                 }
1458 1454
             }
1459 1455
             foreach ($h as $kk => $v) {
1460
-                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $v[0])) {
1461
-                    $h[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $alias . '.', $v[0]);
1456
+                if (preg_match('(\b'.preg_quote($k.'.').')i', $v[0])) {
1457
+                    $h[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $alias.'.', $v[0]);
1462 1458
                 }
1463 1459
             }
1464
-            if (isset($o[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $o[0])) {
1465
-                $o[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $alias . '.', $o[0]);
1460
+            if (isset($o[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $o[0])) {
1461
+                $o[0] = preg_replace('(\b'.preg_quote($k.'.').')i', $alias.'.', $o[0]);
1466 1462
             }
1467 1463
             foreach ($j as $kk => $v) {
1468 1464
                 foreach ($v->keymap as $kkk => $vv) {
1469
-                    if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv)) {
1465
+                    if (preg_match('(\b'.preg_quote($k.'.').')i', $vv)) {
1470 1466
                         $j[$kk]->keymap[$kkk] = preg_replace(
1471
-                            '(\b'.preg_quote($k . '.'). ')i',
1472
-                            $alias . '.',
1467
+                            '(\b'.preg_quote($k.'.').')i',
1468
+                            $alias.'.',
1473 1469
                             $vv
1474 1470
                         );
1475 1471
                     }
@@ -1477,29 +1473,29 @@  discard block
 block discarded – undo
1477 1473
             }
1478 1474
         }
1479 1475
 
1480
-        $key = array_map(function ($v) use ($table) {
1481
-            return $table . '.' . $v;
1476
+        $key = array_map(function($v) use ($table) {
1477
+            return $table.'.'.$v;
1482 1478
         }, $this->pkey);
1483 1479
         $own = false;
1484 1480
         $dir = 'ASC';
1485 1481
         if (count($o)) {
1486 1482
             $dir = strpos($o[0], ' DESC') ? 'DESC' : 'ASC';
1487
-            $own = strpos($o[2], $table . '.') === 0;
1483
+            $own = strpos($o[2], $table.'.') === 0;
1488 1484
         }
1489 1485
 
1490 1486
         $dst = $key;
1491 1487
         if (count($o)) {
1492 1488
             if ($own) {
1493 1489
                 // if using own table - do not use max/min in order - that will prevent index usage
1494
-                $dst[] = $o[2] . ' orderbyfix___';
1490
+                $dst[] = $o[2].' orderbyfix___';
1495 1491
             } else {
1496
-                $dst[] = 'MAX(' . $o[2] . ') orderbyfix___';
1492
+                $dst[] = 'MAX('.$o[2].') orderbyfix___';
1497 1493
             }
1498 1494
         }
1499 1495
         $dst = array_unique($dst);
1500 1496
 
1501 1497
         $par = [];
1502
-        $sql  = 'SELECT DISTINCT '.implode(', ', $dst).' FROM '.$this->definition->getFullName().' ';
1498
+        $sql = 'SELECT DISTINCT '.implode(', ', $dst).' FROM '.$this->definition->getFullName().' ';
1503 1499
         foreach ($relations as $k => $v) {
1504 1500
             $table = $v[1] !== $this->definition->getName() ? $getAlias($v[1]) : $v[1];
1505 1501
             $v = $v[0];
@@ -1510,13 +1506,13 @@  discard block
 block discarded – undo
1510 1506
                 foreach ($v->keymap as $kk => $vv) {
1511 1507
                     $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' ';
1512 1508
                 }
1513
-                $sql .= implode(' AND ', $tmp) . ' ';
1509
+                $sql .= implode(' AND ', $tmp).' ';
1514 1510
                 $sql .= 'LEFT JOIN '.$v->table->getFullName().' '.$getAlias($k).' ON ';
1515 1511
                 $tmp = [];
1516 1512
                 foreach ($v->pivot_keymap as $kk => $vv) {
1517 1513
                     $tmp[] = $getAlias($k).'.'.$vv.' = '.$alias.'.'.$kk.' ';
1518 1514
                 }
1519
-                $sql .= implode(' AND ', $tmp) . ' ';
1515
+                $sql .= implode(' AND ', $tmp).' ';
1520 1516
             } else {
1521 1517
                 $alias = $getAlias($k);
1522 1518
                 $sql .= 'LEFT JOIN '.$v->table->getFullName().' '.$alias.' ON ';
@@ -1525,37 +1521,37 @@  discard block
 block discarded – undo
1525 1521
                     $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' ';
1526 1522
                 }
1527 1523
                 if ($v->sql) {
1528
-                    $tmp[] = $v->sql . ' ';
1524
+                    $tmp[] = $v->sql.' ';
1529 1525
                     $par = array_merge($par, $v->par ?? []);
1530 1526
                 }
1531
-                $sql .= implode(' AND ', $tmp) . ' ';
1527
+                $sql .= implode(' AND ', $tmp).' ';
1532 1528
             }
1533 1529
         }
1534 1530
         foreach ($j as $k => $v) {
1535
-            $sql .= ($v->many ? 'LEFT ' : '' ) . 'JOIN '.$v->table->getFullName().' '.$k.' ON ';
1531
+            $sql .= ($v->many ? 'LEFT ' : '').'JOIN '.$v->table->getFullName().' '.$k.' ON ';
1536 1532
             $tmp = [];
1537 1533
             foreach ($v->keymap as $kk => $vv) {
1538 1534
                 $tmp[] = $kk.' = '.$vv;
1539 1535
             }
1540
-            $sql .= implode(' AND ', $tmp) . ' ';
1536
+            $sql .= implode(' AND ', $tmp).' ';
1541 1537
         }
1542 1538
         if (count($w)) {
1543 1539
             $sql .= 'WHERE ';
1544 1540
             $tmp = [];
1545 1541
             foreach ($w as $v) {
1546
-                $tmp[] = '(' . $v[0] . ')';
1542
+                $tmp[] = '('.$v[0].')';
1547 1543
                 $par = array_merge($par, $v[1]);
1548 1544
             }
1549 1545
             $sql .= implode(' AND ', $tmp).' ';
1550 1546
         }
1551 1547
         if (!$own) {
1552
-            $sql .= 'GROUP BY ' . implode(', ', $key) . ' ';
1548
+            $sql .= 'GROUP BY '.implode(', ', $key).' ';
1553 1549
         }
1554 1550
         if (count($h)) {
1555 1551
             $sql .= 'HAVING ';
1556 1552
             $tmp = [];
1557 1553
             foreach ($h as $v) {
1558
-                $tmp[] = '(' . $v[0] . ')';
1554
+                $tmp[] = '('.$v[0].')';
1559 1555
                 $par = array_merge($par, $v[1]);
1560 1556
             }
1561 1557
             $sql .= implode(' AND ', $tmp).' ';
@@ -1563,38 +1559,38 @@  discard block
 block discarded – undo
1563 1559
         if (count($o)) {
1564 1560
             $sql .= 'ORDER BY ';
1565 1561
             if ($own) {
1566
-                $sql .= $o[2] . ' ' . $dir;
1562
+                $sql .= $o[2].' '.$dir;
1567 1563
             } else {
1568
-                $sql .= 'MAX('.$o[2].') ' . $dir;
1564
+                $sql .= 'MAX('.$o[2].') '.$dir;
1569 1565
             }
1570 1566
         }
1571 1567
         $porder = [];
1572 1568
         $pdir = (count($o) && strpos($o[0], 'DESC') !== false) ? 'DESC' : 'ASC';
1573 1569
         foreach ($this->definition->getPrimaryKey() as $field) {
1574
-            $porder[] = $this->getColumn($field)['name'] . ' ' . $pdir;
1570
+            $porder[] = $this->getColumn($field)['name'].' '.$pdir;
1575 1571
         }
1576 1572
         if (count($porder)) {
1577
-            $sql .= (count($o) ? ', ' : 'ORDER BY ') . implode(', ', $porder) . ' ';
1573
+            $sql .= (count($o) ? ', ' : 'ORDER BY ').implode(', ', $porder).' ';
1578 1574
         }
1579 1575
 
1580 1576
         if ($this->li_of[0]) {
1581 1577
             if ($this->db->driverName() === 'oracle') {
1582
-                if ((int)$this->db->driverOption('version', 0) >= 12) {
1583
-                    $sql .= 'OFFSET ' . $this->li_of[1] . ' ROWS FETCH NEXT ' . $this->li_of[0] . ' ROWS ONLY';
1578
+                if ((int) $this->db->driverOption('version', 0) >= 12) {
1579
+                    $sql .= 'OFFSET '.$this->li_of[1].' ROWS FETCH NEXT '.$this->li_of[0].' ROWS ONLY';
1584 1580
                 } else {
1585
-                    $sql = "SELECT " . implode(', ', $dst) . " 
1581
+                    $sql = "SELECT ".implode(', ', $dst)." 
1586 1582
                             FROM (
1587 1583
                                 SELECT tbl__.*, rownum rnum__ FROM (
1588
-                                    " . $sql . "
1584
+                                    " . $sql."
1589 1585
                                 ) tbl__ 
1590
-                                WHERE rownum <= " . ($this->li_of[0] + $this->li_of[1]) . "
1586
+                                WHERE rownum <= " . ($this->li_of[0] + $this->li_of[1])."
1591 1587
                             ) WHERE rnum__ > " . $this->li_of[1];
1592 1588
                 }
1593 1589
             } else {
1594
-                $sql .= 'LIMIT ' . $this->li_of[0] . ' OFFSET ' . $this->li_of[1];
1590
+                $sql .= 'LIMIT '.$this->li_of[0].' OFFSET '.$this->li_of[1];
1595 1591
             }
1596 1592
         }
1597
-        return array_map(function ($v) {
1593
+        return array_map(function($v) {
1598 1594
             if (array_key_exists('orderbyfix___', $v)) {
1599 1595
                 unset($v['orderbyfix___']);
1600 1596
             }
Please login to merge, or discard this patch.
src/driver/postgre/Schema.php 1 patch
Spacing   +29 added lines, -29 removed lines patch added patch discarded remove patch
@@ -29,8 +29,8 @@  discard block
 block discarded – undo
29 29
             $table = $temp[1];
30 30
         }
31 31
 
32
-        if (isset($tables[$schema . '.' . $table])) {
33
-            return $tables[$schema . '.' . $table];
32
+        if (isset($tables[$schema.'.'.$table])) {
33
+            return $tables[$schema.'.'.$table];
34 34
         }
35 35
 
36 36
         static $relationsT = null;
@@ -61,7 +61,7 @@  discard block
 block discarded – undo
61 61
                         (kc.table_schema = ? OR ct.table_schema = ?) AND
62 62
                         kc.table_name IS NOT NULL AND
63 63
                         kc.position_in_unique_constraint IS NOT NULL",
64
-                    [ $catalog, $main, $main ]
64
+                    [$catalog, $main, $main]
65 65
                 )
66 66
             )->toArray();
67 67
             foreach ($col as $row) {
@@ -71,8 +71,8 @@  discard block
 block discarded – undo
71 71
                 if ($row['referenced_table_schema'] !== $main) {
72 72
                     $additional[] = $row['referenced_table_schema'];
73 73
                 }
74
-                $relationsT[$row['table_schema'] . '.' . $row['table_name']][] = $row;
75
-                $relationsR[$row['referenced_table_schema'] . '.' . $row['referenced_table_name']][] = $row;
74
+                $relationsT[$row['table_schema'].'.'.$row['table_name']][] = $row;
75
+                $relationsR[$row['referenced_table_schema'].'.'.$row['referenced_table_name']][] = $row;
76 76
             }
77 77
             foreach (array_filter(array_unique($additional)) as $s) {
78 78
                 $col = Collection::from(
@@ -97,12 +97,12 @@  discard block
 block discarded – undo
97 97
                             (kc.table_schema = ? AND ct.table_schema = ?) AND
98 98
                             kc.table_name IS NOT NULL AND
99 99
                             kc.position_in_unique_constraint IS NOT NULL",
100
-                        [ $catalog, $s, $s ]
100
+                        [$catalog, $s, $s]
101 101
                     )
102 102
                 )->toArray();
103 103
                 foreach ($col as $row) {
104
-                    $relationsT[$row['table_schema'] . '.' . $row['table_name']][] = $row;
105
-                    $relationsR[$row['referenced_table_schema'] . '.' . $row['referenced_table_name']][] = $row;
104
+                    $relationsT[$row['table_schema'].'.'.$row['table_name']][] = $row;
105
+                    $relationsR[$row['referenced_table_schema'].'.'.$row['referenced_table_name']][] = $row;
106 106
                 }
107 107
             }
108 108
         }
@@ -114,12 +114,12 @@  discard block
 block discarded – undo
114 114
             ->query(
115 115
                 "SELECT * FROM information_schema.columns
116 116
                  WHERE table_name = ? AND table_schema = ? AND table_catalog = ?",
117
-                [ $table, $schema, $catalog ]
117
+                [$table, $schema, $catalog]
118 118
             ))
119
-            ->mapKey(function ($v): string {
119
+            ->mapKey(function($v): string {
120 120
                 return $v['column_name'];
121 121
             })
122
-            ->map(function ($v) {
122
+            ->map(function($v) {
123 123
                 $v['length'] = null;
124 124
                 if (!isset($v['data_type'])) {
125 125
                     return $v;
@@ -127,7 +127,7 @@  discard block
 block discarded – undo
127 127
                 switch ($v['data_type']) {
128 128
                     case 'character':
129 129
                     case 'character varying':
130
-                        $v['length'] = (int)$v['character_maximum_length'];
130
+                        $v['length'] = (int) $v['character_maximum_length'];
131 131
                         break;
132 132
                 }
133 133
                 $v['hidden'] = $v['is_identity'] !== 'YES' && $v['is_generated'] === 'ALWAYS';
@@ -135,13 +135,13 @@  discard block
 block discarded – undo
135 135
             })
136 136
             ->toArray();
137 137
         if (!count($columns)) {
138
-            throw new DBException('Table not found by name: ' . implode('.', [$schema,$table]));
138
+            throw new DBException('Table not found by name: '.implode('.', [$schema, $table]));
139 139
         }
140 140
         $pkname = Collection::from($this
141 141
             ->query(
142 142
                 "SELECT constraint_name FROM information_schema.table_constraints
143 143
                 WHERE table_name = ? AND constraint_type = ? AND table_schema = ? AND table_catalog = ?",
144
-                [ $table, 'PRIMARY KEY', $schema, $catalog ]
144
+                [$table, 'PRIMARY KEY', $schema, $catalog]
145 145
             ))
146 146
             ->pluck('constraint_name')
147 147
             ->value();
@@ -151,12 +151,12 @@  discard block
 block discarded – undo
151 151
                 ->query(
152 152
                     "SELECT column_name FROM information_schema.constraint_column_usage
153 153
                      WHERE table_name = ? AND constraint_name = ? AND table_schema = ? AND table_catalog = ?",
154
-                    [ $table, $pkname, $schema, $catalog ]
154
+                    [$table, $pkname, $schema, $catalog]
155 155
                 ))
156 156
                 ->pluck('column_name')
157 157
                 ->toArray();
158 158
         }
159
-        $tables[$schema . '.' .$table] = $definition = (new Table($table, $schema))
159
+        $tables[$schema.'.'.$table] = $definition = (new Table($table, $schema))
160 160
             ->addColumns($columns)
161 161
             ->setPrimaryKey($primary)
162 162
             ->setComment('');
@@ -166,8 +166,8 @@  discard block
 block discarded – undo
166 166
             // assuming current table is on the "one" end having "many" records in the referencing table
167 167
             // resulting in a "hasMany" or "manyToMany" relationship (if a pivot table is detected)
168 168
             $relations = [];
169
-            foreach ($relationsR[$schema . '.' . $table] ?? [] as $relation) {
170
-                $relations[$relation['constraint_name']]['table'] = $relation['table_schema'] . '.' .
169
+            foreach ($relationsR[$schema.'.'.$table] ?? [] as $relation) {
170
+                $relations[$relation['constraint_name']]['table'] = $relation['table_schema'].'.'.
171 171
                     $relation['table_name'];
172 172
                 $relations[$relation['constraint_name']]['keymap'][$relation['referenced_column_name']] =
173 173
                     $relation['column_name'];
@@ -184,11 +184,11 @@  discard block
 block discarded – undo
184 184
                 $usedcol = [];
185 185
                 if (count($columns)) {
186 186
                     foreach (Collection::from($relationsT[$data['table']] ?? [])
187
-                        ->filter(function ($v) use ($columns) {
187
+                        ->filter(function($v) use ($columns) {
188 188
                             return in_array($v['column_name'], $columns);
189 189
                         }) as $relation
190 190
                     ) {
191
-                        $foreign[$relation['constraint_name']]['table'] = $relation['referenced_table_schema'] . '.' .
191
+                        $foreign[$relation['constraint_name']]['table'] = $relation['referenced_table_schema'].'.'.
192 192
                             $relation['referenced_table_name'];
193 193
                         $foreign[$relation['constraint_name']]['keymap'][$relation['column_name']] =
194 194
                             $relation['referenced_column_name'];
@@ -208,7 +208,7 @@  discard block
 block discarded – undo
208 208
                         $definition->getName() == $relname ||
209 209
                         $definition->getColumn($relname)
210 210
                     ) {
211
-                        $relname = $orig . '_' . (++ $cntr);
211
+                        $relname = $orig.'_'.(++$cntr);
212 212
                     }
213 213
                     $definition->addRelation(
214 214
                         new TableRelation(
@@ -233,7 +233,7 @@  discard block
 block discarded – undo
233 233
                         $definition->getName() == $relname ||
234 234
                         $definition->getColumn($relname)
235 235
                     ) {
236
-                        $relname = $orig . '_' . (++ $cntr);
236
+                        $relname = $orig.'_'.(++$cntr);
237 237
                     }
238 238
                     $definition->addRelation(
239 239
                         new TableRelation(
@@ -250,8 +250,8 @@  discard block
 block discarded – undo
250 250
             // assuming current table is linked to "one" record in the referenced table
251 251
             // resulting in a "belongsTo" relationship
252 252
             $relations = [];
253
-            foreach ($relationsT[$schema . '.' . $table] ?? [] as $relation) {
254
-                $relations[$relation['constraint_name']]['table'] = $relation['referenced_table_schema'] . '.' .
253
+            foreach ($relationsT[$schema.'.'.$table] ?? [] as $relation) {
254
+                $relations[$relation['constraint_name']]['table'] = $relation['referenced_table_schema'].'.'.
255 255
                     $relation['referenced_table_name'];
256 256
                 $relations[$relation['constraint_name']]['keymap'][$relation['column_name']] =
257 257
                     $relation['referenced_column_name'];
@@ -268,7 +268,7 @@  discard block
 block discarded – undo
268 268
                     $definition->getName() == $relname ||
269 269
                     $definition->getColumn($relname)
270 270
                 ) {
271
-                    $relname = $orig . '_' . (++ $cntr);
271
+                    $relname = $orig.'_'.(++$cntr);
272 272
                 }
273 273
                 $definition->addRelation(
274 274
                     new TableRelation(
@@ -288,19 +288,19 @@  discard block
 block discarded – undo
288 288
         $tables = Collection::from($this
289 289
             ->query(
290 290
                 "SELECT table_name FROM information_schema.tables where table_schema = ? AND table_catalog = ?",
291
-                [ $this->connection['opts']['schema'] ?? 'public', $this->connection['name'] ]
291
+                [$this->connection['opts']['schema'] ?? 'public', $this->connection['name']]
292 292
             ))
293
-            ->mapKey(function ($v) {
293
+            ->mapKey(function($v) {
294 294
                 return strtolower($v['table_name']);
295 295
             })
296 296
             ->pluck('table_name')
297
-            ->map(function ($v) {
297
+            ->map(function($v) {
298 298
                 return $this->table($v)->toLowerCase();
299 299
             })
300 300
             ->toArray();
301 301
 
302 302
         foreach (array_keys($tables) as $k) {
303
-            $tables[($this->connection['opts']['schema'] ?? 'public') . '.' . $k] = &$tables[$k];
303
+            $tables[($this->connection['opts']['schema'] ?? 'public').'.'.$k] = &$tables[$k];
304 304
         }
305 305
         return $tables;
306 306
     }
Please login to merge, or discard this patch.