diff --git a/CHANGELOG.md b/CHANGELOG.md index 947145e4..6abbcc85 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - Enh #315: Implement `ColumnSchemaInterface` classes according to the data type of database table columns for type casting performance. Related with yiisoft/db#752 (@Tigrov) - Chg #348: Replace call of `SchemaInterface::getRawTableName()` to `QuoterInterface::getRawTableName()` (@Tigrov) +- Enh #349: Add method chaining for column classes (@Tigrov) ## 1.3.0 March 21, 2024 diff --git a/composer.json b/composer.json index 16a72d36..fa7d17ba 100644 --- a/composer.json +++ b/composer.json @@ -41,7 +41,7 @@ "require-dev": { "maglnet/composer-require-checker": "^4.2", "phpunit/phpunit": "^9.5|^10.0", - "rector/rector": "^1.0", + "rector/rector": "^1.1.1", "roave/infection-static-analysis-plugin": "^1.16", "spatie/phpunit-watcher": "^1.23", "vimeo/psalm": "^4.30|^5.20", diff --git a/src/Column/ArrayColumnSchema.php b/src/Column/ArrayColumnSchema.php index 7d558c1e..43e2a299 100644 --- a/src/Column/ArrayColumnSchema.php +++ b/src/Column/ArrayColumnSchema.php @@ -45,9 +45,10 @@ public function __construct( /** * Set column of an array item. */ - public function column(ColumnSchemaInterface|null $column): void + public function column(ColumnSchemaInterface|null $column): static { $this->column = $column; + return $this; } /** @@ -88,9 +89,10 @@ public function getColumn(): ColumnSchemaInterface /** * Set dimension of an array, must be greater than 0. */ - public function dimension(int $dimension): void + public function dimension(int $dimension): static { $this->dimension = $dimension; + return $this; } /** diff --git a/src/Column/SequenceColumnSchemaInterface.php b/src/Column/SequenceColumnSchemaInterface.php index 0abb74ab..df228cd5 100644 --- a/src/Column/SequenceColumnSchemaInterface.php +++ b/src/Column/SequenceColumnSchemaInterface.php @@ -16,5 +16,5 @@ public function getSequenceName(): string|null; /** * Set the name of an associated sequence if a column is auto incremental. */ - public function sequenceName(string|null $sequenceName): void; + public function sequenceName(string|null $sequenceName): static; } diff --git a/src/Column/SequenceColumnSchemaTrait.php b/src/Column/SequenceColumnSchemaTrait.php index 1e8e645e..3beab37b 100644 --- a/src/Column/SequenceColumnSchemaTrait.php +++ b/src/Column/SequenceColumnSchemaTrait.php @@ -16,8 +16,9 @@ public function getSequenceName(): string|null return $this->sequenceName; } - public function sequenceName(string|null $sequenceName = null): void + public function sequenceName(string|null $sequenceName): static { $this->sequenceName = $sequenceName; + return $this; } } diff --git a/tests/ColumnSchemaTest.php b/tests/ColumnSchemaTest.php index fcbb9dca..5f290e1f 100644 --- a/tests/ColumnSchemaTest.php +++ b/tests/ColumnSchemaTest.php @@ -347,10 +347,12 @@ public function testIntegerColumnSchema() $intCol = new IntegerColumnSchema(); $this->assertNull($intCol->getSequenceName()); + $this->assertSame($intCol, $intCol->sequenceName('int_seq')); + $this->assertSame('int_seq', $intCol->getSequenceName()); - $intCol->sequenceName('int_seq'); + $intCol->sequenceName(null); - $this->assertSame('int_seq', $intCol->getSequenceName()); + $this->assertNull($intCol->getSequenceName()); } public function testBigIntColumnSchema() @@ -358,10 +360,12 @@ public function testBigIntColumnSchema() $bigintCol = new BigIntColumnSchema(); $this->assertNull($bigintCol->getSequenceName()); + $this->assertSame($bigintCol, $bigintCol->sequenceName('bigint_seq')); + $this->assertSame('bigint_seq', $bigintCol->getSequenceName()); - $bigintCol->sequenceName('bigint_seq'); + $bigintCol->sequenceName(null); - $this->assertSame('bigint_seq', $bigintCol->getSequenceName()); + $this->assertNull($bigintCol->getSequenceName()); } public function testArrayColumnSchema() @@ -378,4 +382,18 @@ public function testArrayColumnSchema() $arrayCol->dimension(2); $this->assertSame(2, $arrayCol->getDimension()); } + + public function testArrayColumnSchemaColumn(): void + { + $arrayCol = new ArrayColumnSchema(SchemaInterface::TYPE_STRING, SchemaInterface::PHP_TYPE_STRING); + $intCol = new IntegerColumnSchema(); + + $this->assertInstanceOf(StringColumnSchema::class, $arrayCol->getColumn()); + $this->assertSame($arrayCol, $arrayCol->column($intCol)); + $this->assertSame($intCol, $arrayCol->getColumn()); + + $arrayCol->column(null); + + $this->assertInstanceOf(StringColumnSchema::class, $arrayCol->getColumn()); + } }