Skip to content

Commit e720715

Browse files
committed
Introduce table editor
1 parent 1548f28 commit e720715

File tree

4 files changed

+176
-0
lines changed

4 files changed

+176
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\DBAL\Schema\Exception;
6+
7+
use Doctrine\DBAL\Schema\SchemaException;
8+
use LogicException;
9+
10+
/** @psalm-immutable */
11+
final class InvalidTableDefinition extends LogicException implements SchemaException
12+
{
13+
public static function nameNotSet(): self
14+
{
15+
return new self('Table name is not set.');
16+
}
17+
}

src/Schema/Table.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,31 @@ public function getComment(): ?string
847847
return $this->_options['comment'] ?? null;
848848
}
849849

850+
/**
851+
* Instantiates a new table editor.
852+
*/
853+
public static function editor(): TableEditor
854+
{
855+
return new TableEditor();
856+
}
857+
858+
/**
859+
* Instantiates a new table editor and initializes it with the table's properties.
860+
*/
861+
public function edit(): TableEditor
862+
{
863+
return self::editor()
864+
->setName($this->getObjectName()->toString())
865+
->setColumns($this->_columns)
866+
->setIndexes($this->_indexes)
867+
->setUniqueConstraints($this->uniqueConstraints)
868+
->setForeignKeyConstraints($this->_fkConstraints)
869+
->setOptions($this->_options)
870+
->setConfiguration(
871+
new TableConfiguration($this->maxIdentifierLength),
872+
);
873+
}
874+
850875
/**
851876
* @param array<string|int, string> $columns
852877
* @param array<int, string> $flags

src/Schema/TableEditor.php

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\DBAL\Schema;
6+
7+
use Doctrine\DBAL\Schema\Exception\InvalidTableDefinition;
8+
9+
final class TableEditor
10+
{
11+
private ?string $name = null;
12+
13+
/** @var array<Column> */
14+
private array $columns = [];
15+
16+
/** @var array<Index> */
17+
private array $indexes = [];
18+
19+
/** @var array<UniqueConstraint> */
20+
private array $uniqueConstraints = [];
21+
22+
/** @var array<ForeignKeyConstraint> */
23+
private array $foreignKeyConstraints = [];
24+
25+
/** @var array<string, mixed> */
26+
private array $options = [];
27+
28+
private ?TableConfiguration $configuration = null;
29+
30+
/** @internal Use {@link Table::editor()} or {@link Table::edit()} to create an instance */
31+
public function __construct()
32+
{
33+
}
34+
35+
public function setName(string $name): self
36+
{
37+
$this->name = $name;
38+
39+
return $this;
40+
}
41+
42+
/** @param array<Column> $columns */
43+
public function setColumns(array $columns): self
44+
{
45+
$this->columns = $columns;
46+
47+
return $this;
48+
}
49+
50+
/** @param array<Index> $indexes */
51+
public function setIndexes(array $indexes): self
52+
{
53+
$this->indexes = $indexes;
54+
55+
return $this;
56+
}
57+
58+
/** @param array<UniqueConstraint> $uniqueConstraints */
59+
public function setUniqueConstraints(array $uniqueConstraints): self
60+
{
61+
$this->uniqueConstraints = $uniqueConstraints;
62+
63+
return $this;
64+
}
65+
66+
/** @param array<ForeignKeyConstraint> $foreignKeyConstraints */
67+
public function setForeignKeyConstraints(array $foreignKeyConstraints): self
68+
{
69+
$this->foreignKeyConstraints = $foreignKeyConstraints;
70+
71+
return $this;
72+
}
73+
74+
/** @param array<string, mixed> $options */
75+
public function setOptions(array $options): self
76+
{
77+
$this->options = $options;
78+
79+
return $this;
80+
}
81+
82+
public function setConfiguration(TableConfiguration $configuration): self
83+
{
84+
$this->configuration = $configuration;
85+
86+
return $this;
87+
}
88+
89+
public function create(): Table
90+
{
91+
if ($this->name === null) {
92+
throw InvalidTableDefinition::nameNotSet();
93+
}
94+
95+
return new Table(
96+
$this->name,
97+
$this->columns,
98+
$this->indexes,
99+
$this->uniqueConstraints,
100+
$this->foreignKeyConstraints,
101+
$this->options,
102+
$this->configuration,
103+
);
104+
}
105+
}

tests/Schema/TableEditorTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\DBAL\Tests\Schema;
6+
7+
use Doctrine\DBAL\Schema\Exception\InvalidTableDefinition;
8+
use Doctrine\DBAL\Schema\Table;
9+
use PHPUnit\Framework\TestCase;
10+
11+
class TableEditorTest extends TestCase
12+
{
13+
public function testSetName(): void
14+
{
15+
$table = (new Table('accounts'))
16+
->edit()
17+
->setName('contacts')
18+
->create();
19+
20+
self::assertSame('contacts', $table->getName());
21+
}
22+
23+
public function testNameNotSet(): void
24+
{
25+
$this->expectException(InvalidTableDefinition::class);
26+
27+
Table::editor()->create();
28+
}
29+
}

0 commit comments

Comments
 (0)