Property Visibilities
This commit is contained in:
@@ -6,7 +6,9 @@ use ArrayAccess;
|
||||
use Core\Driver\SQL\Condition\Condition;
|
||||
use Core\Driver\SQL\Expression\Count;
|
||||
use Core\Driver\SQL\SQL;
|
||||
use Core\Objects\Context;
|
||||
use Core\Objects\DatabaseEntity\Attribute\Transient;
|
||||
use Core\Objects\DatabaseEntity\Attribute\Visibility;
|
||||
use JsonSerializable;
|
||||
|
||||
abstract class DatabaseEntity implements ArrayAccess, JsonSerializable {
|
||||
@@ -52,7 +54,61 @@ abstract class DatabaseEntity implements ArrayAccess, JsonSerializable {
|
||||
}
|
||||
}
|
||||
|
||||
public abstract function jsonSerialize(): array;
|
||||
public function jsonSerialize(?array $propertyNames = null): array {
|
||||
$properties = (new \ReflectionClass(get_called_class()))->getProperties();
|
||||
$ignoredProperties = ["entityLogConfig", "customData"];
|
||||
|
||||
$jsonArray = [];
|
||||
foreach ($properties as $property) {
|
||||
$property->setAccessible(true);
|
||||
$propertyName = $property->getName();
|
||||
if (in_array($propertyName, $ignoredProperties)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!empty($property->getAttributes(Transient::class))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$visibility = DatabaseEntityHandler::getAttribute($property, Visibility::class);
|
||||
if ($visibility) {
|
||||
$visibilityType = $visibility->getType();
|
||||
if ($visibilityType === Visibility::NONE) {
|
||||
continue;
|
||||
} else if ($visibilityType === Visibility::BY_GROUP) {
|
||||
$currentUser = Context::instance()->getUser();
|
||||
$groups = $visibility->getGroups();
|
||||
if (!empty($groups)) {
|
||||
if (!$currentUser || empty(array_intersect(array_keys($currentUser->getGroups()), $groups))) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($propertyNames === null || isset($propertyNames[$propertyName]) || in_array($propertyName, $propertyNames)) {
|
||||
if ($property->isInitialized($this)) {
|
||||
$value = $property->getValue($this);
|
||||
if ($value instanceof \DateTime) {
|
||||
$value = $value->getTimestamp();
|
||||
} else if ($value instanceof DatabaseEntity) {
|
||||
$subPropertyNames = $propertyNames[$propertyName] ?? null;
|
||||
$value = $value->jsonSerialize($subPropertyNames);
|
||||
}
|
||||
|
||||
$jsonArray[$property->getName()] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $jsonArray;
|
||||
}
|
||||
|
||||
public static function toJsonArray(array $entities, ?array $properties = null): array {
|
||||
return array_map(function ($entity) use ($properties) {
|
||||
return $entity->jsonSerialize($properties);
|
||||
}, $entities);
|
||||
}
|
||||
|
||||
public function preInsert(array &$row) { }
|
||||
public function postFetch(SQL $sql, array $row) { }
|
||||
|
||||
@@ -83,7 +83,7 @@ class DatabaseEntityHandler implements Persistable {
|
||||
}
|
||||
|
||||
$propertyType = $property->getType();
|
||||
$columnName = self::getColumnName($propertyName);
|
||||
$columnName = self::buildColumnName($propertyName);
|
||||
if (!($propertyType instanceof \ReflectionNamedType)) {
|
||||
$this->raiseError("Cannot persist class '$className': Property '$propertyName' has no valid type");
|
||||
}
|
||||
@@ -206,13 +206,13 @@ class DatabaseEntityHandler implements Persistable {
|
||||
}
|
||||
}
|
||||
|
||||
private static function getAttribute(\ReflectionProperty $property, string $attributeClass): ?object {
|
||||
public static function getAttribute(\ReflectionProperty $property, string $attributeClass): ?object {
|
||||
$attributes = $property->getAttributes($attributeClass);
|
||||
$attribute = array_shift($attributes);
|
||||
return $attribute?->newInstance();
|
||||
}
|
||||
|
||||
public static function getColumnName(string $propertyName): string {
|
||||
public static function buildColumnName(string $propertyName): string {
|
||||
// abcTestLOL => abc_test_lol
|
||||
return strtolower(preg_replace_callback("/([a-z])([A-Z]+)/", function ($m) {
|
||||
return $m[1] . "_" . strtolower($m[2]);
|
||||
@@ -239,10 +239,18 @@ class DatabaseEntityHandler implements Persistable {
|
||||
return $this->nmRelations;
|
||||
}
|
||||
|
||||
public function getColumnName(string $property): string {
|
||||
if ($property === "id") {
|
||||
return "$this->tableName.id";
|
||||
} else {
|
||||
return $this->tableName . "." . $this->columns[$property]->getName();
|
||||
}
|
||||
}
|
||||
|
||||
public function getColumnNames(): array {
|
||||
$columns = ["$this->tableName.id"];
|
||||
foreach ($this->columns as $column) {
|
||||
$columns[] = $this->tableName . "." . $column->getName();
|
||||
foreach (array_keys($this->columns) as $property) {
|
||||
$columns[] = $this->getColumnName($property);
|
||||
}
|
||||
|
||||
return $columns;
|
||||
@@ -286,7 +294,7 @@ class DatabaseEntityHandler implements Persistable {
|
||||
} else if ($column instanceof JsonColumn) {
|
||||
$value = json_decode($value);
|
||||
} else if (isset($this->relations[$propertyName])) {
|
||||
$relColumnPrefix = self::getColumnName($propertyName) . "_";
|
||||
$relColumnPrefix = self::buildColumnName($propertyName) . "_";
|
||||
if (array_key_exists($relColumnPrefix . "id", $row)) {
|
||||
$relId = $row[$relColumnPrefix . "id"];
|
||||
if ($relId !== null) {
|
||||
|
||||
@@ -42,7 +42,18 @@ class DatabaseEntityQuery extends Select {
|
||||
}
|
||||
}
|
||||
|
||||
public function addCustomValue(mixed $selectValue): Select {
|
||||
public function only(array $fields): DatabaseEntityQuery {
|
||||
if (!in_array("id", $fields)) {
|
||||
$fields[] = "id";
|
||||
}
|
||||
|
||||
$this->select(array_map(function ($field) {
|
||||
return $this->handler->getColumnName($field);
|
||||
}, $fields));
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addCustomValue(mixed $selectValue): DatabaseEntityQuery {
|
||||
if (is_string($selectValue)) {
|
||||
$this->additionalColumns[] = $selectValue;
|
||||
} else if ($selectValue instanceof Alias) {
|
||||
@@ -108,7 +119,7 @@ class DatabaseEntityQuery extends Select {
|
||||
$this->innerJoin($referencedTable, "$tableName.$foreignColumnName", "$alias.id", $alias);
|
||||
}
|
||||
|
||||
$relationColumnPrefix .= DatabaseEntityHandler::getColumnName($propertyName) . "_";
|
||||
$relationColumnPrefix .= DatabaseEntityHandler::buildColumnName($propertyName) . "_";
|
||||
$recursiveRelations = $relationHandler->getRelations();
|
||||
foreach ($relationHandler->getColumns() as $relPropertyName => $relColumn) {
|
||||
$relColumnName = $relColumn->getName();
|
||||
|
||||
@@ -34,7 +34,7 @@ class NMRelation implements Persistable {
|
||||
}
|
||||
|
||||
public function getIdColumn(DatabaseEntityHandler $handler): string {
|
||||
return DatabaseEntityHandler::getColumnName($handler->getTableName()) . "_id";
|
||||
return DatabaseEntityHandler::buildColumnName($handler->getTableName()) . "_id";
|
||||
}
|
||||
|
||||
public function getDataColumns(): array {
|
||||
@@ -56,8 +56,8 @@ class NMRelation implements Persistable {
|
||||
foreach ($this->properties as $tableName => $properties) {
|
||||
$columns[$tableName] = [];
|
||||
foreach ($properties as $property) {
|
||||
$columnName = DatabaseEntityHandler::getColumnName($tableName) . "_" .
|
||||
DatabaseEntityHandler::getColumnName($property->getName());
|
||||
$columnName = DatabaseEntityHandler::buildColumnName($tableName) . "_" .
|
||||
DatabaseEntityHandler::buildColumnName($property->getName());
|
||||
$columns[$tableName][$property->getName()] = $columnName;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user