predefined entities + override database entity handler
This commit is contained in:
parent
cee54a1946
commit
5ac363d360
@ -112,6 +112,7 @@ abstract class DatabaseEntity implements ArrayAccess, JsonSerializable {
|
|||||||
|
|
||||||
public function preInsert(array &$row) { }
|
public function preInsert(array &$row) { }
|
||||||
public function postFetch(SQL $sql, array $row) { }
|
public function postFetch(SQL $sql, array $row) { }
|
||||||
|
public static function getPredefinedValues(SQL $sql): array { return []; }
|
||||||
|
|
||||||
public static function fromRow(SQL $sql, array $row): static {
|
public static function fromRow(SQL $sql, array $row): static {
|
||||||
$handler = self::getHandler($sql);
|
$handler = self::getHandler($sql);
|
||||||
@ -201,7 +202,7 @@ abstract class DatabaseEntity implements ArrayAccess, JsonSerializable {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getHandler(SQL $sql, $obj_or_class = null): DatabaseEntityHandler {
|
public static function getHandler(SQL $sql, $obj_or_class = null, $allowOverride = false): DatabaseEntityHandler {
|
||||||
|
|
||||||
if (!$obj_or_class) {
|
if (!$obj_or_class) {
|
||||||
$obj_or_class = get_called_class();
|
$obj_or_class = get_called_class();
|
||||||
@ -213,14 +214,16 @@ abstract class DatabaseEntity implements ArrayAccess, JsonSerializable {
|
|||||||
$class = $obj_or_class;
|
$class = $obj_or_class;
|
||||||
}
|
}
|
||||||
|
|
||||||
// if we are in an extending context, get the database handler for the root entity,
|
if (!$allowOverride) {
|
||||||
// as we do not persist attributes of the inheriting class
|
// if we are in an extending context, get the database handler for the root entity,
|
||||||
while ($class->getParentClass()->getName() !== DatabaseEntity::class) {
|
// as we do not persist attributes of the inheriting class
|
||||||
$class = $class->getParentClass();
|
while ($class->getParentClass()->getName() !== DatabaseEntity::class) {
|
||||||
|
$class = $class->getParentClass();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$handler = self::$handlers[$class->getShortName()] ?? null;
|
$handler = self::$handlers[$class->getShortName()] ?? null;
|
||||||
if (!$handler) {
|
if (!$handler || $allowOverride) {
|
||||||
$handler = new DatabaseEntityHandler($sql, $class);
|
$handler = new DatabaseEntityHandler($sql, $class);
|
||||||
self::$handlers[$class->getShortName()] = $handler;
|
self::$handlers[$class->getShortName()] = $handler;
|
||||||
}
|
}
|
||||||
|
@ -468,7 +468,8 @@ class DatabaseEntityHandler implements Persistable {
|
|||||||
$property->setAccessible(true);
|
$property->setAccessible(true);
|
||||||
$relEntities = $property->getValue($entity);
|
$relEntities = $property->getValue($entity);
|
||||||
foreach ($relEntities as $relEntity) {
|
foreach ($relEntities as $relEntity) {
|
||||||
$nmRow = [$entity->getId(), $relEntity->getId()];
|
$relEntityId = (is_int($relEntity) ? $relEntity : $relEntity->getId());
|
||||||
|
$nmRow = [$entity->getId(), $relEntityId];
|
||||||
if (!empty($dataColumns)) {
|
if (!empty($dataColumns)) {
|
||||||
foreach (array_keys($dataColumns[$thisTableName]) as $propertyName) {
|
foreach (array_keys($dataColumns[$thisTableName]) as $propertyName) {
|
||||||
$nmRow[] = $property->getName() === $propertyName;
|
$nmRow[] = $property->getName() === $propertyName;
|
||||||
@ -621,9 +622,20 @@ class DatabaseEntityHandler implements Persistable {
|
|||||||
public function getCreateQueries(SQL $sql): array {
|
public function getCreateQueries(SQL $sql): array {
|
||||||
|
|
||||||
$queries = [];
|
$queries = [];
|
||||||
|
$table = $this->getTableName();
|
||||||
|
|
||||||
|
// Create Table
|
||||||
$queries[] = $this->getTableQuery($sql);
|
$queries[] = $this->getTableQuery($sql);
|
||||||
|
|
||||||
$table = $this->getTableName();
|
// pre defined values
|
||||||
|
$getPredefinedValues = $this->entityClass->getMethod("getPredefinedValues");
|
||||||
|
$getPredefinedValues->setAccessible(true);
|
||||||
|
$predefinedValues = $getPredefinedValues->invoke(null, $sql);
|
||||||
|
if ($predefinedValues) {
|
||||||
|
$queries[] = $this->getInsertQuery($predefinedValues);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Entity Log
|
||||||
$entityLogConfig = $this->entityClass->getProperty("entityLogConfig");
|
$entityLogConfig = $this->entityClass->getProperty("entityLogConfig");
|
||||||
$entityLogConfig->setAccessible(true);
|
$entityLogConfig->setAccessible(true);
|
||||||
$entityLogConfig = $entityLogConfig->getValue();
|
$entityLogConfig = $entityLogConfig->getValue();
|
||||||
|
@ -36,4 +36,12 @@ class Group extends DatabaseEntity {
|
|||||||
|
|
||||||
return User::toJsonArray($users, ["id", "name", "fullName", "profilePicture"]);
|
return User::toJsonArray($users, ["id", "name", "fullName", "profilePicture"]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function getPredefinedValues(SQL $sql): array {
|
||||||
|
return [
|
||||||
|
new Group(Group::ADMIN, Group::GROUPS[Group::ADMIN], "#dc3545"),
|
||||||
|
new Group(Group::MODERATOR, Group::GROUPS[Group::MODERATOR], "#28a745"),
|
||||||
|
new Group(Group::SUPPORT, Group::GROUPS[Group::SUPPORT], "#007bff"),
|
||||||
|
];
|
||||||
|
}
|
||||||
}
|
}
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace Core\Objects\DatabaseEntity {
|
namespace Core\Objects\DatabaseEntity {
|
||||||
|
|
||||||
|
use Core\Driver\SQL\SQL;
|
||||||
use Core\Objects\DatabaseEntity\Attribute\MaxLength;
|
use Core\Objects\DatabaseEntity\Attribute\MaxLength;
|
||||||
use Core\Objects\DatabaseEntity\Attribute\Transient;
|
use Core\Objects\DatabaseEntity\Attribute\Transient;
|
||||||
use Core\Objects\DatabaseEntity\Controller\DatabaseEntity;
|
use Core\Objects\DatabaseEntity\Controller\DatabaseEntity;
|
||||||
@ -139,6 +140,13 @@ namespace Core\Objects\DatabaseEntity {
|
|||||||
public function hasModule(string $module): bool {
|
public function hasModule(string $module): bool {
|
||||||
return array_key_exists($module, $this->entries);
|
return array_key_exists($module, $this->entries);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function getPredefinedValues(SQL $sql): array {
|
||||||
|
return [
|
||||||
|
new Language(Language::AMERICAN_ENGLISH, "en_US", 'American English'),
|
||||||
|
new Language(Language::GERMAN_STANDARD, "de_DE", 'Deutsch Standard'),
|
||||||
|
];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,7 +24,8 @@ class ApiRoute extends Route {
|
|||||||
header("Content-Type: text/html");
|
header("Content-Type: text/html");
|
||||||
$document = new TemplateDocument($router, "swagger.twig");
|
$document = new TemplateDocument($router, "swagger.twig");
|
||||||
return $document->load();
|
return $document->load();
|
||||||
} else if (!preg_match("/[a-zA-Z]+/", $params["endpoint"])) {
|
} else if (!preg_match("/[a-zA-Z]+/", $params["endpoint"]) ||
|
||||||
|
!preg_match("/[a-zA-Z]*/", $params["method"])) {
|
||||||
http_response_code(400);
|
http_response_code(400);
|
||||||
$response = createError("Invalid Method");
|
$response = createError("Invalid Method");
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
Reference in New Issue
Block a user