- WIP on Ulmus -- new use case showed up, modification were needed.

This commit is contained in:
Dave Mc Nicoll 2019-12-10 15:27:45 -05:00
parent 1b051f923e
commit b589ba3c2b
5 changed files with 28 additions and 13 deletions

View File

@ -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) ) {

View File

@ -21,6 +21,8 @@ class EntityResolver {
public array $methods; public array $methods;
protected array $fieldList = [];
public function __construct(string $entityClass) public function __construct(string $entityClass)
{ {
$this->entityClass = $entityClass; $this->entityClass = $entityClass;
@ -32,14 +34,18 @@ class EntityResolver {
$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

View File

@ -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;
} }

View File

@ -12,6 +12,10 @@ 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 ( is_null($value) ) { 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; $this->{$field['name']} = null;
} }
elseif ( $field['type'] === 'array' ) { elseif ( $field['type'] === 'array' ) {

View File

@ -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