From d992d1a54dde0c0a4b0315f8725f0ac36498cdc3 Mon Sep 17 00:00:00 2001 From: Sergei Tigrov Date: Fri, 18 Oct 2024 18:09:29 +0700 Subject: [PATCH] Load column properties via constructor (#286) --- CHANGELOG.md | 1 + src/Column/ColumnBuilder.php | 3 +-- src/Column/ColumnFactory.php | 7 +++---- src/Schema.php | 6 +++--- 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 60c3ae3..741141d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ - New #282: Add `ColumnDefinitionBuilder` class (@Tigrov) - Bug #285: Fix `DMLQueryBuilder::insertBatch()` method (@Tigrov) - Enh #283: Refactor `Dsn` class (@Tigrov) +- Enh #286: Use constructor to create columns and initialize properties (@Tigrov) ## 1.3.0 March 21, 2024 diff --git a/src/Column/ColumnBuilder.php b/src/Column/ColumnBuilder.php index fda13e4..41e9457 100644 --- a/src/Column/ColumnBuilder.php +++ b/src/Column/ColumnBuilder.php @@ -31,7 +31,6 @@ public static function bigint(int|null $size = 20): ColumnSchemaInterface public static function binary(int|null $size = null): ColumnSchemaInterface { - return (new BinaryColumnSchema(ColumnType::BINARY)) - ->size($size); + return new BinaryColumnSchema(ColumnType::BINARY, size: $size); } } diff --git a/src/Column/ColumnFactory.php b/src/Column/ColumnFactory.php index 0d188e0..8916825 100644 --- a/src/Column/ColumnFactory.php +++ b/src/Column/ColumnFactory.php @@ -52,9 +52,7 @@ protected function getType(string $dbType, array $info = []): string $dbType = strtolower($dbType); if ($dbType === 'number') { - $scale = isset($info['scale']) ? (int) $info['scale'] : null; - - return match ($scale) { + return match ($info['scale'] ?? null) { null => ColumnType::DOUBLE, 0 => ColumnType::INTEGER, default => ColumnType::DECIMAL, @@ -73,7 +71,8 @@ protected function getType(string $dbType, array $info = []): string public function fromType(string $type, array $info = []): ColumnSchemaInterface { if ($type === ColumnType::BINARY) { - return (new BinaryColumnSchema($type))->load($info); + unset($info['type']); + return new BinaryColumnSchema($type, ...$info); } return parent::fromType($type, $info); diff --git a/src/Schema.php b/src/Schema.php index 7f6771c..9060c54 100644 --- a/src/Schema.php +++ b/src/Schema.php @@ -42,7 +42,7 @@ * column_name: string, * data_type: string, * data_scale: string|null, - * size: string, + * size: string|null, * nullable: string, * data_default: string|null, * is_pk: string|null, @@ -430,8 +430,8 @@ private function loadColumnSchema(array $info): ColumnSchemaInterface $dbType = $info['data_type']; $column = $columnFactory->fromDbType($dbType, [ - 'scale' => $info['data_scale'], - 'size' => $info['size'], + 'scale' => $info['data_scale'] !== null ? (int) $info['data_scale'] : null, + 'size' => $info['size'] !== null ? (int) $info['size'] : null, ]); /** @psalm-suppress DeprecatedMethod */ $column->name($info['column_name']);