- Added ArrayOf attributes and DatasetFill/DatasetPull events. ArrayCollection is also working with ArrayOf attributes
This commit is contained in:
1 parent
fd5d31fa17
commit
009e30eb7e
21 files changed
+322
-43
No files matched your search
+35
-15
@@ -3,7 +3,9 @@
|
||||
namespace Ulmus;
|
||||
|
||||
use Notes\Attribute\Ignore;
|
||||
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
|
||||
use Ulmus\{Attribute\Obj\JsonSerialize,
|
||||
Attribute\Property\Join,
|
||||
Attribute\Property\Relation,
|
||||
@@ -14,11 +16,8 @@ use Ulmus\{Attribute\Obj\JsonSerialize,
|
||||
Entity\DatasetHandler,
|
||||
Entity\EntityInterface,
|
||||
QueryBuilder\QueryBuilderInterface};
|
||||
use Ulmus\SearchRequest\{Attribute\SearchParameter,
|
||||
SearchMethodEnum,
|
||||
SearchRequestInterface,
|
||||
SearchRequestFromRequestTrait,
|
||||
SearchRequestPaginationTrait};
|
||||
|
||||
use Ulmus\{Attribute\RegisterEntityEventInterface, Entity\EntityValueModifier, SearchRequest, Event};
|
||||
|
||||
#[JsonSerialize(includeRelations: true)]
|
||||
trait EntityTrait {
|
||||
@@ -48,6 +47,9 @@ trait EntityTrait {
|
||||
#[Ignore]
|
||||
public function initializeEntity(iterable|null $dataset = null) : void
|
||||
{
|
||||
# Attributs can attach themselves on events thrown by this trait
|
||||
$this->entityRegisterAttributes();
|
||||
|
||||
$this->datasetHandler = new DatasetHandler(static::resolveEntity(), $this->entityStrictFieldsDeclaration);
|
||||
|
||||
if ($dataset) {
|
||||
@@ -57,18 +59,39 @@ trait EntityTrait {
|
||||
$this->resetVirtualProperties();
|
||||
}
|
||||
|
||||
#[Ignore]
|
||||
public function entityRegisterAttributes() : void
|
||||
{
|
||||
foreach($this->resolveEntity()->reflectedClass->getProperties(true) as $field => $property) {
|
||||
foreach($property->attributes as $tag) {
|
||||
if ( $tag->object instanceof RegisterEntityEventInterface ) {
|
||||
$tag->object->attach($this, $field);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[Ignore]
|
||||
public function entityFillFromDataset(iterable $dataset, bool $overwriteDataset = false) : self
|
||||
{
|
||||
$properties = $this->resolveEntity()->reflectedClass->getProperties(true);
|
||||
$loaded = $this->isLoaded();
|
||||
|
||||
$handler = $this->datasetHandler->push($dataset);
|
||||
$generator = $this->datasetHandler->push($dataset);
|
||||
|
||||
foreach($generator as $field => $value) {
|
||||
foreach($properties[$field]->attributes as $tag) {
|
||||
if ( $tag->object instanceof EntityValueModifier ) {
|
||||
$value = $tag->object->push($value, $properties[$field]);
|
||||
}
|
||||
}
|
||||
|
||||
foreach($handler as $field => $value) {
|
||||
$this->$field = $value;
|
||||
}
|
||||
|
||||
$this->entityDatasetUnmatchedFields = $handler->getReturn();
|
||||
$this->eventExecute(Event\Entity\DatasetFill::class, $this);
|
||||
|
||||
$this->entityDatasetUnmatchedFields = $generator->getReturn();
|
||||
|
||||
# Keeping original data to diff on UPDATE query
|
||||
if ( ! $loaded ) {
|
||||
@@ -77,7 +100,7 @@ trait EntityTrait {
|
||||
elseif ($overwriteDataset) {
|
||||
$this->entityLoadedDataset = array_change_key_case(is_array($dataset) ? $dataset : iterator_to_array($dataset), \CASE_LOWER) + $this->entityLoadedDataset;
|
||||
}
|
||||
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -91,7 +114,6 @@ trait EntityTrait {
|
||||
return iterator_to_array($this->entityYieldDataset($includeRelations, $rewriteValue));
|
||||
}
|
||||
|
||||
|
||||
#[Ignore]
|
||||
protected function entityYieldDataset(bool $includeRelations = false, bool $rewriteValue = true) : \Generator
|
||||
{
|
||||
@@ -106,13 +128,11 @@ trait EntityTrait {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[Ignore]
|
||||
public function resetVirtualProperties() : self
|
||||
{
|
||||
foreach($this->resolveEntity()->reflectedClass->getProperties(true) as $field => $property) {
|
||||
foreach($property->attributes as $tag) {
|
||||
|
||||
if ( $tag->object instanceof ResettablePropertyInterface ) {
|
||||
unset($this->$field);
|
||||
}
|
||||
@@ -133,7 +153,7 @@ trait EntityTrait {
|
||||
}
|
||||
|
||||
#[Ignore]
|
||||
public function toArray($includeRelations = false, array $filterFields = null, bool $rewriteValue = true) : array
|
||||
public function toArray($includeRelations = false, array $filterFields = [], bool $rewriteValue = true) : array
|
||||
{
|
||||
$dataset = $this->entityGetDataset($includeRelations, false, $rewriteValue);
|
||||
|
||||
@@ -193,7 +213,7 @@ trait EntityTrait {
|
||||
{
|
||||
$rel = static::resolveEntity()->searchFieldAnnotation($name, [ Attribute\Property\Relation::class ]);
|
||||
|
||||
if ( $this->isLoaded() && $rel ) {
|
||||
if ( $rel && $this->isLoaded() ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -250,7 +270,7 @@ trait EntityTrait {
|
||||
}
|
||||
}
|
||||
|
||||
if (is_object($value) && ($value instanceof JsonSerializable) ) {
|
||||
if (is_object($value) && ($value instanceof \JsonSerializable) ) {
|
||||
$dataset[$key] = $value->jsonSerialize();
|
||||
}
|
||||
else {
|
||||
|
||||
Reference in new issue
Block a user