diff --git a/lib/Providers/Personal/EntityProperties.php b/lib/Providers/Personal/EntityProperties.php deleted file mode 100644 index 1b7b602..0000000 --- a/lib/Providers/Personal/EntityProperties.php +++ /dev/null @@ -1,50 +0,0 @@ - - * SPDX-License-Identifier: AGPL-3.0-or-later - */ - -namespace KTXM\ProviderLocalChrono\Providers\Personal; - -use KTXF\Chrono\Entity\EntityPropertiesMutableAbstract; - -class EntityProperties extends EntityPropertiesMutableAbstract { - - public function jsonSerialize(): array { - $data = $this->getDataRaw(); - - if (is_array($data)) { - return $data; - } - - return []; - } - - public function jsonDeserialize(array|string $data): static { - if (is_string($data)) { - $data = json_decode($data, true); - } - - if (is_array($data) && array_key_exists('data', $data)) { - $this->setDataRaw($data['data']); - return $this; - } - - $this->setDataRaw($data); - return $this; - } - - public function fromStore(array $data): static { - $this->setDataRaw($data['data'] ?? null); - return $this; - } - - public function toStore(): array { - return array_filter([ - 'data' => $this->getDataRaw(), - ], static fn($value) => $value !== null); - } -} diff --git a/lib/Providers/Personal/EntityResource.php b/lib/Providers/Personal/EntityResource.php index 8430424..d759604 100644 --- a/lib/Providers/Personal/EntityResource.php +++ b/lib/Providers/Personal/EntityResource.php @@ -10,6 +10,10 @@ declare(strict_types=1); namespace KTXM\ProviderLocalChrono\Providers\Personal; use KTXF\Chrono\Entity\EntityMutableAbstract; +use KTXF\Chrono\Entity\EntityType; +use KTXF\Chrono\Entity\EventObject; +use KTXF\Chrono\Entity\JournalObject; +use KTXF\Chrono\Entity\TaskObject; use KTXF\Utile\Timestamp; class EntityResource extends EntityMutableAbstract { @@ -35,8 +39,12 @@ class EntityResource extends EntityMutableAbstract { $this->data[self::PROPERTY_COLLECTION] = $document['cid'] ?? null; $this->data[self::PROPERTY_CREATED] = $document['createdOn'] ?? null; $this->data[self::PROPERTY_MODIFIED] = $document['modifiedOn'] ?? null; - $this->getProperties()->fromStore($document); - $this->data[self::PROPERTY_SIGNATURE] = md5(json_encode($this->getProperties()->getDataRaw())); + $data = $document['data'] ?? []; + if (is_string($data)) { + $data = json_decode($data, true) ?: []; + } + $this->properties = EntityType::fromData($data)->instantiate()->jsonDeserialize($data); + $this->data[self::PROPERTY_SIGNATURE] = md5(json_encode($this->properties)); return $this; } @@ -51,17 +59,13 @@ class EntityResource extends EntityMutableAbstract { 'modifiedOn' => Timestamp::normalize($this->data[self::PROPERTY_MODIFIED] ?? null), ], static fn($value) => $value !== null); - $document = array_merge($document, $this->getProperties()->toStore()); + $document['data'] = json_decode(json_encode($this->getProperties()), true); return $document; } - public function getProperties(): EntityProperties { - if (!isset($this->properties)) { - $this->properties = new EntityProperties([]); - } - - return $this->properties; + public function getProperties(): EventObject|TaskObject|JournalObject { + return $this->properties ??= new EventObject(); } } diff --git a/lib/Providers/Personal/PersonalService.php b/lib/Providers/Personal/PersonalService.php index 821e9a4..b3e81e6 100644 --- a/lib/Providers/Personal/PersonalService.php +++ b/lib/Providers/Personal/PersonalService.php @@ -14,6 +14,7 @@ use KTXF\Chrono\Collection\CollectionBaseInterface; use KTXF\Chrono\Collection\CollectionPropertiesBaseInterface; use KTXF\Chrono\Entity\EntityBaseInterface; use KTXF\Chrono\Entity\EntityPropertiesMutableInterface; +use KTXF\Chrono\Entity\EntityType; use KTXF\Chrono\Service\ServiceBaseInterface; use KTXF\Chrono\Service\ServiceCollectionMutableInterface; use KTXF\Chrono\Service\ServiceEntityMutableInterface; @@ -406,8 +407,10 @@ class PersonalService implements ServiceBaseInterface, ServiceCollectionMutableI } } - public function entityFresh(): EntityResource { - return new EntityResource(); + public function entityFresh(EntityType $type = EntityType::Event): EntityResource { + $entity = new EntityResource(); + $entity->setProperties($type->instantiate()); + return $entity; } public function entityCreate(CollectionIdentifierInterface $target, EntityPropertiesMutableInterface $properties, array $options = []): EntityBaseInterface { @@ -416,15 +419,9 @@ class PersonalService implements ServiceBaseInterface, ServiceCollectionMutableI if ($this->collectionAccessible($collection) === false) { throw new InvalidParameterException("Invalid: Collection identifier '$collection' does not exist or does not belong to user '{$this->serviceUserId}'"); } - // convert entity to a native type if needed - if (!($properties instanceof EntityResource)) { - $nativeEntity = $this->entityFresh(); - $nativeEntity->jsonDeserialize([ - EntityResource::PROPERTY_PROPERTIES => $properties->jsonSerialize(), - ]); - } else { - $nativeEntity = clone $properties; - } + // wrap the typed entity object in a native resource + $nativeEntity = $this->entityFresh(); + $nativeEntity->setProperties($properties); // create entity in store (store will handle userId and collection) $result = $this->store->entityCreate($this->serviceTenantId, $this->serviceUserId, $collection, $nativeEntity); return $result; @@ -442,15 +439,9 @@ class PersonalService implements ServiceBaseInterface, ServiceCollectionMutableI if (!isset($extant[$identifier]) || $extant[$identifier] === false) { throw new InvalidParameterException("Invalid: Entity identifier '$identifier' does not exist or does not belong to collection '$collection' or user '{$this->serviceUserId}'"); } - // convert entity to a native type if needed - if (!($properties instanceof EntityResource)) { - $nativeEntity = $this->entityFresh(); - $nativeEntity->jsonDeserialize([ - EntityResource::PROPERTY_PROPERTIES => $properties->jsonSerialize(), - ]); - } else { - $nativeEntity = clone $properties; - } + // wrap the typed entity object in a native resource + $nativeEntity = $this->entityFresh(); + $nativeEntity->setProperties($properties); // modify entity in store $result = $this->store->entityModify($this->serviceTenantId, $this->serviceUserId, $collection, $identifier, $nativeEntity); return $result;