ntentan /
yentu
| 1 | <?php |
||
| 2 | namespace yentu\database; |
||
| 3 | |||
| 4 | abstract class BasicKey extends DatabaseItem implements Commitable, Changeable //, Initializable |
||
| 5 | { |
||
| 6 | protected array $columns = []; |
||
| 7 | protected Table $table; |
||
| 8 | protected ?string $name; |
||
| 9 | |||
| 10 | public function __construct($columns, $table) |
||
| 11 | { |
||
| 12 | $this->columns = $columns; |
||
| 13 | $this->table = $table; |
||
| 14 | } |
||
| 15 | |||
| 16 | #[\Override] |
||
| 17 | public function isNew(): bool |
||
| 18 | { |
||
| 19 | if ($this->new === null) { |
||
| 20 | $this->new = !$this->doesKeyExist($this->buildDescription()); |
||
| 21 | } |
||
| 22 | return $this->new; |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 23 | } |
||
| 24 | |||
| 25 | abstract protected function doesKeyExist($constraint); |
||
| 26 | abstract protected function addKey($constraint); |
||
| 27 | abstract protected function dropKey($constraint); |
||
| 28 | abstract protected function getNamePostfix(); |
||
| 29 | |||
| 30 | #[\Override] |
||
| 31 | public function buildDescription() |
||
| 32 | { |
||
| 33 | return array( |
||
| 34 | 'table' => $this->table->getName(), |
||
| 35 | 'schema' => $this->table->getSchema()->getName(), |
||
| 36 | 'columns' => $this->columns, |
||
| 37 | 'name' => $this->name |
||
| 38 | ?? $this->table->getName() . '_' . implode('_', $this->columns) . '_' . $this->getNamePostfix() |
||
| 39 | ); |
||
| 40 | } |
||
| 41 | |||
| 42 | #[\Override] |
||
| 43 | public function commitNew() |
||
| 44 | { |
||
| 45 | $this->addKey($this->buildDescription()); |
||
| 46 | } |
||
| 47 | |||
| 48 | public function drop() |
||
| 49 | { |
||
| 50 | if (!$this->isNew()) { |
||
| 51 | $this->dropKey($this->buildDescription()); |
||
| 52 | } |
||
| 53 | return $this; |
||
| 54 | } |
||
| 55 | |||
| 56 | public function name($name) |
||
| 57 | { |
||
| 58 | $this->name = $name; |
||
| 59 | $this->new = !$this->doesKeyExist($this->buildDescription()); |
||
| 60 | return $this; |
||
| 61 | } |
||
| 62 | } |
||
| 63 | |||
| 64 |