Skip to content

Commit d992d1a

Browse files
authored
Load column properties via constructor (#286)
1 parent 51becbe commit d992d1a

File tree

4 files changed

+8
-9
lines changed

4 files changed

+8
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- New #282: Add `ColumnDefinitionBuilder` class (@Tigrov)
1919
- Bug #285: Fix `DMLQueryBuilder::insertBatch()` method (@Tigrov)
2020
- Enh #283: Refactor `Dsn` class (@Tigrov)
21+
- Enh #286: Use constructor to create columns and initialize properties (@Tigrov)
2122

2223
## 1.3.0 March 21, 2024
2324

src/Column/ColumnBuilder.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ public static function bigint(int|null $size = 20): ColumnSchemaInterface
3131

3232
public static function binary(int|null $size = null): ColumnSchemaInterface
3333
{
34-
return (new BinaryColumnSchema(ColumnType::BINARY))
35-
->size($size);
34+
return new BinaryColumnSchema(ColumnType::BINARY, size: $size);
3635
}
3736
}

src/Column/ColumnFactory.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ protected function getType(string $dbType, array $info = []): string
5252
$dbType = strtolower($dbType);
5353

5454
if ($dbType === 'number') {
55-
$scale = isset($info['scale']) ? (int) $info['scale'] : null;
56-
57-
return match ($scale) {
55+
return match ($info['scale'] ?? null) {
5856
null => ColumnType::DOUBLE,
5957
0 => ColumnType::INTEGER,
6058
default => ColumnType::DECIMAL,
@@ -73,7 +71,8 @@ protected function getType(string $dbType, array $info = []): string
7371
public function fromType(string $type, array $info = []): ColumnSchemaInterface
7472
{
7573
if ($type === ColumnType::BINARY) {
76-
return (new BinaryColumnSchema($type))->load($info);
74+
unset($info['type']);
75+
return new BinaryColumnSchema($type, ...$info);
7776
}
7877

7978
return parent::fromType($type, $info);

src/Schema.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
* column_name: string,
4343
* data_type: string,
4444
* data_scale: string|null,
45-
* size: string,
45+
* size: string|null,
4646
* nullable: string,
4747
* data_default: string|null,
4848
* is_pk: string|null,
@@ -430,8 +430,8 @@ private function loadColumnSchema(array $info): ColumnSchemaInterface
430430

431431
$dbType = $info['data_type'];
432432
$column = $columnFactory->fromDbType($dbType, [
433-
'scale' => $info['data_scale'],
434-
'size' => $info['size'],
433+
'scale' => $info['data_scale'] !== null ? (int) $info['data_scale'] : null,
434+
'size' => $info['size'] !== null ? (int) $info['size'] : null,
435435
]);
436436
/** @psalm-suppress DeprecatedMethod */
437437
$column->name($info['column_name']);

0 commit comments

Comments
 (0)