Skip to content

Commit

Permalink
Add method chaining for columns (#349)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov authored Jun 23, 2024
1 parent 85c27c5 commit 48dbbe8
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 4 additions & 2 deletions src/Column/ArrayColumnSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Column/SequenceColumnSchemaInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
3 changes: 2 additions & 1 deletion src/Column/SequenceColumnSchemaTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
26 changes: 22 additions & 4 deletions tests/ColumnSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -347,21 +347,25 @@ 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()
{
$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()
Expand All @@ -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());
}
}

0 comments on commit 48dbbe8

Please sign in to comment.