Skip to content

Commit

Permalink
Add method chaining for columns
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov committed May 30, 2024
1 parent 85c27c5 commit faa9136
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
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 faa9136

Please sign in to comment.