Model/src/EntityBlueprint.php

156 lines
3.9 KiB
PHP
Raw Normal View History

2024-11-08 17:12:46 +01:00
<?php
namespace Nest\Model;
use Nest\Types\Definitions\CarbonType;
use Nest\Types\Definitions\Integers\BigintType;
/**
* @template T
*/
class EntityBlueprint
{
/**
* Entity class.
* @var class-string<T>
*/
protected string $entityClass;
/**
* Database identifier of the entity.
* @var string|null
*/
protected ?string $database = null;
/**
* Database table name of the entity.
* @var string|null
*/
protected ?string $table = null;
/**
* Properties definition array.
* @var array<string, (ReadableFieldBlueprint|ReadableArrayBlueprint|ReadableEntityPropertyBlueprint)>
*/
protected array $properties = [];
/**
* @param class-string<T> $entityClass Entity class.
*/
public function __construct(string $entityClass)
{
$this->entityClass = $entityClass;
}
/**
* Set the database table name of the entity.
* @param string $table The database table name of the entity.
* @return $this
*/
public function setTable(string $table): static
{
$this->table = $table;
return $this;
}
/**
* Get the database table name of the entity.
* @return string|null The database table name.
*/
public function getTable(): ?string
{
return $this->table;
}
/**
* Set the database of the entity.
* @param string $databaseIdentifier The database identifier.
* @return $this
*/
public function setDatabase(string $databaseIdentifier): static
{
$this->database = $databaseIdentifier;
return $this;
}
/**
* Get the database of the entity.
* @return string|null The database identifier.
*/
public function getDatabase(): ?string
{
return $this->database ?? "default";
}
/**
* Get the defined properties.
* @return array<string, (ReadableFieldBlueprint|ReadableArrayBlueprint|ReadableEntityPropertyBlueprint)> The defined properties.
*/
public function getProperties(): array
{
return $this->properties;
}
public function field(string $name, string $type): FieldBlueprint
{
return $this->properties[$name] = (new ReadableFieldBlueprint())->type($type);
}
public function id(string $name = "id", string $type = BigintType::class): FieldBlueprint
{
return $this->foreignId($name, $type)->autoIncrement()->primary();
}
public function foreignId(string $name, string $type = BigintType::class): FieldBlueprint
{
return $this->field($name, $type)->unsigned()->index();
}
public function createdAt(string $name = "created_at", string $type = CarbonType::class): FieldBlueprint
{
return $this->field($name, $type)->required()->currentDateByDefault();
}
public function updatedAt(string $name = "updated_at", string $type = CarbonType::class): FieldBlueprint
{
return $this->field($name, $type)->required()->currentDateByDefault()->currentDateOnUpdate();
}
/**
* Define an array field.
* @param string $name The name of the field.
* @param string $type The type of the array values.
* @return ArrayBlueprint The array blueprint.
*/
public function array(string $name, string $type): ArrayBlueprint
{
return $this->properties[$name] = (new ReadableArrayBlueprint($this->entityClass, $name))->type($type);
}
/**
* Define an entity property.
* @param string $name The name of the property.
* @param string $class The class of the property.
* @param bool $multiple Whether the property is an array or not.
* @return EntityPropertyBlueprint The property blueprint.
*/
public function entity(string $name, string $class, bool $multiple = false): EntityPropertyBlueprint
{
return $this->properties[$name] = (new ReadableEntityPropertyBlueprint())->class($class)->multiple($multiple);
}
/**
* Define an entities array property.
* @param string $name The name of the property.
* @param string $class The class of the property.
* @return EntityPropertyBlueprint The property blueprint.
*/
public function entities(string $name, string $class): EntityPropertyBlueprint
{
return $this->entity($name, $class, true);
}
}