- 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; | ||||
| 
 | ||||
| #[\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 | ||||
|      * @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); | ||||
|          | ||||
|  | ||||
| @ -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 | ||||
|  | ||||
| @ -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; | ||||
| 
 | ||||
|  | ||||
| @ -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; | ||||
|     } | ||||
| } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user