- Added support for Ignore attribute in dataset export of EntityTrait

- Work done on JSONification of entity
This commit is contained in:
Dave M. 2023-11-17 22:40:21 -05:00
parent 7f8780d328
commit 3a80fee9c3
5 changed files with 60 additions and 22 deletions

View File

@ -3,4 +3,10 @@
namespace Ulmus\Attribute\Property\Relation; namespace Ulmus\Attribute\Property\Relation;
#[\Attribute] #[\Attribute]
class Ignore {} class Ignore {
public function __construct(
public bool $ignoreExport = false,
) {}
}

View File

@ -77,7 +77,7 @@ class ConnectionAdapter
* @param string $name An Ulmus adapter or full class name implementing AdapterInterface * @param string $name An Ulmus adapter or full class name implementing AdapterInterface
* @return 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); $class = substr($name, 0, 2) === "\\" ? $name : sprintf("\\%s\\Adapter\\%s", __NAMESPACE__, $name);

View File

@ -326,6 +326,17 @@ class EntityCollection extends \ArrayObject implements \JsonSerializable {
return $list; 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 public function fromArray(array $datasets, ? string /*stringable*/ $entityClass = null) : self
{ {
foreach($datasets as $dataset) { foreach($datasets as $dataset) {
@ -360,7 +371,7 @@ class EntityCollection extends \ArrayObject implements \JsonSerializable {
public function jsonSerialize(): mixed public function jsonSerialize(): mixed
{ {
return $this->toArray(true); return $this->toJsonArray(true);
} }
public function append($value) : void public function append($value) : void

View File

@ -168,24 +168,41 @@ trait EntityTrait {
} }
} }
# @TODO Must fix recursive bug !
if ($includeRelations) { if ($includeRelations) {
foreach($entityResolver->properties as $name => $field){ 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 ($relation) {
if ( null !== $value = $this->$name ?? null ) { $ignore = $entityResolver->searchFieldAnnotation($name, [ Attribute\Property\Relation\Ignore::class ] );
if ( is_iterable($value) ) {
$list = [];
foreach($value as $entity) { if ($ignore && $ignore->ignoreExport) {
$list[] = $entity->entityGetDataset(false); if ( $relation->isOneToOne() ) {
} # empty object
$dataset[$name] = ( new \ReflectionClass($field['type']) )->newInstanceWithoutConstructor();
$dataset[$name] = $list;
} }
elseif ( is_object($value) ) { else {
$dataset[$name] = $value->entityGetDataset(false); # 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] #[Ignore]
public function __isset(string $name) : bool public function __isset(string $name) : bool
{ {
#if ( null !== $relation = static::resolveEntity()->searchFieldAnnotation($name, new Relation() ) ) { $rel = static::resolveEntity()->searchFieldAnnotation($name, [ Attribute\Property\Relation::class ]);
# return isset($this->{$relation->key});
#}
$rel = static::resolveEntity()->searchFieldAnnotation($name, [ Attribute\Property\Relation::class, Relation::class ]);
if ( $this->isLoaded() && $rel ) { if ( $this->isLoaded() && $rel ) {
return true; return true;
@ -327,7 +341,7 @@ trait EntityTrait {
#[Ignore] #[Ignore]
public static function searchRequest(...$arguments) : SearchRequestInterface public static function searchRequest(...$arguments) : SearchRequestInterface
{ {
return new class() implements SearchRequestInterface return new class() implements SearchRequestInterface, \JsonSerializable
{ {
use SearchRequestPaginationTrait; use SearchRequestPaginationTrait;

View File

@ -44,7 +44,7 @@ trait SearchRequestPaginationTrait {
public function pageCount() : int public function pageCount() : int
{ {
return ceil($this->count() / $this->limit()); return $this->pageCount = ceil($this->count() / $this->limit());
} }
public function hasPagination() : int public function hasPagination() : int
@ -65,4 +65,11 @@ trait SearchRequestPaginationTrait {
return $total <= $this->count() ? $total : $this->count(); return $total <= $this->count() ? $total : $this->count();
} }
public function jsonSerialize(): mixed
{
$this->pageCount();
return $this;
}
} }