Merge pull request 'chore: standardize chrono provider' (#3) from chore/standardize-chrono-provider into main

Reviewed-on: #3
This commit was merged in pull request #3.
This commit is contained in:
2026-02-17 08:14:39 +00:00
7 changed files with 451 additions and 529 deletions

View File

@@ -9,184 +9,78 @@ declare(strict_types=1);
namespace KTXM\ProviderLocalChrono\Providers\Personal; namespace KTXM\ProviderLocalChrono\Providers\Personal;
use KTXF\Chrono\Collection\CollectionContent; use KTXF\Chrono\Collection\CollectionMutableAbstract;
use KTXF\Chrono\Collection\CollectionPermissions;
use KTXF\Chrono\Collection\CollectionRoles;
use KTXF\Chrono\Collection\ICollectionMutable;
class Collection implements ICollectionMutable { class Collection extends CollectionMutableAbstract {
private ?string $userId = null; private ?string $userId = null;
private string $providerId = 'default';
private string $serviceId = 'personal';
private ?string $collectionId = null;
private ?string $collectionUuid = null; private ?string $collectionUuid = null;
private ?string $collectionLabel = null;
private ?string $collectionDescription = null;
private ?int $collectionPriority = null;
private ?bool $collectionVisibility = null;
private ?string $collectionColor = null;
private ?string $collectionCreatedOn = null;
private ?string $collectionModifiedOn = null;
private bool $collectionEnabled = true; private bool $collectionEnabled = true;
private ?string $collectionSignature = null;
private array $collectionPermissions = [
CollectionPermissions::View->value => true,
CollectionPermissions::Create->value => true,
CollectionPermissions::Modify->value => true,
CollectionPermissions::Destroy->value => true,
CollectionPermissions::Share->value => true,
];
private array $collectionAttributes = [
'roles' => [
CollectionRoles::Individual->value => true,
],
'contents' => [
CollectionContent::Event->value => true,
CollectionContent::Task->value => true,
CollectionContent::Journal->value => true,
],
];
public function jsonSerialize(): mixed { public function __construct(
return [ string $provider = 'default',
self::JSON_PROPERTY_TYPE => self::JSON_TYPE, string|int $service = 'personal',
self::JSON_PROPERTY_PROVIDER => $this->providerId, ) {
self::JSON_PROPERTY_SERVICE => $this->serviceId, parent::__construct($provider, $service);
self::JSON_PROPERTY_IN => null,
self::JSON_PROPERTY_ID => $this->collectionId,
self::JSON_PROPERTY_LABEL => $this->collectionLabel,
self::JSON_PROPERTY_DESCRIPTION => $this->collectionDescription,
self::JSON_PROPERTY_PRIORITY => $this->collectionPriority,
self::JSON_PROPERTY_VISIBILITY => $this->collectionVisibility,
self::JSON_PROPERTY_COLOR => $this->collectionColor,
self::JSON_PROPERTY_CREATED => $this->collectionCreatedOn,
self::JSON_PROPERTY_MODIFIED => $this->collectionModifiedOn,
self::JSON_PROPERTY_ENABLED => $this->collectionEnabled,
self::JSON_PROPERTY_SIGNATURE => $this->collectionSignature,
self::JSON_PROPERTY_PERMISSIONS => [$this->userId => $this->collectionPermissions],
self::JSON_PROPERTY_ROLES => $this->collectionAttributes['roles'] ?? [],
self::JSON_PROPERTY_CONTENTS => $this->collectionAttributes['contents'] ?? [],
];
}
public function jsonDeserialize(array|string $data): static
{
if (is_string($data)) {
$data = json_decode($data, true);
}
$this->collectionId = $data[self::JSON_PROPERTY_ID] ?? null;
$this->collectionLabel = $data[self::JSON_PROPERTY_LABEL] ?? null;
$this->collectionDescription = $data[self::JSON_PROPERTY_DESCRIPTION] ?? null;
$this->collectionPriority = $data[self::JSON_PROPERTY_PRIORITY] ?? null;
$this->collectionVisibility = $data[self::JSON_PROPERTY_VISIBILITY] ?? null;
$this->collectionColor = $data[self::JSON_PROPERTY_COLOR] ?? null;
$this->collectionCreatedOn = $data[self::JSON_PROPERTY_CREATED] ?? null;
$this->collectionModifiedOn = $data[self::JSON_PROPERTY_MODIFIED] ?? null;
$this->collectionEnabled = $data[self::JSON_PROPERTY_ENABLED] ?? true;
$this->collectionSignature = $data[self::JSON_PROPERTY_SIGNATURE] ?? null;
return $this;
} }
public function fromStore(array|object $data): self public function fromStore(array|object $data): self
{ {
// Convert object to array if needed
if (is_object($data)) { if (is_object($data)) {
$data = (array) $data; $data = (array) $data;
} }
// extract properties
if (isset($data['cid'])) { if (isset($data['cid'])) {
$this->collectionId = $data['cid']; $this->data[self::JSON_PROPERTY_IDENTIFIER] = (string) $data['cid'];
} elseif (isset($data['_id'])) { } elseif (isset($data['_id'])) {
if (is_object($data['_id']) && method_exists($data['_id'], '__toString')) { if (is_object($data['_id']) && method_exists($data['_id'], '__toString')) {
$this->collectionId = (string) $data['_id']; $this->data[self::JSON_PROPERTY_IDENTIFIER] = (string) $data['_id'];
} elseif (is_array($data['_id']) && isset($data['_id']['$oid'])) { } elseif (is_array($data['_id']) && isset($data['_id']['$oid'])) {
$this->collectionId = $data['_id']['$oid']; $this->data[self::JSON_PROPERTY_IDENTIFIER] = (string) $data['_id']['$oid'];
} else { } else {
$this->collectionId = (string) $data['_id']; $this->data[self::JSON_PROPERTY_IDENTIFIER] = (string) $data['_id'];
} }
} }
$this->userId = $data['uid'] ?? null; $this->userId = $data['uid'] ?? null;
$this->collectionLabel = $data['label'] ?? null; $this->collectionUuid = isset($data['uuid']) ? (string) $data['uuid'] : null;
$this->collectionDescription = $data['description'] ?? null; $this->getProperties()->fromStore($data);
$this->collectionColor = $data['color'] ?? null; $this->data[self::JSON_PROPERTY_CREATED] = $data['createdOn'] ?? $data['created'] ?? null;
$this->collectionCreatedOn = $data['created'] ?? null; $this->data[self::JSON_PROPERTY_MODIFIED] = $data['modifiedOn'] ?? $data['modified'] ?? null;
$this->collectionModifiedOn = $data['modified'] ?? null;
$this->collectionEnabled = $data['enabled'] ?? true; $this->collectionEnabled = $data['enabled'] ?? true;
$this->collectionSignature = isset($data['signature']) ? md5((string)$data['signature']) : null; $this->data[self::JSON_PROPERTY_SIGNATURE] = isset($data['signature']) ? md5((string) $data['signature']) : null;
return $this; return $this;
} }
public function toStore(): array public function toStore(): array
{ {
$data = [ $properties = $this->getProperties();
$data = array_filter([
'uid' => $this->userId, 'uid' => $this->userId,
'uuid' => $this->collectionUuid, 'uuid' => $this->collectionUuid,
'label' => $this->collectionLabel, 'label' => $properties->getLabel(),
'description' => $this->collectionDescription, 'description' => $properties->getDescription(),
'color' => $this->collectionColor, 'priority' => $properties->getPriority(),
'created' => $this->collectionCreatedOn, 'visibility' => $properties->getVisibility(),
'modified' => $this->collectionModifiedOn, 'color' => $properties->getColor(),
'signature' => $this->collectionSignature, 'createdOn' => $this->data[self::JSON_PROPERTY_CREATED] ?? null,
'modifiedOn' => $this->data[self::JSON_PROPERTY_MODIFIED] ?? null,
'signature' => $this->data[self::JSON_PROPERTY_SIGNATURE] ?? null,
'enabled' => $this->collectionEnabled, 'enabled' => $this->collectionEnabled,
]; ], static fn($value) => $value !== null);
// Only include _id if it exists (for updates) if ($this->identifier() !== null) {
if ($this->collectionId !== null) { $data['cid'] = (string) $this->identifier();
$data['_id'] = $this->collectionId;
} }
return $data; return $data;
} }
public function in(): null { public function uuid(): ?string {
return null;
}
public function id(): string {
return $this->collectionId;
}
public function created(): ?\DateTimeImmutable {
return $this->collectionCreatedOn ? new \DateTimeImmutable($this->collectionCreatedOn) : null;
}
public function modified(): ?\DateTimeImmutable {
return $this->collectionModifiedOn ? new \DateTimeImmutable($this->collectionModifiedOn) : null;
}
public function attributes(): array {
return $this->collectionAttributes;
}
public function uuid(): string {
return $this->collectionUuid; return $this->collectionUuid;
} }
public function signature(): ?string {
return $this->collectionSignature;
}
public function roles(): array {
return $this->collectionAttributes['roles'] ?? [];
}
public function role(CollectionRoles $role): bool {
return $this->collectionAttributes['roles'][$role->value] ?? false;
}
public function contents(): array {
return $this->collectionAttributes['content'] ?? [];
}
public function contains(CollectionContent $content): bool {
return $this->collectionAttributes['content'][$content->value] ?? false;
}
public function getEnabled(): bool { public function getEnabled(): bool {
return (bool)$this->collectionEnabled; return (bool)$this->collectionEnabled;
} }
@@ -196,57 +90,12 @@ class Collection implements ICollectionMutable {
return $this; return $this;
} }
public function getPermissions(): array { public function getProperties(): CollectionProperties {
return [$this->userId => $this->collectionPermissions]; if (!isset($this->properties)) {
} $this->properties = new CollectionProperties([]);
}
public function hasPermission(CollectionPermissions $permission): bool { return $this->properties;
return $this->collectionPermissions[$permission->value] ?? false;
}
public function getLabel(): ?string {
return $this->collectionLabel;
}
public function setLabel(string $value): self {
$this->collectionLabel = $value;
return $this;
}
public function getDescription(): ?string {
return $this->collectionDescription;
}
public function setDescription(?string $value): self {
$this->collectionDescription = $value;
return $this;
}
public function getPriority(): ?int {
return $this->collectionPriority;
}
public function setPriority(?int $value): self {
$this->collectionPriority = $value;
return $this;
}
public function getVisibility(): ?bool {
return $this->collectionVisibility;
}
public function setVisibility(?bool $value): self {
$this->collectionVisibility = $value;
return $this;
}
public function getColor(): ?string {
return $this->collectionColor;
}
public function setColor(?string $value): self {
$this->collectionColor = $value;
return $this;
} }
} }

View File

@@ -0,0 +1,63 @@
<?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\Collection\CollectionContent;
use KTXF\Chrono\Collection\CollectionPropertiesMutableAbstract;
/**
* Personal Chrono Collection Properties Implementation
*/
class CollectionProperties extends CollectionPropertiesMutableAbstract {
/**
* Converts store document values into collection properties.
*/
public function fromStore(array $data): static {
$this->data[self::JSON_PROPERTY_CONTENTS] = CollectionContent::Event->value;
if (isset($data['label'])) {
$this->data[self::JSON_PROPERTY_LABEL] = (string) $data['label'];
}
if (array_key_exists('description', $data)) {
$this->data[self::JSON_PROPERTY_DESCRIPTION] = $data['description'];
}
if (array_key_exists('priority', $data)) {
$this->data[self::JSON_PROPERTY_PRIORITY] = $data['priority'] !== null ? (int) $data['priority'] : null;
}
if (array_key_exists('visibility', $data)) {
$this->data[self::JSON_PROPERTY_VISIBILITY] = $data['visibility'] !== null ? (bool) $data['visibility'] : null;
}
if (array_key_exists('color', $data)) {
$this->data[self::JSON_PROPERTY_COLOR] = $data['color'];
}
if (isset($data['content'])) {
$this->data[self::JSON_PROPERTY_CONTENTS] = (string) $data['content'];
}
return $this;
}
/**
* Converts collection properties into store document values.
*/
public function toStore(): array {
$content = $this->content();
return array_filter([
'label' => $this->getLabel(),
'description' => $this->getDescription(),
'priority' => $this->getPriority(),
'visibility' => $this->getVisibility(),
'color' => $this->getColor(),
'content' => $content instanceof CollectionContent ? $content->value : null,
], static fn($value) => $value !== null);
}
}

View File

@@ -9,154 +9,86 @@ declare(strict_types=1);
namespace KTXM\ProviderLocalChrono\Providers\Personal; namespace KTXM\ProviderLocalChrono\Providers\Personal;
use DateTimeImmutable; use KTXF\Chrono\Entity\EntityMutableAbstract;
use KTXF\Chrono\Entity\IEntityBase;
use KTXF\Chrono\Entity\IEntityMutable;
use KTXF\Chrono\Event\EventObject; use KTXF\Chrono\Event\EventObject;
/** /**
* Entity wrapper - contains metadata and EntityData * Personal Chrono Entity Resource Implementation
*/ */
class Entity implements IEntityBase, IEntityMutable { class Entity extends EntityMutableAbstract {
// Metadata fields (system-managed)
private ?string $entityId = null;
private ?string $tenantId = null; private ?string $tenantId = null;
private ?string $userId = null; private ?string $userId = null;
private ?string $collectionId = null; private ?EventObject $entityDataObject = null;
private ?string $createdOn = null;
private ?string $modifiedOn = null;
private ?string $entitySignature = null;
// Entity display properties public function __construct(
private ?int $entityPriority = null; string $provider = 'default',
private ?bool $entityVisibility = null; string|int $service = 'personal',
private ?string $entityColor = null; ) {
private string|array|null $entityData = null; parent::__construct($provider, $service);
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 { public function fromStore(array|object $document): self {
// Convert object to array if needed
if (is_object($document)) { if (is_object($document)) {
$document = (array) $document; $document = (array) $document;
} }
// Load metadata $this->data[self::JSON_PROPERTY_IDENTIFIER] = $document['eid'] ?? null;
$this->entityId = $document['eid'] ?? null;
$this->tenantId = $document['tid'] ?? null; $this->tenantId = $document['tid'] ?? null;
$this->userId = $document['uid'] ?? null; $this->userId = $document['uid'] ?? null;
$this->collectionId = $document['cid'] ?? null; $this->data[self::JSON_PROPERTY_COLLECTION] = $document['cid'] ?? null;
$this->createdOn = $document['createdOn'] ?? null; $this->data[self::JSON_PROPERTY_CREATED] = $document['createdOn'] ?? null;
$this->modifiedOn = $document['modifiedOn'] ?? null; $this->data[self::JSON_PROPERTY_MODIFIED] = $document['modifiedOn'] ?? null;
$this->entityData = $document['data'] ?? null; $this->getProperties()->fromStore($document);
$this->entitySignature = md5(json_encode($this->entityData)); $this->data[self::JSON_PROPERTY_SIGNATURE] = md5(json_encode($this->getDataJson()));
$this->entityDataObject = null;
return $this; return $this;
} }
public function toStore(): array { public function toStore(): array {
$document = [ $document = array_filter([
'tid' => $this->tenantId, 'tid' => $this->tenantId,
'uid' => $this->userId, 'uid' => $this->userId,
'cid' => $this->collectionId, 'cid' => $this->collection(),
'eid' => $this->entityId, 'eid' => $this->identifier(),
'createdOn' => $this->createdOn ?? date('c'), 'createdOn' => $this->data[self::JSON_PROPERTY_CREATED] ?? date('c'),
'modifiedOn' => date('c'), 'modifiedOn' => date('c'),
'data' => $this->entityData, ], static fn($value) => $value !== null);
];
$document = array_merge($document, $this->getProperties()->toStore());
return $document; return $document;
} }
public function in(): string|int { public function getProperties(): EntityProperties {
return $this->collectionId ?? ''; if (!isset($this->properties)) {
} $this->properties = new EntityProperties([]);
}
public function id(): string|int { return $this->properties;
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(): EventObject|null { public function getDataObject(): EventObject|null {
return $this->entityData ? (new EventObject)->jsonDeserialize($this->entityData) : null; return $this->entityDataObject;
} }
public function setDataObject(object $value): static public function setDataObject(EventObject $value): static
{ {
if ($value instanceof EventObject) { $this->entityDataObject = $value;
$this->entityData = $value->jsonSerialize(); $this->setDataJson($value->jsonSerialize());
}
return $this; return $this;
} }
public function getDataJson(): array|string|null { public function getDataJson(): array|string|null {
return $this->entityData; return $this->getProperties()->getDataRaw();
} }
public function setDataJson(array|string $value): static public function setDataJson(array|string|null $value): static
{ {
$this->entityData = $value; $this->entityDataObject = null;
$this->getProperties()->setDataRaw($value);
$this->data[self::JSON_PROPERTY_SIGNATURE] = md5(json_encode($value));
return $this; return $this;
} }

View File

@@ -0,0 +1,50 @@
<?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\EntityPropertiesMutableAbstract;
class EntityProperties extends EntityPropertiesMutableAbstract {
public function jsonSerialize(): array {
$data = $this->getDataRaw();
if (is_array($data)) {
return $data;
}
return [];
}
public function jsonDeserialize(array|string $data): static {
if (is_string($data)) {
$data = json_decode($data, true);
}
if (is_array($data) && array_key_exists('data', $data)) {
$this->setDataRaw($data['data']);
return $this;
}
$this->setDataRaw($data);
return $this;
}
public function fromStore(array $data): static {
$this->setDataRaw($data['data'] ?? null);
return $this;
}
public function toStore(): array {
return array_filter([
'data' => $this->getDataRaw(),
], static fn($value) => $value !== null);
}
}

View File

@@ -9,11 +9,14 @@ declare(strict_types=1);
namespace KTXM\ProviderLocalChrono\Providers\Personal; namespace KTXM\ProviderLocalChrono\Providers\Personal;
use KTXF\Chrono\Collection\ICollectionMutable; use KTXF\Chrono\Collection\CollectionBaseInterface;
use KTXF\Chrono\Service\IServiceBase; use KTXF\Chrono\Collection\CollectionMutableInterface;
use KTXF\Chrono\Entity\IEntityMutable; use KTXF\Chrono\Entity\EntityMutableInterface;
use KTXF\Chrono\Service\IServiceCollectionMutable; use KTXF\Chrono\Service\ServiceBaseInterface;
use KTXF\Chrono\Service\IServiceEntityMutable; use KTXF\Chrono\Service\ServiceCollectionMutableInterface;
use KTXF\Chrono\Service\ServiceEntityMutableInterface;
use KTXF\Resource\Delta\Delta;
use KTXF\Resource\Delta\DeltaCollection;
use KTXF\Resource\Exceptions\InvalidParameterException; use KTXF\Resource\Exceptions\InvalidParameterException;
use KTXF\Resource\Filter\Filter; use KTXF\Resource\Filter\Filter;
use KTXF\Resource\Filter\IFilter; use KTXF\Resource\Filter\IFilter;
@@ -25,82 +28,83 @@ use KTXF\Resource\Sort\ISort;
use KTXF\Resource\Sort\Sort; use KTXF\Resource\Sort\Sort;
use KTXM\ProviderLocalChrono\Store\Personal\Store; use KTXM\ProviderLocalChrono\Store\Personal\Store;
class PersonalService implements IServiceBase, IServiceCollectionMutable, IServiceEntityMutable { class PersonalService implements ServiceBaseInterface, ServiceCollectionMutableInterface, ServiceEntityMutableInterface
{
public const JSON_TYPE = ServiceBaseInterface::JSON_TYPE;
protected const SERVICE_ID = 'personal'; private const PROVIDER_IDENTIFIER = 'default';
protected const SERVICE_LABEL = 'Personal Calendar Service'; private const SERVICE_IDENTIFIER = 'personal';
protected const SERVICE_PROVIDER = 'default'; private const SERVICE_LABEL = 'Personal Calendar Service';
protected array $serviceCollectionCache = []; private array $serviceCollectionCache = [];
protected ?string $serviceTenantId = null; private ?string $serviceTenantId = null;
protected ?string $serviceUserId = null; private ?string $serviceUserId = null;
protected ?bool $serviceEnabled = true; private ?bool $serviceEnabled = true;
protected array $serviceAbilities = [ private array $serviceAbilities = [
self::CAPABILITY_COLLECTION_LIST => true, self::CAPABILITY_COLLECTION_LIST => true,
self::CAPABILITY_COLLECTION_LIST_FILTER => [ self::CAPABILITY_COLLECTION_LIST_FILTER => [
self::CAPABILITY_FILTER_ANY => 's:100:256:771', self::CAPABILITY_COLLECTION_FILTER_LABEL => 's:100:256:256',
self::CAPABILITY_FILTER_ID => 'a:10:64:192',
self::CAPABILITY_FILTER_URID => 'a:10:64:192',
self::CAPABILITY_FILTER_LABEL => 's:100:256:771',
self::CAPABILITY_FILTER_DESCRIPTION => 's:100:256:771',
], ],
self::CAPABILITY_COLLECTION_LIST_SORT => [ self::CAPABILITY_COLLECTION_LIST_SORT => [
self::CAPABILITY_SORT_ID, self::CAPABILITY_COLLECTION_SORT_LABEL,
self::CAPABILITY_SORT_URID, self::CAPABILITY_COLLECTION_SORT_RANK,
self::CAPABILITY_SORT_LABEL,
self::CAPABILITY_SORT_PRIORITY
], ],
self::CAPABILITY_COLLECTION_EXTANT => true, self::CAPABILITY_COLLECTION_EXTANT => true,
self::CAPABILITY_COLLECTION_FETCH => true, self::CAPABILITY_COLLECTION_FETCH => true,
self::CAPABILITY_COLLECTION_CREATE => true, self::CAPABILITY_COLLECTION_CREATE => true,
self::CAPABILITY_COLLECTION_MODIFY => true, self::CAPABILITY_COLLECTION_UPDATE => true,
self::CAPABILITY_COLLECTION_DESTROY => true, self::CAPABILITY_COLLECTION_DELETE => true,
self::CAPABILITY_ENTITY_LIST => true, self::CAPABILITY_ENTITY_LIST => true,
self::CAPABILITY_ENTITY_LIST_FILTER => [ self::CAPABILITY_ENTITY_LIST_FILTER => [
self::CAPABILITY_FILTER_ANY => 's:100:256:771', self::CAPABILITY_ENTITY_FILTER_ALL => 's:200:256:256',
self::CAPABILITY_FILTER_ID => 'a:10:64:192', self::CAPABILITY_ENTITY_FILTER_ID => 's:100:256:256',
self::CAPABILITY_FILTER_URID => 'a:10:64:192', self::CAPABILITY_ENTITY_FILTER_URID => 's:100:256:256',
self::CAPABILITY_FILTER_LABEL => 's:100:256:771', self::CAPABILITY_ENTITY_FILTER_LABEL => 's:100:256:256',
], ],
self::CAPABILITY_ENTITY_LIST_SORT => [ self::CAPABILITY_ENTITY_LIST_SORT => [
self::CAPABILITY_SORT_LABEL, self::CAPABILITY_ENTITY_SORT_ID,
self::CAPABILITY_ENTITY_SORT_URID,
], ],
self::CAPABILITY_ENTITY_LIST_RANGE => [ self::CAPABILITY_ENTITY_LIST_RANGE => [
self::CAPABILITY_RANGE_TALLY => [self::CAPABILITY_RANGE_TALLY_ABSOLUTE, self::CAPABILITY_RANGE_TALLY_RELATIVE], self::CAPABILITY_ENTITY_RANGE_TALLY => [
self::CAPABILITY_RANGE_DATE => true self::CAPABILITY_ENTITY_RANGE_TALLY_ABSOLUTE,
self::CAPABILITY_ENTITY_RANGE_TALLY_RELATIVE
],
self::CAPABILITY_ENTITY_RANGE_DATE => true,
], ],
self::CAPABILITY_ENTITY_DELTA => true, self::CAPABILITY_ENTITY_DELTA => true,
self::CAPABILITY_ENTITY_EXTANT => true, self::CAPABILITY_ENTITY_EXTANT => true,
self::CAPABILITY_ENTITY_FETCH => true, self::CAPABILITY_ENTITY_FETCH => true,
self::CAPABILITY_ENTITY_CREATE => true, self::CAPABILITY_ENTITY_CREATE => true,
self::CAPABILITY_ENTITY_MODIFY => true, self::CAPABILITY_ENTITY_UPDATE => true,
self::CAPABILITY_ENTITY_DESTROY => true, self::CAPABILITY_ENTITY_DELETE => true,
self::CAPABILITY_ENTITY_COPY => true,
self::CAPABILITY_ENTITY_MOVE => true,
]; ];
public function __construct( public function __construct(
private Store $store, private Store $store,
) {} ) {}
public function jsonSerialize(): mixed { public function initialize(string $tenantId, string $userId): self {
return [
self::JSON_PROPERTY_TYPE => self::JSON_TYPE,
self::JSON_PROPERTY_PROVIDER => self::SERVICE_PROVIDER,
self::JSON_PROPERTY_ID => self::SERVICE_ID,
self::JSON_PROPERTY_LABEL => self::SERVICE_LABEL,
self::JSON_PROPERTY_CAPABILITIES => $this->serviceAbilities,
self::JSON_PROPERTY_ENABLED => $this->serviceEnabled,
];
}
public function init(string $tenantId, string $userId): self {
$this->serviceTenantId = $tenantId; $this->serviceTenantId = $tenantId;
$this->serviceUserId = $userId; $this->serviceUserId = $userId;
return $this; return $this;
} }
public function jsonSerialize(): array {
return array_filter([
self::JSON_PROPERTY_TYPE => self::JSON_TYPE,
self::JSON_PROPERTY_PROVIDER => self::PROVIDER_IDENTIFIER,
self::JSON_PROPERTY_IDENTIFIER => self::SERVICE_IDENTIFIER,
self::JSON_PROPERTY_LABEL => self::SERVICE_LABEL,
self::JSON_PROPERTY_ENABLED => $this->serviceEnabled,
self::JSON_PROPERTY_CAPABILITIES => $this->serviceAbilities,
self::JSON_PROPERTY_LOCATION => null,
self::JSON_PROPERTY_IDENTITY => null,
self::JSON_PROPERTY_AUXILIARY => [],
], fn($v) => $v !== null);
}
public function capable(string $value): bool { public function capable(string $value): bool {
if (isset($this->serviceAbilities[$value])) { if (isset($this->serviceAbilities[$value])) {
return (bool)$this->serviceAbilities[$value]; return (bool)$this->serviceAbilities[$value];
@@ -112,12 +116,13 @@ class PersonalService implements IServiceBase, IServiceCollectionMutable, IServi
return $this->serviceAbilities; return $this->serviceAbilities;
} }
public function in(): string{ public function provider(): string
return self::SERVICE_PROVIDER; {
} return self::PROVIDER_IDENTIFIER;
}
public function id(): string { public function identifier(): string {
return self::SERVICE_ID; return self::SERVICE_IDENTIFIER;
} }
public function getLabel(): string { public function getLabel(): string {
@@ -128,7 +133,31 @@ class PersonalService implements IServiceBase, IServiceCollectionMutable, IServi
return (bool)$this->serviceEnabled; return (bool)$this->serviceEnabled;
} }
public function collectionList(?IFilter $filter = null, ?ISort $sort = null): array { public function setEnabled(bool $enabled): static
{
$this->serviceEnabled = $enabled;
return $this;
}
public function getLocation(): null
{
return null;
}
public function getIdentity(): null
{
return null;
}
public function getAuxiliary(): array
{
return [];
}
// Collection operations
public function collectionList(string|int|null $location, ?IFilter $filter = null, ?ISort $sort = null): array
{
$entries = $this->store->collectionList($this->serviceTenantId, $this->serviceUserId, $filter, $sort); $entries = $this->store->collectionList($this->serviceTenantId, $this->serviceUserId, $filter, $sort);
$this->serviceCollectionCache = $entries; $this->serviceCollectionCache = $entries;
return $entries ?? []; return $entries ?? [];
@@ -142,24 +171,42 @@ class PersonalService implements IServiceBase, IServiceCollectionMutable, IServi
return new Sort($this->serviceAbilities[self::CAPABILITY_COLLECTION_LIST_SORT] ?? []); return new Sort($this->serviceAbilities[self::CAPABILITY_COLLECTION_LIST_SORT] ?? []);
} }
public function collectionExtant(string|int $id): bool { public function collectionExtant(string|int $location, string|int ...$identifiers): array {
// determine if collection is cached $resolvedIdentifiers = $identifiers !== [] ? $identifiers : [$location];
if (isset($this->serviceCollectionCache[$id])) { $response = [];
return true;
foreach ($resolvedIdentifiers as $identifier) {
if (isset($this->serviceCollectionCache[$identifier])) {
$response[$identifier] = true;
continue;
}
$exists = $this->store->collectionExtant($this->serviceTenantId, $this->serviceUserId, (string)$identifier);
$response[$identifier] = $exists;
if ($exists) {
$collection = $this->store->collectionFetch($this->serviceTenantId, $this->serviceUserId, (string)$identifier);
if ($collection !== null) {
$this->serviceCollectionCache[$identifier] = $collection;
}
}
} }
// retrieve from store
return $this->store->collectionExtant($this->serviceTenantId, $this->serviceUserId, $id); return $response;
} }
public function collectionFetch(string|int $id): ?Collection { public function collectionFetch(string|int $identifier): ?CollectionBaseInterface {
// determine if collection is cached // determine if collection is cached
if (isset($this->serviceCollectionCache[$id])) { if (isset($this->serviceCollectionCache[$identifier])) {
return $this->serviceCollectionCache[$id]; return $this->serviceCollectionCache[$identifier];
} }
// retrieve from store // retrieve from store
$collection = $this->store->collectionFetch($this->serviceTenantId, $this->serviceUserId, $id); $collection = $this->store->collectionFetch($this->serviceTenantId, $this->serviceUserId, (string)$identifier);
if ($collection !== null) { if ($collection !== null) {
$this->serviceCollectionCache[$collection->id()] = $collection; $collectionIdentifier = $collection->identifier();
if ($collectionIdentifier !== null) {
$this->serviceCollectionCache[(string) $collectionIdentifier] = $collection;
}
return $collection; return $collection;
} }
return null; return null;
@@ -169,7 +216,7 @@ class PersonalService implements IServiceBase, IServiceCollectionMutable, IServi
return new Collection(); return new Collection();
} }
public function collectionCreate(string|int $location, ICollectionMutable $collection, array $options): Collection { public function collectionCreate(string|int|null $location, CollectionMutableInterface $collection, array $options = []): CollectionBaseInterface {
// convert collection to a native type if needed // convert collection to a native type if needed
if (!($collection instanceof Collection)) { if (!($collection instanceof Collection)) {
$nativeCollection = new Collection(); $nativeCollection = new Collection();
@@ -179,12 +226,15 @@ class PersonalService implements IServiceBase, IServiceCollectionMutable, IServi
} }
// create collection in store // create collection in store
$result = $this->store->collectionCreate($this->serviceTenantId, $this->serviceUserId, $nativeCollection); $result = $this->store->collectionCreate($this->serviceTenantId, $this->serviceUserId, $nativeCollection);
$this->serviceCollectionCache[$result->id()] = $result; $resultIdentifier = $result->identifier();
if ($resultIdentifier !== null) {
$this->serviceCollectionCache[(string) $resultIdentifier] = $result;
}
return $result; return $result;
} }
public function collectionModify(string|int $id, ICollectionMutable $collection): Collection { public function collectionUpdate(string|int $id, CollectionMutableInterface $collection): CollectionBaseInterface {
// validate id // validate id
if (!is_string($id)) { if (!is_string($id)) {
throw new InvalidParameterException("Invalid: Collection identifier '$id' is not valid"); throw new InvalidParameterException("Invalid: Collection identifier '$id' is not valid");
@@ -196,16 +246,23 @@ class PersonalService implements IServiceBase, IServiceCollectionMutable, IServi
// convert collection to a native type if needed // convert collection to a native type if needed
if (!($collection instanceof Collection)) { if (!($collection instanceof Collection)) {
$nativeCollection = new Collection(); $nativeCollection = new Collection();
$nativeCollection->jsonDeserialize($collection->jsonSerialize()); $data = $collection->jsonSerialize();
$data[Collection::JSON_PROPERTY_IDENTIFIER] = $id;
$nativeCollection->jsonDeserialize($data);
} else { } else {
$nativeCollection = clone $collection; $nativeCollection = clone $collection;
if ($nativeCollection->identifier() === null) {
$data = $nativeCollection->jsonSerialize();
$data[Collection::JSON_PROPERTY_IDENTIFIER] = $id;
$nativeCollection->jsonDeserialize($data);
}
} }
// modify collection in store // modify collection in store
$result = $this->store->collectionModify($this->serviceTenantId, $this->serviceUserId, $nativeCollection); $result = $this->store->collectionModify($this->serviceTenantId, $this->serviceUserId, $nativeCollection);
return $result; return $result;
} }
public function collectionDestroy(string|int $id): bool { public function collectionDelete(string|int $id, bool $force = false, bool $recursive = false): bool {
// validate id // validate id
if (!is_string($id)) { if (!is_string($id)) {
throw new InvalidParameterException("Invalid: Collection identifier '$id' is not valid"); throw new InvalidParameterException("Invalid: Collection identifier '$id' is not valid");
@@ -269,7 +326,7 @@ class PersonalService implements IServiceBase, IServiceCollectionMutable, IServi
return $this->store->entityExtant($collection, ...$identifiers); return $this->store->entityExtant($collection, ...$identifiers);
} }
public function entityDelta(string|int $collection, string $signature, string $detail = 'ids'): array { public function entityDelta(string|int $collection, string $signature, string $detail = 'ids'): Delta {
// validate id // validate id
if (!is_string($collection)) { if (!is_string($collection)) {
throw new InvalidParameterException("Invalid: Collection identifier '$collection' is not valid"); throw new InvalidParameterException("Invalid: Collection identifier '$collection' is not valid");
@@ -279,7 +336,14 @@ class PersonalService implements IServiceBase, IServiceCollectionMutable, IServi
throw new InvalidParameterException("Invalid: Collection identifier '$collection' does not exist or does not belong to user '{$this->serviceUserId}'"); throw new InvalidParameterException("Invalid: Collection identifier '$collection' does not exist or does not belong to user '{$this->serviceUserId}'");
} }
// retrieve entity delta from store // retrieve entity delta from store
return $this->store->chronicleReminisce($this->serviceTenantId, $this->serviceUserId,$collection, $signature); $delta = $this->store->chronicleReminisce($this->serviceTenantId, $collection, $signature);
return new Delta(
new DeltaCollection($delta['additions'] ?? []),
new DeltaCollection($delta['modifications'] ?? []),
new DeltaCollection($delta['deletions'] ?? []),
$delta['signature'] ?? ''
);
} }
public function entityFetch(string|int $collection, string|int ...$identifiers): array { public function entityFetch(string|int $collection, string|int ...$identifiers): array {
@@ -300,7 +364,7 @@ class PersonalService implements IServiceBase, IServiceCollectionMutable, IServi
return new Entity(); return new Entity();
} }
public function entityCreate(string|int $collection, IEntityMutable $entity, array $options): Entity { public function entityCreate(string|int $collection, EntityMutableInterface $entity, array $options = []): Entity {
// validate collection identifier // validate collection identifier
if (!is_string($collection)) { if (!is_string($collection)) {
throw new InvalidParameterException("Invalid: Collection identifier '$collection' is not valid"); throw new InvalidParameterException("Invalid: Collection identifier '$collection' is not valid");
@@ -322,7 +386,7 @@ class PersonalService implements IServiceBase, IServiceCollectionMutable, IServi
return $result; return $result;
} }
public function entityModify(string|int $collection, string|int $identifier, IEntityMutable $entity): Entity { public function entityUpdate(string|int $collection, string|int $identifier, EntityMutableInterface $entity): Entity {
// validate collection identifier // validate collection identifier
if (!is_string($collection)) { if (!is_string($collection)) {
throw new InvalidParameterException("Invalid: Collection identifier '$collection' is not valid"); throw new InvalidParameterException("Invalid: Collection identifier '$collection' is not valid");
@@ -355,7 +419,7 @@ class PersonalService implements IServiceBase, IServiceCollectionMutable, IServi
return $result; return $result;
} }
public function entityDestroy(string|int $collection, string|int $identifier): IEntityMutable { public function entityDelete(string|int $collection, string|int $identifier): EntityMutableInterface {
// validate collection identifier // validate collection identifier
if (!is_string($collection)) { if (!is_string($collection)) {
throw new InvalidParameterException("Invalid: Collection identifier '$collection' is not valid"); throw new InvalidParameterException("Invalid: Collection identifier '$collection' is not valid");

View File

@@ -10,126 +10,90 @@ declare(strict_types=1);
namespace KTXM\ProviderLocalChrono\Providers; namespace KTXM\ProviderLocalChrono\Providers;
use Psr\Container\ContainerInterface; use Psr\Container\ContainerInterface;
use KTXF\Chrono\Provider\IProviderBase; use KTXF\Chrono\Provider\ProviderBaseInterface;
use KTXF\Chrono\Service\IServiceBase; use KTXF\Chrono\Service\ServiceBaseInterface;
use KTXF\Resource\Provider\ProviderInterface;
use KTXM\ProviderLocalChrono\Providers\Personal\PersonalService; use KTXM\ProviderLocalChrono\Providers\Personal\PersonalService;
class Provider implements IProviderBase, ProviderInterface { /**
* Local Storage Provider for Chrono
*/
class Provider implements ProviderBaseInterface
{
protected const PROVIDER_ID = 'default'; public const JSON_TYPE = ProviderBaseInterface::JSON_TYPE;
protected const PROVIDER_IDENTIFIER = 'default';
protected const PROVIDER_LABEL = 'Default Chrono Provider'; protected const PROVIDER_LABEL = 'Default Chrono Provider';
protected const PROVIDER_DESCRIPTION = 'Provides local calendar/event storage'; protected const PROVIDER_DESCRIPTION = 'Provides local calendar/event storage';
protected const PROVIDER_ICON = 'calendar'; protected const PROVIDER_ICON = 'mdi-calendar';
protected array $providerAbilities = [
self::CAPABILITY_SERVICE_LIST => true,
self::CAPABILITY_SERVICE_FETCH => true,
self::CAPABILITY_SERVICE_EXTANT => true,
];
protected array $providerAbilities = [];
private ?array $servicesCache = []; private ?array $servicesCache = [];
public function __construct( public function __construct(
private readonly ContainerInterface $container, private readonly ContainerInterface $container,
) { ) {}
$this->providerAbilities = [
self::CAPABILITY_SERVICE_LIST => true, public function jsonSerialize(): array
self::CAPABILITY_SERVICE_FETCH => true, {
self::CAPABILITY_SERVICE_EXTANT => true, return [
]; self::JSON_PROPERTY_TYPE => self::JSON_TYPE,
self::JSON_PROPERTY_IDENTIFIER => self::PROVIDER_IDENTIFIER,
self::JSON_PROPERTY_LABEL => self::PROVIDER_LABEL,
self::JSON_PROPERTY_CAPABILITIES => $this->providerAbilities,
];
}
public function jsonDeserialize(array|string $data): static
{
return $this;
}
public function type(): string
{
return self::TYPE_MAIL;
}
public function identifier(): string
{
return self::PROVIDER_IDENTIFIER;
}
public function label(): string
{
return self::PROVIDER_LABEL;
}
public function description(): string
{
return self::PROVIDER_DESCRIPTION;
}
public function icon(): string
{
return self::PROVIDER_ICON;
}
public function capable(string $value): bool
{
return !empty($this->providerAbilities[$value]);
}
public function capabilities(): array
{
return $this->providerAbilities;
}
protected function serviceInstancePersonal(string $tenantId, string $userId): PersonalService {
$service = $this->container->get(PersonalService::class);
$service->initialize($tenantId, $userId);
return $service;
} }
/**
* @inheritDoc
*/
public function type(): string {
return ProviderInterface::TYPE_CHRONO;
}
/**
* @inheritDoc
*/
public function identifier(): string {
return self::PROVIDER_ID;
}
/**
* @inheritDoc
*/
public function description(): string {
return self::PROVIDER_DESCRIPTION;
}
/**
* @inheritDoc
*/
public function icon(): string {
return self::PROVIDER_ICON;
}
public function jsonSerialize(): mixed {
return $this->toJson();
}
public function toJson(): array {
return [
self::JSON_PROPERTY_TYPE => self::JSON_TYPE,
self::JSON_PROPERTY_ID => self::PROVIDER_ID,
self::JSON_PROPERTY_LABEL => self::PROVIDER_LABEL,
self::JSON_PROPERTY_CAPABILITIES => $this->providerAbilities,
];
}
/**
* Confirms if specific capability is supported
*
* @since 1.0.0
*
* @inheritdoc
*/
public function capable(string $value): bool {
if (isset($this->providerAbilities[$value])) {
return (bool)$this->providerAbilities[$value];
}
return false;
}
/**
* Lists all supported capabilities
*
* @since 1.0.0
*
* @inheritdoc
*/
public function capabilities(): array {
return $this->providerAbilities;
}
/**
* An arbitrary unique text string identifying this provider
*
* @since 1.0.0
*
* @inheritdoc
*/
public function id(): string {
return self::PROVIDER_ID;
}
/**
* The localized human friendly name of this provider
*
* @since 1.0.0
*
* @inheritdoc
*/
public function label(): string {
return self::PROVIDER_LABEL;
}
/**
* Retrieve collection of services for a specific user
*
* @since 1.0.0
*
* @inheritdoc
*/
public function serviceList(string $tenantId, string $userId, array $filter = []): array { public function serviceList(string $tenantId, string $userId, array $filter = []): array {
// if no filter is provided, return all services // if no filter is provided, return all services
if ($filter === []) { if ($filter === []) {
@@ -143,45 +107,7 @@ class Provider implements IProviderBase, ProviderInterface {
return array_intersect_key($this->servicesCache[$userId],array_flip($filter)); return array_intersect_key($this->servicesCache[$userId],array_flip($filter));
} }
/** public function serviceFetch(string $tenantId, string $userId, string|int $identifier): ?ServiceBaseInterface {
* construct service object instance
*
* @since 1.0.0
*
* @return PersonalService blank service instance
*/
protected function serviceInstancePersonal(string $tenantId, string $userId): PersonalService {
$service = $this->container->get(PersonalService::class);
$service->init($tenantId, $userId);
return $service;
}
/**
* Determine if any services are configured for a specific user
*
* @since 1.0.0
*
* @inheritdoc
*/
public function serviceExtant(string $tenantId, string $userId, int|string ...$identifiers): array {
$data = [];
foreach ($identifiers as $id) {
$data[$id] = match ($id) {
'personal' => true,
default => false,
};
}
return $data;
}
/**
* Retrieve a service with a specific identifier
*
* @since 1.0.0
*
* @inheritdoc
*/
public function serviceFetch(string $tenantId, string $userId, string|int $identifier): ?IServiceBase {
// check if services are cached // check if services are cached
if (isset($this->servicesCache[$userId][$identifier])) { if (isset($this->servicesCache[$userId][$identifier])) {
@@ -196,4 +122,15 @@ class Provider implements IProviderBase, ProviderInterface {
} }
public function serviceExtant(string $tenantId, string $userId, int|string ...$identifiers): array {
$data = [];
foreach ($identifiers as $id) {
$data[$id] = match ($id) {
'personal' => true,
default => false,
};
}
return $data;
}
} }

View File

@@ -146,7 +146,10 @@ class Store {
$list = []; $list = [];
foreach ($cursor as $entry) { foreach ($cursor as $entry) {
$entry = (new Collection())->fromStore($entry); $entry = (new Collection())->fromStore($entry);
$list[$entry->id()] = $entry; $identifier = $entry->identifier();
if ($identifier !== null) {
$list[(string) $identifier] = $entry;
}
} }
return $list; return $list;
} }
@@ -220,6 +223,7 @@ class Store {
$data['tid'] = $tenantId; $data['tid'] = $tenantId;
$data['uid'] = $userId; $data['uid'] = $userId;
$data['cid'] = UUID::v4(); $data['cid'] = UUID::v4();
$data['signature'] = isset($data['signature']) && is_numeric($data['signature']) ? (int)$data['signature'] : 0;
$data['createdOn'] = date('c'); $data['createdOn'] = date('c');
$data['modifiedOn'] = $data['createdOn']; $data['modifiedOn'] = $data['createdOn'];
// create entry // create entry
@@ -245,7 +249,10 @@ class Store {
// convert entity to store format // convert entity to store format
$data = $entity->toStore(); $data = $entity->toStore();
// prepare data for modification // prepare data for modification
$cid = $entity->id(); $cid = $entity->identifier();
if ($cid === null) {
throw new \InvalidArgumentException('Collection identifier is required for modification');
}
$data['modifiedOn'] = date('c'); $data['modifiedOn'] = date('c');
unset($data['_id'], $data['tid'], $data['uid'], $data['cid']); unset($data['_id'], $data['tid'], $data['uid'], $data['cid']);
// modify entry // modify entry
@@ -266,7 +273,11 @@ class Store {
* @return Collection * @return Collection
*/ */
public function collectionDestroy(string $tenantId, string $userId, Collection $entity): Collection { public function collectionDestroy(string $tenantId, string $userId, Collection $entity): Collection {
return $this->collectionDestroyById($tenantId, $userId, $entity->id()) ? $entity : $entity; $identifier = $entity->identifier();
if ($identifier === null) {
return $entity;
}
return $this->collectionDestroyById($tenantId, $userId, (string) $identifier) ? $entity : $entity;
} }
/** /**
@@ -341,7 +352,10 @@ class Store {
$list = []; $list = [];
foreach ($cursor as $entry) { foreach ($cursor as $entry) {
$entity = (new Entity())->fromStore($entry); $entity = (new Entity())->fromStore($entry);
$list[$entity->id()] = $entity; $identifier = $entity->identifier();
if ($identifier !== null) {
$list[(string) $identifier] = $entity;
}
} }
return $list; return $list;
} }
@@ -405,7 +419,10 @@ class Store {
$list = []; $list = [];
foreach ($cursor as $entry) { foreach ($cursor as $entry) {
$entity = (new Entity())->fromStore($entry); $entity = (new Entity())->fromStore($entry);
$list[$entity->id()] = $entity; $identifier = $entity->identifier();
if ($identifier !== null) {
$list[(string) $identifier] = $entity;
}
} }
return $list; return $list;
@@ -459,7 +476,7 @@ class Store {
if ($result->getInsertedCount() === 1) { if ($result->getInsertedCount() === 1) {
$eid = $data['eid']; $eid = $data['eid'];
$entity->fromStore(['eid' => $eid, 'tid' => $tenantId, 'uid' => $userId, 'cid' => $collectionId]); $entity->fromStore($data);
// Chronicle the creation (operation 1) // Chronicle the creation (operation 1)
$this->chronicleDocument($tenantId, $collectionId, $eid, 1); $this->chronicleDocument($tenantId, $collectionId, $eid, 1);
} }
@@ -512,18 +529,21 @@ class Store {
* @return Entity * @return Entity
*/ */
public function entityDestroy(string $tenantId, string $userId, string $collectionId, Entity $entity): Entity { public function entityDestroy(string $tenantId, string $userId, string $collectionId, Entity $entity): Entity {
$identifier = $entity->id(); $identifier = $entity->identifier();
if ($identifier === null) {
return $entity;
}
$result = $this->_store->selectCollection($this->_EntityTable)->deleteOne([ $result = $this->_store->selectCollection($this->_EntityTable)->deleteOne([
'tid' => $tenantId, 'tid' => $tenantId,
'uid' => $userId, 'uid' => $userId,
'cid' => $collectionId, 'cid' => $collectionId,
'eid' => $identifier 'eid' => (string) $identifier
]); ]);
if ($result->getDeletedCount() === 1) { if ($result->getDeletedCount() === 1) {
// Chronicle the deletion (operation 3) // Chronicle the deletion (operation 3)
$this->chronicleDocument($tenantId, $collectionId, $identifier, 3); $this->chronicleDocument($tenantId, $collectionId, (string) $identifier, 3);
} }
return $entity; return $entity;
@@ -570,12 +590,19 @@ class Store {
private function chronicleDocument(string $tid, string $cid, string $eid, int $operation): void { private function chronicleDocument(string $tid, string $cid, string $eid, int $operation): void {
// retrieve current token from collection // retrieve current token from collection
$collection = $this->_store->selectCollection($this->_CollectionTable)->findOne([ $collection = $this->_store->selectCollection($this->_CollectionTable)->findOne([
'tid' => $tid,
'cid' => $cid 'cid' => $cid
], [ ], [
'projection' => ['signature' => 1, '_id' => 0] 'projection' => ['signature' => 1, '_id' => 0]
]); ]);
$signature = $collection['signature'] ?? 0; $signatureRaw = $collection['signature'] ?? 0;
if (is_numeric($signatureRaw)) {
$signature = (int)$signatureRaw;
} else {
$decoded = is_string($signatureRaw) ? base64_decode($signatureRaw, true) : false;
$signature = (is_string($decoded) && is_numeric($decoded)) ? (int)$decoded : 0;
}
// document operation in chronicle // document operation in chronicle
$this->_store->selectCollection($this->_ChronicleTable)->insertOne([ $this->_store->selectCollection($this->_ChronicleTable)->insertOne([
@@ -587,10 +614,10 @@ class Store {
'mutatedOn' => time(), 'mutatedOn' => time(),
]); ]);
// increment token atomically // update signature as normalized numeric value
$this->_store->selectCollection($this->_CollectionTable)->updateOne( $this->_store->selectCollection($this->_CollectionTable)->updateOne(
['cid' => $cid], ['tid' => $tid, 'cid' => $cid],
['$inc' => ['signature' => 1]] ['$set' => ['signature' => $signature + 1]]
); );
} }