2019-08-21 20:13:00 +00:00
|
|
|
<?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 {
|
2019-12-10 20:27:45 +00:00
|
|
|
|
|
|
|
protected bool $strictEntityFieldsDeclaration = true;
|
|
|
|
|
|
|
|
protected array $unmatchedEntityDatasetFields = [];
|
2019-08-21 20:13:00 +00:00
|
|
|
|
2019-12-10 14:23:41 +00:00
|
|
|
/**
|
|
|
|
* @Ignore
|
|
|
|
*/
|
2019-08-21 20:13:00 +00:00
|
|
|
public function entityFillFromDataset($dataset) : self
|
|
|
|
{
|
|
|
|
$fields = Ulmus::resolveEntity(static::class);
|
|
|
|
|
|
|
|
foreach($dataset as $key => $value) {
|
2019-12-10 20:27:45 +00:00
|
|
|
$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;
|
|
|
|
}
|
2019-08-21 20:13:00 +00:00
|
|
|
}
|
2019-12-10 20:27:45 +00:00
|
|
|
elseif ( is_null($value) ) {
|
2019-08-21 20:13:00 +00:00
|
|
|
$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 ( EntityField::isObjectType($field['type']) ) {
|
|
|
|
$this->{$field['name']} = new $field['type']();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Ignore
|
|
|
|
*/
|
|
|
|
public static function repository() : Repository
|
|
|
|
{
|
|
|
|
return Ulmus::repository(static::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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";
|
|
|
|
}
|
|
|
|
}
|