refactor: standardize design
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
93
lib/Providers/Personal/EntityResource.php
Normal file
93
lib/Providers/Personal/EntityResource.php
Normal file
@@ -0,0 +1,93 @@
|
||||
<?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\EntityMutableAbstract;
|
||||
use KTXF\Chrono\Event\EventObject;
|
||||
|
||||
class EntityResource extends EntityMutableAbstract {
|
||||
|
||||
private ?string $tenantId = null;
|
||||
private ?string $userId = null;
|
||||
private ?EventObject $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(): EventObject|null {
|
||||
return $this->entityDataObject;
|
||||
}
|
||||
|
||||
public function setDataObject(EventObject $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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user