* SPDX-License-Identifier: AGPL-3.0-or-later */ namespace KTXM\ProviderLocalPeople\Providers\Personal; use KTXF\Utile\Timestamp; use KTXF\People\Entity\EntityMutableAbstract; 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; $this->getProperties()->fromStore($document); $this->data[self::PROPERTY_SIGNATURE] = md5(json_encode($this->getProperties()->getDataRaw())); 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 = array_merge($document, $this->getProperties()->toStore()); return $document; } public function getProperties(): EntityProperties { if (!isset($this->properties)) { $this->properties = new EntityProperties([]); } return $this->properties; } }