refactor: use new interface design
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -1,50 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user