- WIP on Notes v2.x

This commit is contained in:
mcndave committed 2024-05-09 19:49:27 +00:00
1 parent 1dd9da6eb6
commit 75231f32b3
7 files changed
+104 -156

No files matched your search

+31 -27
View File
@@ -40,8 +40,10 @@ trait EntityTrait {
$entityResolver = $this->resolveEntity();
foreach($dataset as $key => $value) {
$field = $entityResolver->field(strtolower($key), EntityResolver::KEY_COLUMN_NAME, false) ?? null;
$field ??= $entityResolver->field(strtolower($key), EntityResolver::KEY_LC_ENTITY_NAME, false);
$field = $entityResolver->field(strtolower($key), EntityResolver::KEY_COLUMN_NAME, false) ?? $entityResolver->field(strtolower($key), EntityResolver::KEY_LC_ENTITY_NAME, false);
# Temp. fix, incoming patch soon
$type = $field->getTypes()[0];
if ( $field === null ) {
if ($this->entityStrictFieldsDeclaration ) {
@@ -52,12 +54,12 @@ trait EntityTrait {
}
}
elseif ( is_null($value) ) {
$this->{$field['name']} = null;
$this->{$field->name} = null;
}
elseif ( $field['type'] === 'array' ) {
elseif ( $field->expectType('array') ) {
if ( is_string($value)) {
if (substr($value, 0, 1) === "a") {
$this->{$field['name']} = unserialize($value);
$this->{$field->name} = unserialize($value);
}
else {
$data = json_decode($value, true);
@@ -66,41 +68,41 @@ trait EntityTrait {
throw new \Exception(sprintf("JSON error while decoding in EntityTrait : '%s' given %s", json_last_error_msg(), $value));
}
$this->{$field['name']} = $data;
$this->{$field->name} = $data;
}
}
elseif ( is_array($value) ) {
$this->{$field['name']} = $value;
$this->{$field->name} = $value;
}
}
elseif ( EntityField::isScalarType($field['type']) ) {
elseif ( EntityField::isScalarType($type->type) ) {
if ( $field['type'] === 'string' ) {
$annotation = $entityResolver->searchFieldAnnotation($field['name'], [ Attribute\Property\Field::class, Field::class ] );
if ( $type->type === 'string' ) {
$annotation = $entityResolver->searchFieldAnnotation($field->name, [ Attribute\Property\Field::class ] );
if ( $annotation->length ?? null ) {
$value = mb_substr($value, 0, $annotation->length);
}
}
elseif ( $field['type'] === 'bool' ) {
elseif ( $type->type === 'bool' ) {
$value = (bool) $value;
}
$this->{$field['name']} = $value;
$this->{$field->name} = $value;
}
elseif ( $value instanceof \UnitEnum ) {
$this->{$field['name']} = $value;
$this->{$field->name} = $value;
}
elseif (enum_exists($field['type'])) {
$this->{$field['name']} = $field['type']::from($value);
elseif (enum_exists($type->type)) {
$this->{$field->name} = $type->type::from($value);
}
elseif ( ! $field['builtin'] ) {
elseif ( ! $type->builtIn ) {
try {
$this->{$field['name']} = Ulmus::instanciateObject($field['type'], [ $value ]);
$this->{$field->name} = Ulmus::instanciateObject($type->type, [ $value ]);
}
catch(\Error $e) {
$f = $field['type'];
throw new \Error(sprintf("%s for class '%s' on field '%s'", $e->getMessage(), get_class($this), $field['name']));
$f = $type->type;
throw new \Error(sprintf("%s for class '%s' on field '%s'", $e->getMessage(), get_class($this), $field->name));
}
}
@@ -123,9 +125,9 @@ trait EntityTrait {
#[Ignore]
public function resetVirtualProperties() : self
{
foreach($this->resolveEntity()->properties as $prop => $property) {
foreach($property['tags'] as $tag) {
if ( in_array(strtolower($tag['tag']), [ 'relation', 'join', 'virtual' ] ) ) {
foreach($this->resolveEntity()->reflectedClass->getProperties(true) as $prop => $property) {
foreach($property->attributes as $tag) {
if ( in_array(strtolower($tag->tag), [ 'relation', 'join', 'virtual' ] ) ) {
unset($this->$prop);
}
}
@@ -155,7 +157,7 @@ trait EntityTrait {
$entityResolver = $this->resolveEntity();
foreach($entityResolver->fieldList(Common\EntityResolver::KEY_ENTITY_NAME, true) as $key => $field) {
$annotation = $entityResolver->searchFieldAnnotation($key, [ Attribute\Property\Field::class, Field::class ]);
$annotation = $entityResolver->searchFieldAnnotation($key, [ Attribute\Property\Field::class ]);
if ( isset($this->$key) ) {
$dataset[$annotation->name ?? $key] = $rewriteValue ?
@@ -163,13 +165,13 @@ trait EntityTrait {
:
$this->$key;
}
elseif ( $field['nullable'] ) {
elseif ( $field->allowsNull() ) {
$dataset[$annotation->name ?? $key] = null;
}
}
if ($includeRelations) {
foreach($entityResolver->properties as $name => $field){
foreach($entityResolver->reflectedClass->getProperties(true) as $name => $field){
$relation = $entityResolver->searchFieldAnnotation($name, [ Attribute\Property\Relation::class ] );
if ($relation) {
@@ -178,7 +180,7 @@ trait EntityTrait {
if ($ignore && $ignore->ignoreExport) {
if ( $relation->isOneToOne() ) {
# empty object
$dataset[$name] = ( new \ReflectionClass($field['type']) )->newInstanceWithoutConstructor();
$dataset[$name] = ( new \ReflectionClass($field->getTypes()[0]) )->newInstanceWithoutConstructor();
}
else {
# empty collection
@@ -326,7 +328,9 @@ trait EntityTrait {
{
$default = ( $alias === false ? '' : Repository::DEFAULT_ALIAS ); # bw compatibility, to be deprecated
return new EntityField(static::class, $name, $alias ? Ulmus::repository(static::class)->adapter->adapter()->escapeIdentifier($alias, Adapter\AdapterInterface::IDENTIFIER_FIELD) : $default, Ulmus::resolveEntity(static::class));
$alias = $alias ? Ulmus::repository(static::class)->adapter->adapter()->escapeIdentifier($alias, Adapter\AdapterInterface::IDENTIFIER_FIELD) : $default;
return new EntityField(static::class, $name, $alias, Ulmus::resolveEntity(static::class));
}
#[Ignore]