From 5ac363d360cee5dfe8fc2261506d621a9f807108 Mon Sep 17 00:00:00 2001 From: Roman Date: Mon, 9 Jan 2023 17:09:57 +0100 Subject: [PATCH] predefined entities + override database entity handler --- .../Controller/DatabaseEntity.class.php | 15 +++++++++------ .../Controller/DatabaseEntityHandler.php | 16 ++++++++++++++-- Core/Objects/DatabaseEntity/Group.class.php | 8 ++++++++ Core/Objects/DatabaseEntity/Language.class.php | 8 ++++++++ Core/Objects/Router/ApiRoute.class.php | 3 ++- 5 files changed, 41 insertions(+), 9 deletions(-) diff --git a/Core/Objects/DatabaseEntity/Controller/DatabaseEntity.class.php b/Core/Objects/DatabaseEntity/Controller/DatabaseEntity.class.php index e239783..34556ce 100644 --- a/Core/Objects/DatabaseEntity/Controller/DatabaseEntity.class.php +++ b/Core/Objects/DatabaseEntity/Controller/DatabaseEntity.class.php @@ -112,6 +112,7 @@ abstract class DatabaseEntity implements ArrayAccess, JsonSerializable { public function preInsert(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 { $handler = self::getHandler($sql); @@ -201,7 +202,7 @@ abstract class DatabaseEntity implements ArrayAccess, JsonSerializable { 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) { $obj_or_class = get_called_class(); @@ -213,14 +214,16 @@ abstract class DatabaseEntity implements ArrayAccess, JsonSerializable { $class = $obj_or_class; } - // if we are in an extending context, get the database handler for the root entity, - // as we do not persist attributes of the inheriting class - while ($class->getParentClass()->getName() !== DatabaseEntity::class) { - $class = $class->getParentClass(); + if (!$allowOverride) { + // if we are in an extending context, get the database handler for the root entity, + // as we do not persist attributes of the inheriting class + while ($class->getParentClass()->getName() !== DatabaseEntity::class) { + $class = $class->getParentClass(); + } } $handler = self::$handlers[$class->getShortName()] ?? null; - if (!$handler) { + if (!$handler || $allowOverride) { $handler = new DatabaseEntityHandler($sql, $class); self::$handlers[$class->getShortName()] = $handler; } diff --git a/Core/Objects/DatabaseEntity/Controller/DatabaseEntityHandler.php b/Core/Objects/DatabaseEntity/Controller/DatabaseEntityHandler.php index f6a517f..b422c10 100644 --- a/Core/Objects/DatabaseEntity/Controller/DatabaseEntityHandler.php +++ b/Core/Objects/DatabaseEntity/Controller/DatabaseEntityHandler.php @@ -468,7 +468,8 @@ class DatabaseEntityHandler implements Persistable { $property->setAccessible(true); $relEntities = $property->getValue($entity); foreach ($relEntities as $relEntity) { - $nmRow = [$entity->getId(), $relEntity->getId()]; + $relEntityId = (is_int($relEntity) ? $relEntity : $relEntity->getId()); + $nmRow = [$entity->getId(), $relEntityId]; if (!empty($dataColumns)) { foreach (array_keys($dataColumns[$thisTableName]) as $propertyName) { $nmRow[] = $property->getName() === $propertyName; @@ -621,9 +622,20 @@ class DatabaseEntityHandler implements Persistable { public function getCreateQueries(SQL $sql): array { $queries = []; + $table = $this->getTableName(); + + // Create Table $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->setAccessible(true); $entityLogConfig = $entityLogConfig->getValue(); diff --git a/Core/Objects/DatabaseEntity/Group.class.php b/Core/Objects/DatabaseEntity/Group.class.php index 998c086..f12a81f 100644 --- a/Core/Objects/DatabaseEntity/Group.class.php +++ b/Core/Objects/DatabaseEntity/Group.class.php @@ -36,4 +36,12 @@ class Group extends DatabaseEntity { 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"), + ]; + } } \ No newline at end of file diff --git a/Core/Objects/DatabaseEntity/Language.class.php b/Core/Objects/DatabaseEntity/Language.class.php index 5e7f344..b3ded3a 100644 --- a/Core/Objects/DatabaseEntity/Language.class.php +++ b/Core/Objects/DatabaseEntity/Language.class.php @@ -2,6 +2,7 @@ namespace Core\Objects\DatabaseEntity { + use Core\Driver\SQL\SQL; use Core\Objects\DatabaseEntity\Attribute\MaxLength; use Core\Objects\DatabaseEntity\Attribute\Transient; use Core\Objects\DatabaseEntity\Controller\DatabaseEntity; @@ -139,6 +140,13 @@ namespace Core\Objects\DatabaseEntity { public function hasModule(string $module): bool { 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'), + ]; + } } } diff --git a/Core/Objects/Router/ApiRoute.class.php b/Core/Objects/Router/ApiRoute.class.php index 5e625a3..6630120 100644 --- a/Core/Objects/Router/ApiRoute.class.php +++ b/Core/Objects/Router/ApiRoute.class.php @@ -24,7 +24,8 @@ class ApiRoute extends Route { header("Content-Type: text/html"); $document = new TemplateDocument($router, "swagger.twig"); 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); $response = createError("Invalid Method"); } else {