Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load column properties via constructor #286

Merged
merged 4 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 1 addition & 2 deletions src/Column/ColumnBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
7 changes: 3 additions & 4 deletions src/Column/ColumnFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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']);
Expand Down