From 3a80fee9c3fdddc758fdf086b90afee5571a5c25 Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Fri, 17 Nov 2023 22:40:21 -0500 Subject: [PATCH] - Added support for Ignore attribute in dataset export of EntityTrait - Work done on JSONification of entity --- src/Attribute/Property/Relation/Ignore.php | 8 ++- src/ConnectionAdapter.php | 2 +- src/EntityCollection.php | 13 ++++- src/EntityTrait.php | 50 ++++++++++++------- .../SearchRequestPaginationTrait.php | 9 +++- 5 files changed, 60 insertions(+), 22 deletions(-) diff --git a/src/Attribute/Property/Relation/Ignore.php b/src/Attribute/Property/Relation/Ignore.php index a9a13ad..e44370a 100644 --- a/src/Attribute/Property/Relation/Ignore.php +++ b/src/Attribute/Property/Relation/Ignore.php @@ -3,4 +3,10 @@ namespace Ulmus\Attribute\Property\Relation; #[\Attribute] -class Ignore {} +class Ignore { + + public function __construct( + public bool $ignoreExport = false, + ) {} + +} diff --git a/src/ConnectionAdapter.php b/src/ConnectionAdapter.php index dd29d44..c5f6180 100644 --- a/src/ConnectionAdapter.php +++ b/src/ConnectionAdapter.php @@ -77,7 +77,7 @@ class ConnectionAdapter * @param string $name An Ulmus adapter or full class name implementing AdapterInterface * @return AdapterInterface */ - protected function instanciateAdapter($name) : AdapterInterface + protected function instanciateAdapter(string $name) : AdapterInterface { $class = substr($name, 0, 2) === "\\" ? $name : sprintf("\\%s\\Adapter\\%s", __NAMESPACE__, $name); diff --git a/src/EntityCollection.php b/src/EntityCollection.php index 30b1858..020744a 100644 --- a/src/EntityCollection.php +++ b/src/EntityCollection.php @@ -326,6 +326,17 @@ class EntityCollection extends \ArrayObject implements \JsonSerializable { return $list; } + public function toJsonArray(bool $includeRelations = false) : array { + $list = []; + + foreach($this as $entity) { + $list[] = $entity instanceof \JsonSerializable ? $entity->jsonSerialize() : $entity->toArray($includeRelations); + } + + return $list; + } + + public function fromArray(array $datasets, ? string /*stringable*/ $entityClass = null) : self { foreach($datasets as $dataset) { @@ -360,7 +371,7 @@ class EntityCollection extends \ArrayObject implements \JsonSerializable { public function jsonSerialize(): mixed { - return $this->toArray(true); + return $this->toJsonArray(true); } public function append($value) : void diff --git a/src/EntityTrait.php b/src/EntityTrait.php index 0713a30..9fde948 100644 --- a/src/EntityTrait.php +++ b/src/EntityTrait.php @@ -168,24 +168,41 @@ trait EntityTrait { } } - # @TODO Must fix recursive bug ! if ($includeRelations) { foreach($entityResolver->properties as $name => $field){ - $relation = $entityResolver->searchFieldAnnotation($name, [ Attribute\Property\Relation::class, Relation::class ] ); + $relation = $entityResolver->searchFieldAnnotation($name, [ Attribute\Property\Relation::class ] ); - if ( $relation && isset($this->$name) && ($relation->entity ?? $relation->bridge) !== static::class ) { - if ( null !== $value = $this->$name ?? null ) { - if ( is_iterable($value) ) { - $list = []; + if ($relation) { + $ignore = $entityResolver->searchFieldAnnotation($name, [ Attribute\Property\Relation\Ignore::class ] ); - foreach($value as $entity) { - $list[] = $entity->entityGetDataset(false); - } - - $dataset[$name] = $list; + if ($ignore && $ignore->ignoreExport) { + if ( $relation->isOneToOne() ) { + # empty object + $dataset[$name] = ( new \ReflectionClass($field['type']) )->newInstanceWithoutConstructor(); } - elseif ( is_object($value) ) { - $dataset[$name] = $value->entityGetDataset(false); + else { + # empty collection + $dataset[$name] = []; + } + + continue; + } + + # @TODO Must fix recursive bug.. this last check is way too basic to work + if ( isset($this->$name) && ($relation->entity ?? $relation->bridge) !== static::class ) { + if ( null !== $value = $this->$name ?? null ) { + if ( is_iterable($value) ) { + $list = []; + + foreach($value as $entity) { + $list[] = $entity->entityGetDataset(false); + } + + $dataset[$name] = $list; + } + elseif ( is_object($value) ) { + $dataset[$name] = $value->entityGetDataset(false); + } } } } @@ -240,10 +257,7 @@ trait EntityTrait { #[Ignore] public function __isset(string $name) : bool { - #if ( null !== $relation = static::resolveEntity()->searchFieldAnnotation($name, new Relation() ) ) { - # return isset($this->{$relation->key}); - #} - $rel = static::resolveEntity()->searchFieldAnnotation($name, [ Attribute\Property\Relation::class, Relation::class ]); + $rel = static::resolveEntity()->searchFieldAnnotation($name, [ Attribute\Property\Relation::class ]); if ( $this->isLoaded() && $rel ) { return true; @@ -327,7 +341,7 @@ trait EntityTrait { #[Ignore] public static function searchRequest(...$arguments) : SearchRequestInterface { - return new class() implements SearchRequestInterface + return new class() implements SearchRequestInterface, \JsonSerializable { use SearchRequestPaginationTrait; diff --git a/src/SearchRequest/SearchRequestPaginationTrait.php b/src/SearchRequest/SearchRequestPaginationTrait.php index 356703d..e9bb170 100644 --- a/src/SearchRequest/SearchRequestPaginationTrait.php +++ b/src/SearchRequest/SearchRequestPaginationTrait.php @@ -44,7 +44,7 @@ trait SearchRequestPaginationTrait { public function pageCount() : int { - return ceil($this->count() / $this->limit()); + return $this->pageCount = ceil($this->count() / $this->limit()); } public function hasPagination() : int @@ -65,4 +65,11 @@ trait SearchRequestPaginationTrait { return $total <= $this->count() ? $total : $this->count(); } + + public function jsonSerialize(): mixed + { + $this->pageCount(); + + return $this; + } }