- Added support for Ignore attribute in dataset export of EntityTrait
- Work done on JSONification of entity
This commit is contained in:
parent
7f8780d328
commit
3a80fee9c3
|
@ -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,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -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);
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -168,12 +168,28 @@ 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) {
|
||||||
|
$ignore = $entityResolver->searchFieldAnnotation($name, [ Attribute\Property\Relation\Ignore::class ] );
|
||||||
|
|
||||||
|
if ($ignore && $ignore->ignoreExport) {
|
||||||
|
if ( $relation->isOneToOne() ) {
|
||||||
|
# empty object
|
||||||
|
$dataset[$name] = ( new \ReflectionClass($field['type']) )->newInstanceWithoutConstructor();
|
||||||
|
}
|
||||||
|
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 ( null !== $value = $this->$name ?? null ) {
|
||||||
if ( is_iterable($value) ) {
|
if ( is_iterable($value) ) {
|
||||||
$list = [];
|
$list = [];
|
||||||
|
@ -191,6 +207,7 @@ trait EntityTrait {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return $dataset;
|
return $dataset;
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue