name = $name; } public function requireColumn(string $col): void { if (!$this->existsColumn($col)) throw new InvalidArgumentException("Column '$col' is not defined."); } public function existsColumn(string $col): bool { return isset($this->columns[$col]); } public function existsReference(Schema $schema): bool { foreach ($this->foreignKeys as $foreignKey) if ($foreignKey['table'] == $schema->name) return true; return false; } public function countEntries(): int { return Database::makeQuery($this) ->aggregate('*', 'count', Aggregation::COUNT) ->query(); } public function columnType(string $col) { return $this->columns[$col]['type']; } public function primaryKey() { return $this->columns['primary']['name']; } public function copy(): Schema { $copy = new Schema($this->name); $copy->columns = genericDeepCopy($this->columns); $copy->primaryKey = genericDeepCopy($this->primaryKey); $copy->foreignKeys = genericDeepCopy($this->foreignKeys); $copy->engine = $this->engine; $copy->charset = $this->charset; $copy->collation = $this->collation; return $copy; } public function include(Schema $other): void { // Create a copy of the other schema. $otherCopy = $other->copy(); // Add any column that isn't already defined. foreach ($otherCopy->columns as $colName => $colDef) if (!isset($this->columns[$colName])) $this->columns[$colName] = $colDef; // Add any foreign key that doesn't already exist. foreach ($other->foreignKeys as $colName => $foreignKey) if (!isset($this->foreignKeys[$colName])) $this->foreignKeys[$colName] = $foreignKey; } }