fixup! feat(app framework): Add typed DB entities based on PHP reflection

Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
enhancement/typed-db-entity
Christoph Wurst 1 year ago
parent ad7924f1fd
commit 673211fd1d
No known key found for this signature in database
GPG Key ID: CC42AC2A7F0E56D8

@ -26,29 +26,43 @@ declare(strict_types=1);
namespace OCP\AppFramework\Db;
use ReflectionClass;
use function get_class;
class TypedEntity extends Entity {
/** @var string[][] */
static $reflectedFieldTypes = [];
public function __construct() {
$reflectedSelf = new ReflectionClass($this);
$fieldTypes = $this->getFieldTypes();
foreach ($reflectedSelf->getProperties() as $property) {
if (isset($fieldTypes[$property->getName()])) {
// Don't override
continue;
}
$propertyType = $property->getType();
if ($propertyType === null) {
// Can't derive
continue;
$class = get_class($this);
$cachedTypes = self::$reflectedFieldTypes[get_class($this)] ?? null;
if ($cachedTypes !== null) {
foreach ($cachedTypes as $name => $type) {
$this->addType($name, $type);
}
} else {
$reflectedSelf = new ReflectionClass($this);
$fieldTypes = $this->getFieldTypes();
self::$reflectedFieldTypes[$class] = [];
foreach ($reflectedSelf->getProperties() as $property) {
if (isset($fieldTypes[$property->getName()])) {
// Don't override
continue;
}
$propertyType = $property->getType();
if ($propertyType === null) {
// Can't derive
continue;
}
if (!$propertyType->isBuiltin()) {
// Complex type is not supported
continue;
}
if (!$propertyType->isBuiltin()) {
// Complex type is not supported
continue;
}
$this->addType($property->getName(), $propertyType->getName());
$this->addType($property->getName(), $propertyType->getName());
self::$reflectedFieldTypes[$class][$property->getName()] = $propertyType->getName();
}
}
}

Loading…
Cancel
Save