diff --git a/CHANGELOG.md b/CHANGELOG.md index cf9826f1..03b822c3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ - Enh #362: Update according changes in `ColumnSchemaInterface` (@Tigrov) - New #364: Add `ColumnDefinitionBuilder` class (@Tigrov) - Enh #365: Refactor `Dsn` class (@Tigrov) +- Enh #366: 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 8778a3b3..0daaa57e 100644 --- a/src/Column/ColumnBuilder.php +++ b/src/Column/ColumnBuilder.php @@ -16,50 +16,41 @@ public static function boolean(): ColumnSchemaInterface public static function bit(int|null $size = null): ColumnSchemaInterface { - return (new BitColumnSchema(ColumnType::BIT)) - ->size($size); + return new BitColumnSchema(ColumnType::BIT, size: $size); } public static function tinyint(int|null $size = null): ColumnSchemaInterface { - return (new IntegerColumnSchema(ColumnType::TINYINT)) - ->size($size); + return new IntegerColumnSchema(ColumnType::TINYINT, size: $size); } public static function smallint(int|null $size = null): ColumnSchemaInterface { - return (new IntegerColumnSchema(ColumnType::SMALLINT)) - ->size($size); + return new IntegerColumnSchema(ColumnType::SMALLINT, size: $size); } public static function integer(int|null $size = null): ColumnSchemaInterface { - return (new IntegerColumnSchema(ColumnType::INTEGER)) - ->size($size); + return new IntegerColumnSchema(ColumnType::INTEGER, size: $size); } public static function bigint(int|null $size = null): ColumnSchemaInterface { - return (new IntegerColumnSchema(ColumnType::BIGINT)) - ->size($size); + return new IntegerColumnSchema(ColumnType::BIGINT, size: $size); } public static function binary(int|null $size = null): ColumnSchemaInterface { - return (new BinaryColumnSchema(ColumnType::BINARY)) - ->size($size); + return new BinaryColumnSchema(ColumnType::BINARY, size: $size); } public static function array(ColumnSchemaInterface|null $column = null): ColumnSchemaInterface { - return (new ArrayColumnSchema(ColumnType::ARRAY)) - ->column($column); + return new ArrayColumnSchema(ColumnType::ARRAY, column: $column); } public static function structured(string|null $dbType = null, array $columns = []): ColumnSchemaInterface { - return (new StructuredColumnSchema(ColumnType::STRUCTURED)) - ->dbType($dbType) - ->columns($columns); + return new StructuredColumnSchema(ColumnType::STRUCTURED, dbType: $dbType, columns: $columns); } } diff --git a/src/Column/ColumnFactory.php b/src/Column/ColumnFactory.php index 5e50b716..4ce4b7e9 100644 --- a/src/Column/ColumnFactory.php +++ b/src/Column/ColumnFactory.php @@ -120,33 +120,32 @@ final class ColumnFactory extends AbstractColumnFactory * @psalm-param ColumnInfo $info * @psalm-suppress MoreSpecificImplementedParamType * @psalm-suppress ArgumentTypeCoercion + * @psalm-suppress InvalidNamedArgument + * @psalm-suppress PossiblyInvalidArgument */ public function fromType(string $type, array $info = []): ColumnSchemaInterface { - $dimension = (int)($info['dimension'] ?? 0); + $dimension = $info['dimension'] ?? 0; + unset($info['dimension']); if ($dimension > 0) { - unset($info['dimension']); - $column = (new ArrayColumnSchema()) - ->dimension($dimension) - ->column($info['column'] ?? $this->fromType($type, $info)); - } else { - $column = match ($type) { - ColumnType::BOOLEAN => new BooleanColumnSchema($type), - ColumnType::BIT => new BitColumnSchema($type), - ColumnType::TINYINT => new IntegerColumnSchema($type), - ColumnType::SMALLINT => new IntegerColumnSchema($type), - ColumnType::INTEGER => new IntegerColumnSchema($type), - ColumnType::BIGINT => PHP_INT_SIZE !== 8 - ? new BigIntColumnSchema($type) - : new IntegerColumnSchema($type), - ColumnType::BINARY => new BinaryColumnSchema($type), - ColumnType::STRUCTURED => (new StructuredColumnSchema($type))->columns($info['columns'] ?? []), - default => parent::fromType($type, $info), - }; + $info['column'] ??= $this->fromType($type, $info); + return new ArrayColumnSchema(...$info, dimension: $dimension); } - return $column->load($info); + return match ($type) { + ColumnType::BOOLEAN => new BooleanColumnSchema($type, ...$info), + ColumnType::BIT => new BitColumnSchema($type, ...$info), + ColumnType::TINYINT => new IntegerColumnSchema($type, ...$info), + ColumnType::SMALLINT => new IntegerColumnSchema($type, ...$info), + ColumnType::INTEGER => new IntegerColumnSchema($type, ...$info), + ColumnType::BIGINT => PHP_INT_SIZE !== 8 + ? new BigIntColumnSchema($type, ...$info) + : new IntegerColumnSchema($type, ...$info), + ColumnType::BINARY => new BinaryColumnSchema($type, ...$info), + ColumnType::STRUCTURED => new StructuredColumnSchema($type, ...$info), + default => parent::fromType($type, $info), + }; } protected function getType(string $dbType, array $info = []): string diff --git a/src/Schema.php b/src/Schema.php index 7157a7e7..f184ff79 100644 --- a/src/Schema.php +++ b/src/Schema.php @@ -50,17 +50,17 @@ * data_type: string, * type_type: string|null, * type_scheme: string|null, - * character_maximum_length: int, + * character_maximum_length: int|string, * column_comment: string|null, - * is_nullable: bool, + * is_nullable: bool|string, * column_default: string|null, - * is_autoinc: bool, + * is_autoinc: bool|string, * sequence_name: string|null, * enum_values: string|null, - * size: int|null, - * scale: int|null, + * size: int|string|null, + * scale: int|string|null, * contype: string|null, - * dimension: int + * dimension: int|string * } * @psalm-type ConstraintArray = array< * array-key, @@ -742,26 +742,31 @@ private function loadColumnSchema(array $info): ColumnSchemaInterface $columns = $structured->getColumns(); } + /** @psalm-suppress ArgumentTypeCoercion */ $column = $columnFactory - ->fromType(ColumnType::STRUCTURED, ['dimension' => $info['dimension'], 'columns' => $columns]); + ->fromType(ColumnType::STRUCTURED, [ + 'columns' => $columns, + 'dbType' => $dbType, + 'dimension' => (int) $info['dimension'], + ]); } else { + /** @psalm-suppress ArgumentTypeCoercion */ $column = $columnFactory - ->fromDbType($dbType, ['dimension' => $info['dimension']]); + ->fromDbType($dbType, ['dimension' => (int) $info['dimension']]); } /** @psalm-suppress DeprecatedMethod */ $column->name($info['column_name']); - $column->dbType($dbType); $column->notNull(!$info['is_nullable']); - $column->autoIncrement($info['is_autoinc']); + $column->autoIncrement((bool) $info['is_autoinc']); $column->comment($info['column_comment']); $column->enumValues($info['enum_values'] !== null ? explode(',', str_replace(["''"], ["'"], $info['enum_values'])) : null); $column->primaryKey($info['contype'] === 'p'); $column->unique($info['contype'] === 'u'); - $column->scale($info['scale']); - $column->size($info['size']); + $column->scale($info['scale'] !== null ? (int) $info['scale'] : null); + $column->size($info['size'] !== null ? (int) $info['size'] : null); /** * pg_get_serial_sequence() doesn't track DEFAULT value change. @@ -781,10 +786,9 @@ private function loadColumnSchema(array $info): ColumnSchemaInterface } elseif ($column instanceof ArrayColumnSchema) { /** @var ColumnSchemaInterface $arrayColumn */ $arrayColumn = $column->getColumn(); - $arrayColumn->dbType($dbType); $arrayColumn->enumValues($column->getEnumValues()); - $arrayColumn->scale($info['scale']); - $arrayColumn->size($info['size']); + $arrayColumn->scale($column->getScale()); + $arrayColumn->size($column->getSize()); } $column->defaultValue($this->normalizeDefaultValue($defaultValue, $column));