diff --git a/src/Schema/Column/AbstractColumnSchema.php b/src/Schema/Column/AbstractColumnSchema.php index 4e2f2a517..db1a7267e 100644 --- a/src/Schema/Column/AbstractColumnSchema.php +++ b/src/Schema/Column/AbstractColumnSchema.php @@ -19,16 +19,16 @@ * ```php * use Yiisoft\Db\Schema\ColumnSchema; * - * $column = new ColumnSchema(); - * $column->name('id'); - * $column->allowNull(false); - * $column->dbType('int(11)'); - * $column->phpType('integer'); - * $column->type('integer'); - * $column->defaultValue(0); - * $column->autoIncrement(true); - * $column->primaryKey(true); - * `` + * $column = (new ColumnSchema()) + * ->name('id') + * ->allowNull(false) + * ->dbType('int(11)') + * ->phpType('integer') + * ->type('integer') + * ->defaultValue(0) + * ->autoIncrement() + * ->primaryKey(); + * ``` */ abstract class AbstractColumnSchema implements ColumnSchemaInterface { @@ -53,44 +53,52 @@ public function __construct( ) { } - public function allowNull(bool $value): void + public function allowNull(bool $allowNull = true): static { - $this->allowNull = $value; + $this->allowNull = $allowNull; + return $this; } - public function autoIncrement(bool $value): void + public function autoIncrement(bool $autoIncrement = true): static { - $this->autoIncrement = $value; + $this->autoIncrement = $autoIncrement; + return $this; } - public function comment(string|null $value): void + public function comment(string|null $comment): static { - $this->comment = $value; + $this->comment = $comment; + return $this; } - public function computed(bool $value): void + public function computed(bool $computed = true): static { - $this->computed = $value; + $this->computed = $computed; + return $this; } - public function dbType(string|null $value): void + public function dbType(string|null $dbType): static { - $this->dbType = $value; + $this->dbType = $dbType; + return $this; } - public function defaultValue(mixed $value): void + public function defaultValue(mixed $defaultValue): static { - $this->defaultValue = $value; + $this->defaultValue = $defaultValue; + return $this; } - public function enumValues(array|null $value): void + public function enumValues(array|null $enumValues): static { - $this->enumValues = $value; + $this->enumValues = $enumValues; + return $this; } - public function extra(string|null $value): void + public function extra(string|null $extra): static { - $this->extra = $value; + $this->extra = $extra; + return $this; } public function getComment(): string|null @@ -173,43 +181,51 @@ public function isUnsigned(): bool return $this->unsigned; } - public function name(string|null $name): void + public function name(string|null $name): static { $this->name = $name; + return $this; } - public function phpType(string|null $value): void + public function phpType(string|null $phpType): static { - $this->phpType = $value; + $this->phpType = $phpType; + return $this; } - public function precision(int|null $value): void + public function precision(int|null $precision): static { - $this->precision = $value; + $this->precision = $precision; + return $this; } - public function primaryKey(bool $value): void + public function primaryKey(bool $isPrimaryKey = true): static { - $this->isPrimaryKey = $value; + $this->isPrimaryKey = $isPrimaryKey; + return $this; } - public function scale(int|null $value): void + public function scale(int|null $scale): static { - $this->scale = $value; + $this->scale = $scale; + return $this; } - public function size(int|null $value): void + public function size(int|null $size): static { - $this->size = $value; + $this->size = $size; + return $this; } - public function type(string $value): void + public function type(string $type): static { - $this->type = $value; + $this->type = $type; + return $this; } - public function unsigned(bool $value): void + public function unsigned(bool $unsigned = true): static { - $this->unsigned = $value; + $this->unsigned = $unsigned; + return $this; } } diff --git a/src/Schema/Column/ColumnSchemaInterface.php b/src/Schema/Column/ColumnSchemaInterface.php index 82927ae26..a1b71fbe7 100644 --- a/src/Schema/Column/ColumnSchemaInterface.php +++ b/src/Schema/Column/ColumnSchemaInterface.php @@ -17,11 +17,11 @@ interface ColumnSchemaInterface * * ```php * $columns = [ - * 'description' => $this->text()->allowNull(true), + * 'description' => $this->text()->allowNull(), * ]; * ``` */ - public function allowNull(bool $value): void; + public function allowNull(bool $allowNull = true): static; /** * The database assigns auto incremented column a unique value automatically whenever you insert a new row into @@ -32,11 +32,11 @@ public function allowNull(bool $value): void; * * ```php * $columns = [ - * 'id' => $this->primaryKey()->autoIncrement(true), + * 'id' => $this->primaryKey()->autoIncrement(), * ]; * ``` */ - public function autoIncrement(bool $value): void; + public function autoIncrement(bool $autoIncrement = true): static; /** * The comment for a column in a database table. @@ -49,7 +49,7 @@ public function autoIncrement(bool $value): void; * ]; * ``` */ - public function comment(string|null $value): void; + public function comment(string|null $comment): static; /** * A computed column is a virtual column that computes its values from an expression. @@ -62,13 +62,13 @@ public function comment(string|null $value): void; * ]; * ``` */ - public function computed(bool $value): void; + public function computed(bool $computed = true): static; /** - * The database data-type of column. + * Sets a database data type for the column. * - * The data type can be one of the built-in data types supported by the database server (such as `INTEGER`, `VARCHAR`, - * `DATETIME`, etc.), a custom data type defined by the database server, or `null` if the database + * The data type can be one of the built-in data types supported by the database server (such as `INTEGER`, + * `VARCHAR`, `DATETIME`, etc.), a custom data type defined by the database server, or `null` if the database * allows untyped columns. * * ```php @@ -77,7 +77,7 @@ public function computed(bool $value): void; * ]; * ``` */ - public function dbType(string|null $value): void; + public function dbType(string|null $dbType): static; /** * Convert a value from its PHP representation to a database-specific representation. @@ -100,7 +100,7 @@ public function dbTypecast(mixed $value): mixed; * ]; * ``` */ - public function defaultValue(mixed $value): void; + public function defaultValue(mixed $defaultValue): static; /** * The list of possible values for the `ENUM` column. @@ -111,7 +111,7 @@ public function defaultValue(mixed $value): void; * ]; * ``` */ - public function enumValues(array|null $value): void; + public function enumValues(array|null $enumValues): static; /** * Extra SQL to append to the generated SQL for a column. @@ -125,7 +125,7 @@ public function enumValues(array|null $value): void; * ]; * ``` */ - public function extra(string|null $value): void; + public function extra(string|null $extra): static; /** * @return string|null The comment of the column. @@ -135,7 +135,7 @@ public function extra(string|null $value): void; public function getComment(): string|null; /** - * @return string|null The database type of the column. + * @return string|null The database data type of the column. * Null means the column has no type in the database. * * Note that the type includes size for columns supporting it, e.g. `varchar(128)`. The size can be obtained @@ -230,14 +230,14 @@ public function isAutoIncrement(): bool; public function isComputed(): bool; /** - * Whether this column is a primary key. + * Whether this column is a part of primary key. * * @see primaryKey() */ public function isPrimaryKey(): bool; /** - * Whether this column is unsigned. This is only meaningful when {@see type} is `smallint`, `integer` + * Whether this column is unsigned. This is only meaningful when {@see type} is `tinyint`, `smallint`, `integer` * or `bigint`. * * @see unsigned() @@ -253,7 +253,7 @@ public function isUnsigned(): bool; * ]; * ``` */ - public function name(string|null $name): void; + public function name(string|null $name): static; /** * The PHP data type for representing the data stored in the column. @@ -270,7 +270,7 @@ public function name(string|null $name): void; * ]; * ``` */ - public function phpType(string|null $value): void; + public function phpType(string|null $phpType): static; /** * Converts the input value according to {@see phpType} after retrieval from the database. @@ -288,7 +288,7 @@ public function phpTypecast(mixed $value): mixed; * 'price' => $this->decimal(10, 2)->precision(10), * ]; */ - public function precision(int|null $value): void; + public function precision(int|null $precision): static; /** * The primary key is a column or set of columns that uniquely identifies each row in a table. @@ -299,7 +299,7 @@ public function precision(int|null $value): void; * ]; * ``` */ - public function primaryKey(bool $value): void; + public function primaryKey(bool $isPrimaryKey = true): static; /** * The scale is the number of digits to the right of the decimal point and is only meaningful when {@see type} is @@ -311,12 +311,12 @@ public function primaryKey(bool $value): void; * ]; * ``` */ - public function scale(int|null $value): void; + public function scale(int|null $scale): static; /** * The size refers to the number of characters or digits allowed in a column of a database table. The size is - * typically used for character or numeric data types, such as `VARCHAR` or `INT`, to specify the maximum length or - * precision of the data in the column. + * typically used for character or numeric data types, such as `VARCHAR`, `INT` or DECIMAL, to specify the maximum + * length or precision of the data in the column. * * ```php * $columns = [ @@ -324,7 +324,7 @@ public function scale(int|null $value): void; * ]; * ``` */ - public function size(int|null $value): void; + public function size(int|null $size): static; /** * The database type of the column. @@ -334,7 +334,7 @@ public function size(int|null $value): void; * 'description' => $this->text()->type('text'), * ]; */ - public function type(string $value): void; + public function type(string $type): static; /** * Whether the column type is an unsigned integer. @@ -346,5 +346,5 @@ public function type(string $value): void; * ]; * ``` */ - public function unsigned(bool $value): void; + public function unsigned(bool $unsigned = true): static; } diff --git a/tests/Db/Schema/ColumnSchemaTest.php b/tests/Db/Schema/ColumnSchemaTest.php index 8af577b81..15088df34 100644 --- a/tests/Db/Schema/ColumnSchemaTest.php +++ b/tests/Db/Schema/ColumnSchemaTest.php @@ -20,14 +20,16 @@ public function testAllowNull(): void $column = new ColumnSchema(); $this->assertFalse($column->isAllowNull()); - - $column->allowNull(true); - + $this->assertSame($column, $column->allowNull()); $this->assertTrue($column->isAllowNull()); $column->allowNull(false); $this->assertFalse($column->isAllowNull()); + + $column->allowNull(true); + + $this->assertTrue($column->isAllowNull()); } public function testAutoIncrement(): void @@ -35,14 +37,16 @@ public function testAutoIncrement(): void $column = new ColumnSchema(); $this->assertFalse($column->isAutoIncrement()); - - $column->autoIncrement(true); - + $this->assertSame($column, $column->autoIncrement()); $this->assertTrue($column->isAutoIncrement()); $column->autoIncrement(false); $this->assertFalse($column->isAutoIncrement()); + + $column->autoIncrement(true); + + $this->assertTrue($column->isAutoIncrement()); } public function testComment(): void @@ -50,9 +54,7 @@ public function testComment(): void $column = new ColumnSchema(); $this->assertNull($column->getComment()); - - $column->comment('test'); - + $this->assertSame($column, $column->comment('test')); $this->assertSame('test', $column->getComment()); $column->comment(null); @@ -65,14 +67,16 @@ public function testComputed(): void $column = new ColumnSchema(); $this->assertFalse($column->isComputed()); - - $column->computed(true); - + $this->assertSame($column, $column->computed()); $this->assertTrue($column->isComputed()); $column->computed(false); $this->assertFalse($column->isComputed()); + + $column->computed(true); + + $this->assertTrue($column->isComputed()); } public function testDbType(): void @@ -80,9 +84,7 @@ public function testDbType(): void $column = new ColumnSchema(); $this->assertNull($column->getDbType()); - - $column->dbType('test'); - + $this->assertSame($column, $column->dbType('test')); $this->assertSame('test', $column->getDbType()); $column->dbType(null); @@ -95,9 +97,7 @@ public function testDefaultValue(): void $column = new ColumnSchema(); $this->assertNull($column->getDefaultValue()); - - $column->defaultValue('test'); - + $this->assertSame($column, $column->defaultValue('test')); $this->assertSame('test', $column->getDefaultValue()); $column->defaultValue(null); @@ -110,9 +110,7 @@ public function testEnumValues(): void $column = new ColumnSchema(); $this->assertNull($column->getEnumValues()); - - $column->enumValues(['positive', 'negative']); - + $this->assertSame($column, $column->enumValues(['positive', 'negative'])); $this->assertSame(['positive', 'negative'], $column->getEnumValues()); $column->enumValues([]); @@ -125,9 +123,7 @@ public function testExtra(): void $column = new ColumnSchema(); $this->assertNull($column->getExtra()); - - $column->extra('test'); - + $this->assertSame($column, $column->extra('test')); $this->assertSame('test', $column->getExtra()); $column->extra(''); @@ -140,9 +136,7 @@ public function testName(): void $column = new ColumnSchema(); $this->assertNull($column->getName()); - - $column->name('test'); - + $this->assertSame($column, $column->name('test')); $this->assertSame('test', $column->getName()); $column->name(''); @@ -155,9 +149,7 @@ public function testPhpType(): void $column = new ColumnSchema(); $this->assertNull($column->getPhpType()); - - $column->phpType(SchemaInterface::PHP_TYPE_STRING); - + $this->assertSame($column, $column->phpType(SchemaInterface::PHP_TYPE_STRING)); $this->assertSame(SchemaInterface::PHP_TYPE_STRING, $column->getPhpType()); $column->phpType(null); @@ -170,9 +162,7 @@ public function testPrecision(): void $column = new ColumnSchema(); $this->assertNull($column->getPrecision()); - - $column->precision(10); - + $this->assertSame($column, $column->precision(10)); $this->assertSame(10, $column->getPrecision()); $column->precision(0); @@ -185,14 +175,16 @@ public function testPrimaryKey(): void $column = new ColumnSchema(); $this->assertFalse($column->isPrimaryKey()); - - $column->primaryKey(true); - + $this->assertSame($column, $column->primaryKey()); $this->assertTrue($column->isPrimaryKey()); $column->primaryKey(false); $this->assertFalse($column->isPrimaryKey()); + + $column->primaryKey(true); + + $this->assertTrue($column->isPrimaryKey()); } public function testScale(): void @@ -200,9 +192,7 @@ public function testScale(): void $column = new ColumnSchema(); $this->assertNull($column->getScale()); - - $column->scale(10); - + $this->assertSame($column, $column->scale(10)); $this->assertSame(10, $column->getScale()); $column->scale(0); @@ -215,9 +205,7 @@ public function testSize(): void $column = new ColumnSchema(); $this->assertNull($column->getSize()); - - $column->size(10); - + $this->assertSame($column, $column->size(10)); $this->assertSame(10, $column->getSize()); $column->size(0); @@ -230,9 +218,7 @@ public function testType(): void $column = new ColumnSchema(); $this->assertSame('', $column->getType()); - - $column->type('test'); - + $this->assertSame($column, $column->type('test')); $this->assertSame('test', $column->getType()); $column->type(''); @@ -245,13 +231,15 @@ public function testUnsigned(): void $column = new ColumnSchema(); $this->assertFalse($column->isUnsigned()); - - $column->unsigned(true); - + $this->assertSame($column, $column->unsigned()); $this->assertTrue($column->isUnsigned()); $column->unsigned(false); $this->assertFalse($column->isUnsigned()); + + $column->unsigned(true); + + $this->assertTrue($column->isUnsigned()); } }