diff --git a/src/Platforms/Data/Column.php b/src/Platforms/Data/Column.php index d5bc088..4681e21 100644 --- a/src/Platforms/Data/Column.php +++ b/src/Platforms/Data/Column.php @@ -11,17 +11,21 @@ class Column use StrictObjectTrait; - public string $name; - public string $type; - public ?int $size; - public ?string $default; - public bool $isPrimary; - public bool $isAutoincrement; - public bool $isUnsigned; - public bool $isNullable; - /** - * @var mixed[] - * @phpstan-var array - */ - public array $meta = []; + public function __construct( + public readonly string $name, + public readonly string $type, + public readonly ?int $size, + public readonly ?string $default, + public readonly bool $isPrimary, + public readonly bool $isAutoincrement, + public readonly bool $isUnsigned, + public readonly bool $isNullable, + /** + * @var mixed[] + * @phpstan-var array + */ + public array $meta = [], + ) + { + } } diff --git a/src/Platforms/Data/ForeignKey.php b/src/Platforms/Data/ForeignKey.php index f1370f0..d50f8de 100644 --- a/src/Platforms/Data/ForeignKey.php +++ b/src/Platforms/Data/ForeignKey.php @@ -11,12 +11,16 @@ class ForeignKey use StrictObjectTrait; - public string $name; - public string $schema; - public string $column; - public string $refTable; - public string $refTableSchema; - public string $refColumn; + public function __construct( + public readonly string $name, + public readonly string $schema, + public readonly string $column, + public readonly string $refTable, + public readonly string $refTableSchema, + public readonly string $refColumn, + ) + { + } public function getNameFqn(): string diff --git a/src/Platforms/Data/Table.php b/src/Platforms/Data/Table.php index 35fa659..8ee12e2 100644 --- a/src/Platforms/Data/Table.php +++ b/src/Platforms/Data/Table.php @@ -11,9 +11,13 @@ class Table use StrictObjectTrait; - public string $name; - public string $schema; - public bool $isView; + public function __construct( + public readonly string $name, + public readonly string $schema, + public readonly bool $isView = false, + ) + { + } /** diff --git a/src/Platforms/MySqlPlatform.php b/src/Platforms/MySqlPlatform.php index f984368..b416fa0 100644 --- a/src/Platforms/MySqlPlatform.php +++ b/src/Platforms/MySqlPlatform.php @@ -58,11 +58,11 @@ public function getTables(?string $schema = null): array $tables = []; foreach ($result as $row) { - $table = new Table(); - $table->name = (string) $row->TABLE_NAME; - $table->schema = (string) $row->TABLE_SCHEMA; - $table->isView = $row->TABLE_TYPE === 'VIEW'; - + $table = new Table( + name: (string) $row->TABLE_NAME, + schema: (string) $row->TABLE_SCHEMA, + isView: $row->TABLE_TYPE === 'VIEW', + ); $tables[$table->getUnescapedFqn()] = $table; } return $tables; @@ -81,17 +81,17 @@ public function getColumns(string $table, ?string $schema = null): array foreach ($query as $row) { $type = explode('(', (string) $row->Type); - $column = new Column(); - $column->name = (string) $row->Field; - $column->type = strtoupper($type[0]); - $column->size = isset($type[1]) ? (int) $type[1] : null; - $column->default = $row->Default !== null ? (string) $row->Default : null; - $column->isPrimary = $row->Key === 'PRI'; - $column->isAutoincrement = $row->Extra === 'auto_increment'; - $column->isUnsigned = (bool) strstr((string) $row->Type, 'unsigned'); - $column->isNullable = $row->Null === 'YES'; - $column->meta = []; - + $column = new Column( + name: (string) $row->Field, + type: strtoupper($type[0]), + size: isset($type[1]) ? (int) $type[1] : null, + default: $row->Default !== null ? (string) $row->Default : null, + isPrimary: $row->Key === 'PRI', + isAutoincrement: $row->Extra === 'auto_increment', + isUnsigned: (bool) strstr((string) $row->Type, 'unsigned'), + isNullable: $row->Null === 'YES', + meta: [], + ); $columns[$column->name] = $column; } return $columns; @@ -122,14 +122,14 @@ public function getForeignKeys(string $table, ?string $schema = null): array /** @var array $keys */ $keys = []; foreach ($result as $row) { - $foreignKey = new ForeignKey(); - $foreignKey->name = (string) $row->CONSTRAINT_NAME; - $foreignKey->schema = (string) $row->CONSTRAINT_SCHEMA; - $foreignKey->column = (string) $row->COLUMN_NAME; - $foreignKey->refTable = (string) $row->REFERENCED_TABLE_NAME; - $foreignKey->refTableSchema = (string) $row->REFERENCED_TABLE_SCHEMA; - $foreignKey->refColumn = (string) $row->REFERENCED_COLUMN_NAME; - + $foreignKey = new ForeignKey( + name: (string) $row->CONSTRAINT_NAME, + schema: (string) $row->CONSTRAINT_SCHEMA, + column: (string) $row->COLUMN_NAME, + refTable: (string) $row->REFERENCED_TABLE_NAME, + refTableSchema: (string) $row->REFERENCED_TABLE_SCHEMA, + refColumn: (string) $row->REFERENCED_COLUMN_NAME, + ); $keys[$foreignKey->column] = $foreignKey; } return $keys; diff --git a/src/Platforms/PostgreSqlPlatform.php b/src/Platforms/PostgreSqlPlatform.php index 3c5b0c9..1a39650 100644 --- a/src/Platforms/PostgreSqlPlatform.php +++ b/src/Platforms/PostgreSqlPlatform.php @@ -64,11 +64,11 @@ public function getTables(?string $schema = null): array $tables = []; foreach ($result as $row) { - $table = new Table(); - $table->name = (string) $row->name; - $table->schema = (string) $row->schema; - $table->isView = (bool) $row->is_view; - + $table = new Table( + name: (string) $row->name, + schema: (string) $row->schema, + isView: (bool) $row->is_view, + ); $tables[$table->getUnescapedFqn()] = $table; } return $tables; @@ -110,17 +110,17 @@ public function getColumns(string $table, ?string $schema = null): array $columns = []; foreach ($result as $row) { - $column = new Column(); - $column->name = (string) $row->name; - $column->type = (string) $row->type; - $column->size = $row->size !== null ? (int) $row->size : null; - $column->default = $row->default !== null ? (string) $row->default : null; - $column->isPrimary = (bool) $row->is_primary; - $column->isAutoincrement = (bool) $row->is_autoincrement; - $column->isUnsigned = false; - $column->isNullable = (bool) $row->is_nullable; - $column->meta = isset($row->sequence) ? ['sequence' => $row->sequence] : []; - + $column = new Column( + name: (string) $row->name, + type: (string) $row->type, + size: $row->size !== null ? (int) $row->size : null, + default: $row->default !== null ? (string) $row->default : null, + isPrimary: (bool) $row->is_primary, + isAutoincrement: (bool) $row->is_autoincrement, + isUnsigned: false, + isNullable: (bool) $row->is_nullable, + meta: isset($row->sequence) ? ['sequence' => $row->sequence] : [], + ); $columns[$column->name] = $column; } return $columns; @@ -157,14 +157,14 @@ public function getForeignKeys(string $table, ?string $schema = null): array $keys = []; foreach ($result as $row) { - $foreignKey = new ForeignKey(); - $foreignKey->name = (string) $row->name; - $foreignKey->schema = (string) $row->schema; - $foreignKey->column = (string) $row->column; - $foreignKey->refTable = (string) $row->ref_table; - $foreignKey->refTableSchema = (string) $row->ref_table_schema; - $foreignKey->refColumn = (string) $row->ref_column; - + $foreignKey = new ForeignKey( + name: (string) $row->name, + schema: (string) $row->schema, + column: (string) $row->column, + refTable: (string) $row->ref_table, + refTableSchema: (string) $row->ref_table_schema, + refColumn: (string) $row->ref_column, + ); $keys[$foreignKey->column] = $foreignKey; } return $keys; diff --git a/src/Platforms/SqlServerPlatform.php b/src/Platforms/SqlServerPlatform.php index b04692a..4d024e4 100644 --- a/src/Platforms/SqlServerPlatform.php +++ b/src/Platforms/SqlServerPlatform.php @@ -49,11 +49,11 @@ public function getTables(?string $schema = null): array $tables = []; foreach ($result as $row) { - $table = new Table(); - $table->name = (string) $row->TABLE_NAME; - $table->schema = (string) $row->TABLE_SCHEMA; - $table->isView = $row->TABLE_TYPE === 'VIEW'; - + $table = new Table( + name: (string) $row->TABLE_NAME, + schema: (string) $row->TABLE_SCHEMA, + isView: $row->TABLE_TYPE === 'VIEW', + ); $tables[$table->getUnescapedFqn()] = $table; } return $tables; @@ -105,17 +105,17 @@ public function getColumns(string $table, ?string $schema = null): array $columns = []; foreach ($result as $row) { - $column = new Column(); - $column->name = (string) $row->name; - $column->type = (string) $row->type; - $column->size = $row->size !== null ? (int) $row->size : null; - $column->default = $row->default !== null ? (string) $row->default : null; - $column->isPrimary = (bool) $row->is_primary; - $column->isAutoincrement = (bool) $row->is_autoincrement; - $column->isUnsigned = false; // not available in SqlServer - $column->isNullable = (bool) $row->is_nullable; - $column->meta = []; - + $column = new Column( + name: (string) $row->name, + type: (string) $row->type, + size: $row->size !== null ? (int) $row->size : null, + default: $row->default !== null ? (string) $row->default : null, + isPrimary: (bool) $row->is_primary, + isAutoincrement: (bool) $row->is_autoincrement, + isUnsigned: false, // not available in SqlServer + isNullable: (bool) $row->is_nullable, + meta: [], + ); $columns[$column->name] = $column; } @@ -154,14 +154,14 @@ public function getForeignKeys(string $table, ?string $schema = null): array $keys = []; foreach ($result as $row) { - $foreignKey = new ForeignKey(); - $foreignKey->name = (string) $row->name; - $foreignKey->schema = (string) $row->schema; - $foreignKey->column = (string) $row->column; - $foreignKey->refTable = (string) $row->ref_table; - $foreignKey->refTableSchema = (string) $row->ref_table_schema; - $foreignKey->refColumn = (string) $row->ref_column; - + $foreignKey = new ForeignKey( + name: (string) $row->name, + schema: (string) $row->schema, + column: (string) $row->column, + refTable: (string) $row->ref_table, + refTableSchema: (string) $row->ref_table_schema, + refColumn: (string) $row->ref_column, + ); $keys[$foreignKey->column] = $foreignKey; } return $keys;