Files
provider_local_people/lib/Providers/Personal/EntityResource.php
2026-02-25 15:34:19 -05:00

94 lines
2.5 KiB
PHP

<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace KTXM\ProviderLocalPeople\Providers\Personal;
use KTXF\People\Entity\EntityMutableAbstract;
use KTXF\People\Entity\Individual\IndividualObject;
class EntityResource extends EntityMutableAbstract {
private ?string $tenantId = null;
private ?string $userId = null;
private ?IndividualObject $entityDataObject = 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::JSON_PROPERTY_IDENTIFIER] = $document['eid'] ?? null;
$this->tenantId = $document['tid'] ?? null;
$this->userId = $document['uid'] ?? null;
$this->data[self::JSON_PROPERTY_COLLECTION] = $document['cid'] ?? null;
$this->data[self::JSON_PROPERTY_CREATED] = $document['createdOn'] ?? null;
$this->data[self::JSON_PROPERTY_MODIFIED] = $document['modifiedOn'] ?? null;
$this->getProperties()->fromStore($document);
$this->data[self::JSON_PROPERTY_SIGNATURE] = md5(json_encode($this->getDataJson()));
$this->entityDataObject = null;
return $this;
}
public function toStore(): array {
$document = array_filter([
'tid' => $this->tenantId,
'uid' => $this->userId,
'cid' => $this->collection(),
'eid' => $this->identifier(),
'createdOn' => $this->data[self::JSON_PROPERTY_CREATED] ?? date('c'),
'modifiedOn' => date('c'),
], 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;
}
public function getDataObject(): IndividualObject|null {
return $this->entityDataObject;
}
public function setDataObject(IndividualObject $value): static
{
$this->entityDataObject = $value;
$this->setDataJson($value->jsonSerialize());
return $this;
}
public function getDataJson(): array|string|null {
return $this->getProperties()->getDataRaw();
}
public function setDataJson(array|string|null $value): static
{
$this->entityDataObject = null;
$this->getProperties()->setDataRaw($value);
$this->data[self::JSON_PROPERTY_SIGNATURE] = md5(json_encode($value));
return $this;
}
}