163 lines
4.1 KiB
PHP
163 lines
4.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
|
*/
|
|
|
|
namespace KTXM\PeopleProviderLocal\Providers\Personal;
|
|
|
|
use DateTimeImmutable;
|
|
use KTXF\People\Entity\IEntityBase;
|
|
use KTXF\People\Entity\IEntityMutable;
|
|
use KTXF\People\Entity\Individual\IndividualObject;
|
|
|
|
/**
|
|
* Entity wrapper - contains metadata and EntityData
|
|
*/
|
|
class Entity implements IEntityBase, IEntityMutable {
|
|
|
|
// Metadata fields (system-managed)
|
|
private ?string $entityId = null;
|
|
private ?string $tenantId = null;
|
|
private ?string $userId = null;
|
|
private ?string $collectionId = null;
|
|
private ?string $createdOn = null;
|
|
private ?string $modifiedOn = null;
|
|
private ?string $entitySignature = null;
|
|
|
|
// Entity display properties
|
|
private ?int $entityPriority = null;
|
|
private ?bool $entityVisibility = null;
|
|
private ?string $entityColor = null;
|
|
private string|array|null $entityData = null;
|
|
|
|
public function jsonSerialize(): mixed {
|
|
return [
|
|
self::JSON_PROPERTY_TYPE => self::JSON_TYPE,
|
|
self::JSON_PROPERTY_IN => $this->collectionId,
|
|
self::JSON_PROPERTY_ID => $this->entityId,
|
|
self::JSON_PROPERTY_DATA => $this->entityData,
|
|
self::JSON_PROPERTY_SIGNATURE => $this->entitySignature,
|
|
];
|
|
}
|
|
|
|
public function jsonDeserialize(array|string $data): static
|
|
{
|
|
if (is_string($data)) {
|
|
$data = json_decode($data, true);
|
|
}
|
|
|
|
$this->entityId = $data[self::JSON_PROPERTY_ID] ?? null;
|
|
$this->collectionId = $data[self::JSON_PROPERTY_IN] ?? null;
|
|
$this->entitySignature = $data[self::JSON_PROPERTY_SIGNATURE] ?? null;
|
|
$this->entityData = $data[self::JSON_PROPERTY_DATA] ?? null;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function fromStore(array|object $document): self {
|
|
// Convert object to array if needed
|
|
if (is_object($document)) {
|
|
$document = (array) $document;
|
|
}
|
|
|
|
// Load metadata
|
|
$this->entityId = $document['eid'] ?? null;
|
|
$this->tenantId = $document['tid'] ?? null;
|
|
$this->userId = $document['uid'] ?? null;
|
|
$this->collectionId = $document['cid'] ?? null;
|
|
$this->createdOn = $document['createdOn'] ?? null;
|
|
$this->modifiedOn = $document['modifiedOn'] ?? null;
|
|
$this->entityData = $document['data'] ?? null;
|
|
$this->entitySignature = md5(json_encode($this->entityData));
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function toStore(): array {
|
|
$document = [
|
|
'tid' => $this->tenantId,
|
|
'uid' => $this->userId,
|
|
'cid' => $this->collectionId,
|
|
'eid' => $this->entityId,
|
|
'createdOn' => $this->createdOn ?? date('c'),
|
|
'modifiedOn' => date('c'),
|
|
'data' => $this->entityData,
|
|
];
|
|
|
|
return $document;
|
|
}
|
|
|
|
public function in(): string|int {
|
|
return $this->collectionId ?? '';
|
|
}
|
|
|
|
public function id(): string|int {
|
|
return $this->entityId ?? '';
|
|
}
|
|
|
|
public function created(): ?DateTimeImmutable {
|
|
return $this->createdOn ? new DateTimeImmutable($this->createdOn) : null;
|
|
}
|
|
|
|
public function modified(): ?DateTimeImmutable {
|
|
return $this->modifiedOn ? new DateTimeImmutable($this->modifiedOn) : null;
|
|
}
|
|
|
|
public function signature(): ?string {
|
|
return $this->entitySignature;
|
|
}
|
|
|
|
public function getPriority(): ?int {
|
|
return $this->entityPriority;
|
|
}
|
|
|
|
public function setPriority(?int $value): static {
|
|
$this->entityPriority = $value;
|
|
return $this;
|
|
}
|
|
|
|
public function getVisibility(): ?bool {
|
|
return $this->entityVisibility;
|
|
}
|
|
|
|
public function setVisibility(?bool $value): static {
|
|
$this->entityVisibility = $value;
|
|
return $this;
|
|
}
|
|
|
|
public function getColor(): ?string {
|
|
return $this->entityColor;
|
|
}
|
|
|
|
public function setColor(?string $value): static {
|
|
$this->entityColor = $value;
|
|
return $this;
|
|
}
|
|
|
|
public function getDataObject(): IndividualObject|null {
|
|
return $this->entityData ? (new IndividualObject)->jsonDeserialize($this->entityData) : null;
|
|
}
|
|
|
|
public function setDataObject(IndividualObject $value): static
|
|
{
|
|
$this->entityData = $value->jsonSerialize();
|
|
return $this;
|
|
}
|
|
|
|
public function getDataJson(): array|string|null {
|
|
return $this->entityData;
|
|
}
|
|
|
|
public function setDataJson(array|string $value): static
|
|
{
|
|
$this->entityData = $value;
|
|
return $this;
|
|
}
|
|
|
|
}
|
|
|