@@ -19,7 +19,7 @@ discard block |
||
| 19 | 19 | |
| 20 | 20 | public function __construct( |
| 21 | 21 | DBInterface $db, |
| 22 | - Table|string $table, |
|
| 22 | + Table | string $table, |
|
| 23 | 23 | bool $findRelations = false, |
| 24 | 24 | ?MapperInterface $mapper = null |
| 25 | 25 | ) { |
@@ -34,7 +34,7 @@ discard block |
||
| 34 | 34 | public function iterator(array $fields = null, array $collectionKey = null): Collection |
| 35 | 35 | { |
| 36 | 36 | return Collection::from(parent::iterator($fields, $collectionKey)) |
| 37 | - ->map(function ($v) { |
|
| 37 | + ->map(function($v) { |
|
| 38 | 38 | return $this->mapper->entity($v); |
| 39 | 39 | }) |
| 40 | 40 | ->values(); |
@@ -74,19 +74,19 @@ discard block |
||
| 74 | 74 | ]; |
| 75 | 75 | } |
| 76 | 76 | } |
| 77 | - $connection['type'] = isset($temp['scheme']) && strlen((string)$temp['scheme']) ? $temp['scheme'] : null; |
|
| 78 | - $connection['user'] = isset($temp['user']) && strlen((string)$temp['user']) ? $temp['user'] : null; |
|
| 79 | - $connection['pass'] = isset($temp['pass']) && strlen((string)$temp['pass']) ? $temp['pass'] : null; |
|
| 80 | - $connection['host'] = isset($temp['host']) && strlen((string)$temp['host']) ? $temp['host'] : null; |
|
| 81 | - $connection['name'] = isset($temp['path']) && strlen((string)$temp['path']) ? |
|
| 82 | - trim((string)$temp['path'], '/') : null; |
|
| 83 | - $connection['port'] = isset($temp['port']) && (int)$temp['port'] ? (int)$temp['port'] : null; |
|
| 84 | - if (isset($temp['query']) && strlen((string)$temp['query'])) { |
|
| 85 | - parse_str((string)$temp['query'], $connection['opts']); |
|
| 77 | + $connection['type'] = isset($temp['scheme']) && strlen((string) $temp['scheme']) ? $temp['scheme'] : null; |
|
| 78 | + $connection['user'] = isset($temp['user']) && strlen((string) $temp['user']) ? $temp['user'] : null; |
|
| 79 | + $connection['pass'] = isset($temp['pass']) && strlen((string) $temp['pass']) ? $temp['pass'] : null; |
|
| 80 | + $connection['host'] = isset($temp['host']) && strlen((string) $temp['host']) ? $temp['host'] : null; |
|
| 81 | + $connection['name'] = isset($temp['path']) && strlen((string) $temp['path']) ? |
|
| 82 | + trim((string) $temp['path'], '/') : null; |
|
| 83 | + $connection['port'] = isset($temp['port']) && (int) $temp['port'] ? (int) $temp['port'] : null; |
|
| 84 | + if (isset($temp['query']) && strlen((string) $temp['query'])) { |
|
| 85 | + parse_str((string) $temp['query'], $connection['opts']); |
|
| 86 | 86 | } |
| 87 | 87 | // create the driver |
| 88 | 88 | $connection['type'] = $aliases[$connection['type']] ?? $connection['type']; |
| 89 | - $tmp = '\\vakata\\database\\driver\\'.strtolower((string)$connection['type']).'\\Driver'; |
|
| 89 | + $tmp = '\\vakata\\database\\driver\\'.strtolower((string) $connection['type']).'\\Driver'; |
|
| 90 | 90 | if (!class_exists($tmp)) { |
| 91 | 91 | throw new DBException('Unknown DB backend'); |
| 92 | 92 | } |
@@ -124,7 +124,7 @@ discard block |
||
| 124 | 124 | $new = ''; |
| 125 | 125 | $par = array_values($par); |
| 126 | 126 | if (substr_count($sql, '?') === 2 && !is_array($par[0])) { |
| 127 | - $par = [ $par ]; |
|
| 127 | + $par = [$par]; |
|
| 128 | 128 | } |
| 129 | 129 | $parts = explode('??', $sql); |
| 130 | 130 | $index = 0; |
@@ -134,7 +134,7 @@ discard block |
||
| 134 | 134 | $index += count($tmp) - 1; |
| 135 | 135 | if (isset($par[$index])) { |
| 136 | 136 | if (!is_array($par[$index])) { |
| 137 | - $par[$index] = [ $par[$index] ]; |
|
| 137 | + $par[$index] = [$par[$index]]; |
|
| 138 | 138 | } |
| 139 | 139 | $params = $par[$index]; |
| 140 | 140 | array_splice($par, $index, 1, $params); |
@@ -142,7 +142,7 @@ discard block |
||
| 142 | 142 | $new .= implode(',', array_fill(0, count($params), '?')); |
| 143 | 143 | } |
| 144 | 144 | } |
| 145 | - return [ $new, $par ]; |
|
| 145 | + return [$new, $par]; |
|
| 146 | 146 | } |
| 147 | 147 | /** |
| 148 | 148 | * Run a query (prepare & execute). |
@@ -191,7 +191,7 @@ discard block |
||
| 191 | 191 | ): Collection { |
| 192 | 192 | $coll = Collection::from($this->query($sql, $par, $buff)); |
| 193 | 193 | if (($keys = $this->driver->option('mode')) && in_array($keys, ['strtoupper', 'strtolower'])) { |
| 194 | - $coll->map(function ($v) use ($keys) { |
|
| 194 | + $coll->map(function($v) use ($keys) { |
|
| 195 | 195 | $new = []; |
| 196 | 196 | foreach ($v as $k => $vv) { |
| 197 | 197 | $new[call_user_func($keys, $k)] = $vv; |
@@ -200,18 +200,18 @@ discard block |
||
| 200 | 200 | }); |
| 201 | 201 | } |
| 202 | 202 | if ($key !== null) { |
| 203 | - $coll->mapKey(function ($v) use ($key): int|string { |
|
| 203 | + $coll->mapKey(function($v) use ($key): int | string { |
|
| 204 | 204 | return $v[$key]; |
| 205 | 205 | }); |
| 206 | 206 | } |
| 207 | 207 | if ($skip) { |
| 208 | - $coll->map(function ($v) use ($key) { |
|
| 208 | + $coll->map(function($v) use ($key) { |
|
| 209 | 209 | unset($v[$key]); |
| 210 | 210 | return $v; |
| 211 | 211 | }); |
| 212 | 212 | } |
| 213 | 213 | if ($opti) { |
| 214 | - $coll->map(function ($v) { |
|
| 214 | + $coll->map(function($v) { |
|
| 215 | 215 | return count($v) === 1 ? current($v) : $v; |
| 216 | 216 | }); |
| 217 | 217 | } |
@@ -328,8 +328,7 @@ discard block |
||
| 328 | 328 | public function definition(string $table, bool $detectRelations = true) : Table |
| 329 | 329 | { |
| 330 | 330 | return isset($this->schema) ? |
| 331 | - $this->schema->getTable($table) : |
|
| 332 | - $this->driver->table($table, $detectRelations); |
|
| 331 | + $this->schema->getTable($table) : $this->driver->table($table, $detectRelations); |
|
| 333 | 332 | } |
| 334 | 333 | |
| 335 | 334 | public function hasSchema(): bool |
@@ -439,11 +438,11 @@ discard block |
||
| 439 | 438 | * @param class-string<T>|Table|string $table |
| 440 | 439 | * @return ($table is class-string ? MapperInterface<T> : MapperInterface<Entity>) |
| 441 | 440 | */ |
| 442 | - public function getMapper(Table|string $table): MapperInterface |
|
| 441 | + public function getMapper(Table | string $table): MapperInterface |
|
| 443 | 442 | { |
| 444 | 443 | if (is_string($table)) { |
| 445 | - if (isset($this->mappers['::' . $table])) { |
|
| 446 | - return $this->mappers['::' . $table]; |
|
| 444 | + if (isset($this->mappers['::'.$table])) { |
|
| 445 | + return $this->mappers['::'.$table]; |
|
| 447 | 446 | } |
| 448 | 447 | $table = $this->definition($table); |
| 449 | 448 | } |
@@ -452,14 +451,14 @@ discard block |
||
| 452 | 451 | } |
| 453 | 452 | return $this->mappers[$table->getFullName()] = new Mapper($this, $table); |
| 454 | 453 | } |
| 455 | - public function setMapper(Table|string $table, MapperInterface $mapper, ?string $class = null): static |
|
| 454 | + public function setMapper(Table | string $table, MapperInterface $mapper, ?string $class = null): static |
|
| 456 | 455 | { |
| 457 | 456 | if (is_string($table)) { |
| 458 | 457 | $table = $this->definition($table); |
| 459 | 458 | } |
| 460 | 459 | $this->mappers[$table->getFullName()] = $mapper; |
| 461 | 460 | if (isset($class)) { |
| 462 | - $this->mappers['::' . $class] = $mapper; |
|
| 461 | + $this->mappers['::'.$class] = $mapper; |
|
| 463 | 462 | } |
| 464 | 463 | return $this; |
| 465 | 464 | } |
@@ -470,11 +469,10 @@ discard block |
||
| 470 | 469 | ): TableQueryMapped { |
| 471 | 470 | return new TableQueryMapped($this, $this->definition($table), $findRelations, $mapper); |
| 472 | 471 | } |
| 473 | - public function __call(string $method, array $args): TableQuery|TableQueryMapped |
|
| 472 | + public function __call(string $method, array $args): TableQuery | TableQueryMapped |
|
| 474 | 473 | { |
| 475 | 474 | return ($args[0] ?? false) ? |
| 476 | - $this->tableMapped($method, $args[1] ?? false, $args[2] ?? null) : |
|
| 477 | - $this->table($method, $args[1] ?? false); |
|
| 475 | + $this->tableMapped($method, $args[1] ?? false, $args[2] ?? null) : $this->table($method, $args[1] ?? false); |
|
| 478 | 476 | } |
| 479 | 477 | public function findRelation(string $start, string $end): array |
| 480 | 478 | { |
@@ -510,12 +508,12 @@ discard block |
||
| 510 | 508 | $relations[$t] = $w; |
| 511 | 509 | } |
| 512 | 510 | if (!isset($schema[$name])) { |
| 513 | - $schema[$name] = [ 'edges' => [] ]; |
|
| 511 | + $schema[$name] = ['edges' => []]; |
|
| 514 | 512 | } |
| 515 | 513 | foreach ($relations as $t => $w) { |
| 516 | 514 | $schema[$name]['edges'][$t] = $w; |
| 517 | 515 | if (!isset($schema[$t])) { |
| 518 | - $schema[$t] = [ 'edges' => [] ]; |
|
| 516 | + $schema[$t] = ['edges' => []]; |
|
| 519 | 517 | } |
| 520 | 518 | $schema[$t]['edges'][$name] = $w; |
| 521 | 519 | } |
@@ -118,7 +118,7 @@ discard block |
||
| 118 | 118 | * @param class-string<T>|Table|string $table |
| 119 | 119 | * @return ($table is class-string ? MapperInterface<T> : MapperInterface<Entity>) |
| 120 | 120 | */ |
| 121 | - public function getMapper(Table|string $table): MapperInterface; |
|
| 121 | + public function getMapper(Table | string $table): MapperInterface; |
|
| 122 | 122 | /* |
| 123 | 123 | * @template T of Entity |
| 124 | 124 | * @param Table|string $table |
@@ -126,7 +126,7 @@ discard block |
||
| 126 | 126 | * @param null|class-string<T> $class |
| 127 | 127 | * @return static |
| 128 | 128 | */ |
| 129 | - public function setMapper(Table|string $table, MapperInterface $mapper, ?string $class = null): static; |
|
| 129 | + public function setMapper(Table | string $table, MapperInterface $mapper, ?string $class = null): static; |
|
| 130 | 130 | public function tableMapped( |
| 131 | 131 | string $table, |
| 132 | 132 | bool $findRelations = false, |
@@ -58,12 +58,12 @@ |
||
| 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 | */ |
@@ -27,7 +27,7 @@ discard block |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |