From b589ba3c2b7d08ee2cbd6e1876a0ba190fd3933c Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Tue, 10 Dec 2019 15:27:45 -0500 Subject: [PATCH] - WIP on Ulmus -- new use case showed up, modification were needed. --- src/Common/ArrayObjectTrait.php | 1 - src/Common/EntityResolver.php | 12 +++++++++--- src/EntityCollection.php | 6 +++--- src/EntityTrait.php | 18 ++++++++++++++---- src/Repository.php | 4 ++-- 5 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/Common/ArrayObjectTrait.php b/src/Common/ArrayObjectTrait.php index 172df34..5a3ff22 100644 --- a/src/Common/ArrayObjectTrait.php +++ b/src/Common/ArrayObjectTrait.php @@ -132,7 +132,6 @@ trait ArrayObjectTrait { return $this; } - public function offsetGet($offset) { if ( !is_null($this->arrayobject_pointer) ) { diff --git a/src/Common/EntityResolver.php b/src/Common/EntityResolver.php index 4b2a7a5..0f0281e 100644 --- a/src/Common/EntityResolver.php +++ b/src/Common/EntityResolver.php @@ -20,6 +20,8 @@ class EntityResolver { public array $properties; public array $methods; + + protected array $fieldList = []; public function __construct(string $entityClass) { @@ -28,18 +30,22 @@ class EntityResolver { list($this->uses, $this->class, $this->methods, $this->properties) = array_values( ObjectReflection::fromClass($entityClass)->read() ); - + $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{ return $this->fieldList($fieldKey)[$name]; } 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 diff --git a/src/EntityCollection.php b/src/EntityCollection.php index 5b6b6e6..27b1ade 100644 --- a/src/EntityCollection.php +++ b/src/EntityCollection.php @@ -2,7 +2,7 @@ namespace Ulmus; -class EntityCollection extends \ArrayObject -{ - use Common\ArrayObjectTrait; +#class EntityCollection implements \Countable, \ArrayAccess +class EntityCollection extends \ArrayObject { + # use Common\ArrayObjectTrait; } diff --git a/src/EntityTrait.php b/src/EntityTrait.php index 6965d26..5396cab 100644 --- a/src/EntityTrait.php +++ b/src/EntityTrait.php @@ -11,6 +11,10 @@ use Ulmus\Annotation\Property\{ Field, Relation, OrderBy, Where, }; use Ulmus\Annotation\Property\Field\{ Id, ForeignKey, CreatedAt, UpdatedAt, }; trait EntityTrait { + + protected bool $strictEntityFieldsDeclaration = true; + + protected array $unmatchedEntityDatasetFields = []; /** * @Ignore @@ -20,11 +24,17 @@ trait EntityTrait { $fields = Ulmus::resolveEntity(static::class); foreach($dataset as $key => $value) { - if ( null === $field = $fields->field($key, EntityResolver::KEY_COLUMN_NAME) ?? null ) { - throw new \Exception("Field `$key` can not be found within your entity ".static::class); + $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; + } } - - if ( is_null($value) ) { + elseif ( is_null($value) ) { $this->{$field['name']} = null; } elseif ( $field['type'] === 'array' ) { diff --git a/src/Repository.php b/src/Repository.php index 3802ab4..de8cc9d 100644 --- a/src/Repository.php +++ b/src/Repository.php @@ -36,9 +36,9 @@ class Repository 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