- WIP on Ulmus -- new use case showed up, modification were needed.
This commit is contained in:
parent
1b051f923e
commit
b589ba3c2b
|
@ -132,7 +132,6 @@ trait ArrayObjectTrait {
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function offsetGet($offset)
|
public function offsetGet($offset)
|
||||||
{
|
{
|
||||||
if ( !is_null($this->arrayobject_pointer) ) {
|
if ( !is_null($this->arrayobject_pointer) ) {
|
||||||
|
|
|
@ -20,6 +20,8 @@ class EntityResolver {
|
||||||
public array $properties;
|
public array $properties;
|
||||||
|
|
||||||
public array $methods;
|
public array $methods;
|
||||||
|
|
||||||
|
protected array $fieldList = [];
|
||||||
|
|
||||||
public function __construct(string $entityClass)
|
public function __construct(string $entityClass)
|
||||||
{
|
{
|
||||||
|
@ -28,18 +30,22 @@ class EntityResolver {
|
||||||
list($this->uses, $this->class, $this->methods, $this->properties) = array_values(
|
list($this->uses, $this->class, $this->methods, $this->properties) = array_values(
|
||||||
ObjectReflection::fromClass($entityClass)->read()
|
ObjectReflection::fromClass($entityClass)->read()
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->resolveAnnotations();
|
$this->resolveAnnotations();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function field($name, $fieldKey = self::KEY_ENTITY_NAME) : ? array
|
public function field($name, $fieldKey = self::KEY_ENTITY_NAME, $throwException = true) : ? array
|
||||||
{
|
{
|
||||||
try{
|
try{
|
||||||
return $this->fieldList($fieldKey)[$name];
|
return $this->fieldList($fieldKey)[$name];
|
||||||
}
|
}
|
||||||
catch(\Throwable $e) {
|
catch(\Throwable $e) {
|
||||||
throw new \InvalidArgumentException("Can't find entity field's column named `$name` from entity {$this->entityClass}");
|
if ( $throwException) {
|
||||||
|
throw new \InvalidArgumentException("Can't find entity field's column named `$name` from entity {$this->entityClass}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function fieldList($fieldKey = self::KEY_ENTITY_NAME) : array
|
public function fieldList($fieldKey = self::KEY_ENTITY_NAME) : array
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
namespace Ulmus;
|
namespace Ulmus;
|
||||||
|
|
||||||
class EntityCollection extends \ArrayObject
|
#class EntityCollection implements \Countable, \ArrayAccess
|
||||||
{
|
class EntityCollection extends \ArrayObject {
|
||||||
use Common\ArrayObjectTrait;
|
# use Common\ArrayObjectTrait;
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,10 @@ use Ulmus\Annotation\Property\{ Field, Relation, OrderBy, Where, };
|
||||||
use Ulmus\Annotation\Property\Field\{ Id, ForeignKey, CreatedAt, UpdatedAt, };
|
use Ulmus\Annotation\Property\Field\{ Id, ForeignKey, CreatedAt, UpdatedAt, };
|
||||||
|
|
||||||
trait EntityTrait {
|
trait EntityTrait {
|
||||||
|
|
||||||
|
protected bool $strictEntityFieldsDeclaration = true;
|
||||||
|
|
||||||
|
protected array $unmatchedEntityDatasetFields = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Ignore
|
* @Ignore
|
||||||
|
@ -20,11 +24,17 @@ trait EntityTrait {
|
||||||
$fields = Ulmus::resolveEntity(static::class);
|
$fields = Ulmus::resolveEntity(static::class);
|
||||||
|
|
||||||
foreach($dataset as $key => $value) {
|
foreach($dataset as $key => $value) {
|
||||||
if ( null === $field = $fields->field($key, EntityResolver::KEY_COLUMN_NAME) ?? null ) {
|
$key = strtolower($key);
|
||||||
throw new \Exception("Field `$key` can not be found within your entity ".static::class);
|
|
||||||
|
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) ) {
|
||||||
if ( is_null($value) ) {
|
|
||||||
$this->{$field['name']} = null;
|
$this->{$field['name']} = null;
|
||||||
}
|
}
|
||||||
elseif ( $field['type'] === 'array' ) {
|
elseif ( $field['type'] === 'array' ) {
|
||||||
|
|
|
@ -36,9 +36,9 @@ class Repository
|
||||||
return $this->collectionFromQuery();
|
return $this->collectionFromQuery();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function loadFromPk($value) : EntityCollection
|
public function loadFromPk($value, $primaryKey = "id") : EntityCollection
|
||||||
{
|
{
|
||||||
return $this->where('id', $value)->loadOne();
|
return $this->where($primaryKey, $value)->loadOne();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function loadFromField($field, $value) : EntityCollection
|
public function loadFromField($field, $value) : EntityCollection
|
||||||
|
|
Loading…
Reference in New Issue