WP-Database-Helper/beans/Schema.php
2025-02-24 10:54:51 +01:00

53 lines
1.3 KiB
PHP

<?php
namespace DatabaseHelper\beans;
use DatabaseHelper\enums\Aggregation;
use DatabaseHelper\enums\Charset;
use DatabaseHelper\enums\Collation;
use DatabaseHelper\enums\Engine;
use http\Exception\InvalidArgumentException;
class Schema
{
public string $name = '';
public Primary $id;
/**
* @var array<string, Column>
*/
public array $cols = [];
/**
* @var array<string, Reference>
*/
public array $refs = [];
public Engine $engine = Engine::INNODB;
public Charset $charset = Charset::UTF8;
public Collation $collation = Collation::UTF8_GENERAL_CI;
public function existsCol(string $name): bool {
return isset($this->cols[$name]);
}
public function reqCol(string $name): void {
if (!$this->existsCol($name))
throw new InvalidArgumentException("Column $name does not exist in schema $this->name.");
}
public function getCol(string $name): Column {
return $this->cols[$name];
}
public function existsRef(Schema $other): bool {
$otherId = $other->id;
// TODO
return true;
}
public function reqRef(Schema $other): void {
if (!$this->existsRef($other))
throw new InvalidArgumentException("A reference to $other->name is not defined in $this->name.");
}
}