* SPDX-License-Identifier: AGPL-3.0-or-later */ 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 { private ?string $tenantId = null; private ?string $userId = null; public function __construct( string $provider = 'default', string|int $service = 'personal', ) { parent::__construct($provider, $service); } public function fromStore(array|object $document): self { if (is_object($document)) { $document = (array) $document; } $this->data[self::PROPERTY_IDENTIFIER] = $document['eid'] ?? null; $this->tenantId = $document['tid'] ?? null; $this->userId = $document['uid'] ?? null; $this->data[self::PROPERTY_COLLECTION] = $document['cid'] ?? null; $this->data[self::PROPERTY_CREATED] = $document['createdOn'] ?? null; $this->data[self::PROPERTY_MODIFIED] = $document['modifiedOn'] ?? null; $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; } public function toStore(): array { $document = array_filter([ 'tid' => $this->tenantId, 'uid' => $this->userId, 'cid' => $this->collection(), 'eid' => $this->identifier(), 'createdOn' => Timestamp::normalize($this->data[self::PROPERTY_CREATED] ?? null), 'modifiedOn' => Timestamp::normalize($this->data[self::PROPERTY_MODIFIED] ?? null), ], static fn($value) => $value !== null); $document['data'] = json_decode(json_encode($this->getProperties()), true); return $document; } public function getProperties(): EventObject|TaskObject|JournalObject { return $this->properties ??= new EventObject(); } }