- Some bugfixes were made within annotations. - Added a new Object Instanciator which allows to manipulate and define how an object from an entity must be instanciated with given value.
161 lines
4.7 KiB
PHP
161 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace Ulmus;
|
|
|
|
use Ulmus\Repository,
|
|
Ulmus\Common\EntityResolver,
|
|
Ulmus\Common\EntityField;
|
|
|
|
use Ulmus\Annotation\Classes\{ Method, Table, Collation as Test, };
|
|
use Ulmus\Annotation\Property\{ Field, Relation, OrderBy, Where, };
|
|
use Ulmus\Annotation\Property\Field\{ Id, ForeignKey, CreatedAt, UpdatedAt, };
|
|
|
|
trait EntityTrait {
|
|
|
|
/**
|
|
* @Ignore
|
|
*/
|
|
protected bool $strictEntityFieldsDeclaration = true;
|
|
|
|
/**
|
|
* @Ignore
|
|
*/
|
|
protected array $unmatchedEntityDatasetFields = [];
|
|
|
|
/**
|
|
* @Ignore
|
|
*/
|
|
public function __get(string $name)
|
|
{
|
|
$entityResolver= $this->resolveEntity();
|
|
|
|
# Resolve relations here if one is called
|
|
if ( null !== ( $relation = $entityResolver->searchFieldAnnotation($name, new Relation() ) ) ) {
|
|
$order = $entityResolver->searchFieldAnnotationList($name, new OrderBy() );
|
|
$where = $entityResolver->searchFieldAnnotationList($name, new Where() );
|
|
|
|
$baseEntity = $relation->entity();
|
|
$repository = $baseEntity->repository();
|
|
|
|
foreach($where as $condition) {
|
|
$repository->where($condition->field, $condition->value, $condition->operator);
|
|
}
|
|
|
|
foreach($order as $item) {
|
|
$repository->orderBy($item->field, $item->order);
|
|
}
|
|
|
|
$field = $relation->key;
|
|
|
|
if ( method_exists($this, $filterMethod = "filterRelation$name") ) {
|
|
$this->$filterMethod($repository);
|
|
}
|
|
|
|
switch($relation->type) {
|
|
case 'oneToMany':
|
|
$repository->where( $baseEntity->field($relation->foreignKey), 866); # <<<<<<<<< CHANGE $THIS->ID WITH PROPER NOMENCLATURE
|
|
|
|
return $this->$name = $repository->loadAll();
|
|
|
|
case 'oneToOne':
|
|
$repository->where( $baseEntity->field($relation->foreignKey), $this->$field );
|
|
|
|
$result = $repository->loadAll();
|
|
|
|
if ( count($result) === 0 ) {
|
|
return $baseEntity;
|
|
}
|
|
|
|
return $this->$name = $result[0];
|
|
}
|
|
|
|
return;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @Ignore
|
|
*/
|
|
public function entityFillFromDataset($dataset) : self
|
|
{
|
|
$fields = $this->resolveEntity();
|
|
|
|
foreach($dataset as $key => $value) {
|
|
$key = strtolower($key);
|
|
|
|
if ( null === $field = $fields->field($key, EntityResolver::KEY_COLUMN_NAME, false) ?? null ) {
|
|
if ($this->strictEntityFieldsDeclaration ) {
|
|
throw new \Exception("Field `$key` can not be found within your entity ".static::class);
|
|
}
|
|
else {
|
|
$this->unmatchedEntityDatasetFields[$key] = $value;
|
|
}
|
|
}
|
|
elseif ( is_null($value) ) {
|
|
$this->{$field['name']} = null;
|
|
}
|
|
elseif ( $field['type'] === 'array' ) {
|
|
$this->{$field['name']} = substr($value, 0, 1) === "a" ? unserialize($value) : json_decode($value, true);
|
|
}
|
|
elseif ( EntityField::isScalarType($field['type']) ) {
|
|
$this->{$field['name']} = $value;
|
|
}
|
|
elseif ( ! $field['builtin'] ) {
|
|
$this->{$field['name']} = Ulmus::instanciateObject($field['type'], [ $value ]);
|
|
}
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* @Ignore
|
|
*/
|
|
public function resolveEntity() : EntityResolver
|
|
{
|
|
return Ulmus::resolveEntity(static::class);
|
|
}
|
|
|
|
/**
|
|
* @Ignore
|
|
*/
|
|
public static function repository(string $alias = Repository::DEFAULT_ALIAS) : Repository
|
|
{
|
|
return Ulmus::repository(static::class, $alias, Ulmus::$defaultAdapter);
|
|
}
|
|
|
|
/**
|
|
* @Ignore
|
|
*/
|
|
public static function queryBuilder() : QueryBuilder
|
|
{
|
|
return Ulmus::queryBuilder(static::class);
|
|
}
|
|
|
|
/**
|
|
* @Ignore
|
|
*/
|
|
public static function field($name, ? string $alias = null)
|
|
{
|
|
return new EntityField(static::class, $name, $alias ?: Repository::DEFAULT_ALIAS);
|
|
}
|
|
|
|
/**
|
|
* @Ignore
|
|
*/
|
|
public static function fields(...$fields)
|
|
{
|
|
return implode(', ', array_map(function($name) {
|
|
return static::field($name);
|
|
}, $fields));
|
|
}
|
|
|
|
/**
|
|
* @Ignore
|
|
*/
|
|
public static function table()
|
|
{
|
|
return "REFLECT TABLE";
|
|
}
|
|
}
|