chore: standardize chrono provider #3

Merged
Sebastian merged 1 commits from chore/standardize-chrono-provider into main 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;
use KTXF\Chrono\Collection\CollectionContent;
use KTXF\Chrono\Collection\CollectionPermissions;
use KTXF\Chrono\Collection\CollectionRoles;
use KTXF\Chrono\Collection\ICollectionMutable;
use KTXF\Chrono\Collection\CollectionMutableAbstract;
class Collection implements ICollectionMutable {
class Collection extends CollectionMutableAbstract {
private ?string $userId = null;
private string $providerId = 'default';
private string $serviceId = 'personal';
private ?string $collectionId = 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 ?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 {
return [
self::JSON_PROPERTY_TYPE => self::JSON_TYPE,
self::JSON_PROPERTY_PROVIDER => $this->providerId,
self::JSON_PROPERTY_SERVICE => $this->serviceId,
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 __construct(
string $provider = 'default',
string|int $service = 'personal',
) {
parent::__construct($provider, $service);
}
public function fromStore(array|object $data): self
{
// Convert object to array if needed
if (is_object($data)) {
$data = (array) $data;
}
// extract properties
if (isset($data['cid'])) {
$this->collectionId = $data['cid'];
$this->data[self::JSON_PROPERTY_IDENTIFIER] = (string) $data['cid'];
} elseif (isset($data['_id'])) {
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'])) {
$this->collectionId = $data['_id']['$oid'];
$this->data[self::JSON_PROPERTY_IDENTIFIER] = (string) $data['_id']['$oid'];
} else {
$this->collectionId = (string) $data['_id'];
$this->data[self::JSON_PROPERTY_IDENTIFIER] = (string) $data['_id'];
}
}
$this->userId = $data['uid'] ?? null;
$this->collectionLabel = $data['label'] ?? null;
$this->collectionDescription = $data['description'] ?? null;
$this->collectionColor = $data['color'] ?? null;
$this->collectionCreatedOn = $data['created'] ?? null;
$this->collectionModifiedOn = $data['modified'] ?? null;
$this->collectionUuid = isset($data['uuid']) ? (string) $data['uuid'] : null;
$this->getProperties()->fromStore($data);
$this->data[self::JSON_PROPERTY_CREATED] = $data['createdOn'] ?? $data['created'] ?? null;
$this->data[self::JSON_PROPERTY_MODIFIED] = $data['modifiedOn'] ?? $data['modified'] ?? null;
$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;
}
public function toStore(): array
{
$data = [
$properties = $this->getProperties();
$data = array_filter([
'uid' => $this->userId,
'uuid' => $this->collectionUuid,
'label' => $this->collectionLabel,
'description' => $this->collectionDescription,
'color' => $this->collectionColor,
'created' => $this->collectionCreatedOn,
'modified' => $this->collectionModifiedOn,
'signature' => $this->collectionSignature,
'label' => $properties->getLabel(),
'description' => $properties->getDescription(),
'priority' => $properties->getPriority(),
'visibility' => $properties->getVisibility(),
'color' => $properties->getColor(),
'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,
];
], static fn($value) => $value !== null);
// Only include _id if it exists (for updates)
if ($this->collectionId !== null) {
$data['_id'] = $this->collectionId;
if ($this->identifier() !== null) {
$data['cid'] = (string) $this->identifier();
}
return $data;
}
public function in(): null {
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 {
public function uuid(): ?string {
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 {
return (bool)$this->collectionEnabled;
}
@@ -196,57 +90,12 @@ class Collection implements ICollectionMutable {
return $this;
}
public function getPermissions(): array {
return [$this->userId => $this->collectionPermissions];
}
public function getProperties(): CollectionProperties {
if (!isset($this->properties)) {
$this->properties = new CollectionProperties([]);
}
public function hasPermission(CollectionPermissions $permission): bool {
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;
return $this->properties;
}
}

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;
use DateTimeImmutable;
use KTXF\Chrono\Entity\IEntityBase;
use KTXF\Chrono\Entity\IEntityMutable;
use KTXF\Chrono\Entity\EntityMutableAbstract;
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 $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;
private ?EventObject $entityDataObject = 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 __construct(
string $provider = 'default',
string|int $service = 'personal',
) {
parent::__construct($provider, $service);
}
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->data[self::JSON_PROPERTY_IDENTIFIER] = $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));
$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 = [
$document = array_filter([
'tid' => $this->tenantId,
'uid' => $this->userId,
'cid' => $this->collectionId,
'eid' => $this->entityId,
'createdOn' => $this->createdOn ?? date('c'),
'cid' => $this->collection(),
'eid' => $this->identifier(),
'createdOn' => $this->data[self::JSON_PROPERTY_CREATED] ?? date('c'),
'modifiedOn' => date('c'),
'data' => $this->entityData,
];
], static fn($value) => $value !== null);
$document = array_merge($document, $this->getProperties()->toStore());
return $document;
}
public function in(): string|int {
return $this->collectionId ?? '';
}
public function getProperties(): EntityProperties {
if (!isset($this->properties)) {
$this->properties = new EntityProperties([]);
}
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;
return $this->properties;
}
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->entityData = $value->jsonSerialize();
}
$this->entityDataObject = $value;
$this->setDataJson($value->jsonSerialize());
return $this;
}
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;
}

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;
use KTXF\Chrono\Collection\ICollectionMutable;
use KTXF\Chrono\Service\IServiceBase;
use KTXF\Chrono\Entity\IEntityMutable;
use KTXF\Chrono\Service\IServiceCollectionMutable;
use KTXF\Chrono\Service\IServiceEntityMutable;
use KTXF\Chrono\Collection\CollectionBaseInterface;
use KTXF\Chrono\Collection\CollectionMutableInterface;
use KTXF\Chrono\Entity\EntityMutableInterface;
use KTXF\Chrono\Service\ServiceBaseInterface;
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\Filter\Filter;
use KTXF\Resource\Filter\IFilter;
@@ -25,82 +28,83 @@ use KTXF\Resource\Sort\ISort;
use KTXF\Resource\Sort\Sort;
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';
protected const SERVICE_LABEL = 'Personal Calendar Service';
protected const SERVICE_PROVIDER = 'default';
private const PROVIDER_IDENTIFIER = 'default';
private const SERVICE_IDENTIFIER = 'personal';
private const SERVICE_LABEL = 'Personal Calendar Service';
protected array $serviceCollectionCache = [];
protected ?string $serviceTenantId = null;
protected ?string $serviceUserId = null;
protected ?bool $serviceEnabled = true;
private array $serviceCollectionCache = [];
private ?string $serviceTenantId = null;
private ?string $serviceUserId = null;
private ?bool $serviceEnabled = true;
protected array $serviceAbilities = [
private array $serviceAbilities = [
self::CAPABILITY_COLLECTION_LIST => true,
self::CAPABILITY_COLLECTION_LIST_FILTER => [
self::CAPABILITY_FILTER_ANY => 's:100:256:771',
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_FILTER_LABEL => 's:100:256:256',
],
self::CAPABILITY_COLLECTION_LIST_SORT => [
self::CAPABILITY_SORT_ID,
self::CAPABILITY_SORT_URID,
self::CAPABILITY_SORT_LABEL,
self::CAPABILITY_SORT_PRIORITY
self::CAPABILITY_COLLECTION_SORT_LABEL,
self::CAPABILITY_COLLECTION_SORT_RANK,
],
self::CAPABILITY_COLLECTION_EXTANT => true,
self::CAPABILITY_COLLECTION_FETCH => true,
self::CAPABILITY_COLLECTION_CREATE => true,
self::CAPABILITY_COLLECTION_MODIFY => true,
self::CAPABILITY_COLLECTION_DESTROY => true,
self::CAPABILITY_COLLECTION_CREATE => true,
self::CAPABILITY_COLLECTION_UPDATE => true,
self::CAPABILITY_COLLECTION_DELETE => true,
self::CAPABILITY_ENTITY_LIST => true,
self::CAPABILITY_ENTITY_LIST_FILTER => [
self::CAPABILITY_FILTER_ANY => 's:100:256:771',
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_ENTITY_FILTER_ALL => 's:200:256:256',
self::CAPABILITY_ENTITY_FILTER_ID => 's:100:256:256',
self::CAPABILITY_ENTITY_FILTER_URID => 's:100:256:256',
self::CAPABILITY_ENTITY_FILTER_LABEL => 's:100:256:256',
],
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_RANGE_TALLY => [self::CAPABILITY_RANGE_TALLY_ABSOLUTE, self::CAPABILITY_RANGE_TALLY_RELATIVE],
self::CAPABILITY_RANGE_DATE => true
self::CAPABILITY_ENTITY_RANGE_TALLY => [
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_EXTANT => true,
self::CAPABILITY_ENTITY_FETCH => true,
self::CAPABILITY_ENTITY_CREATE => true,
self::CAPABILITY_ENTITY_MODIFY => true,
self::CAPABILITY_ENTITY_DESTROY => true,
self::CAPABILITY_ENTITY_COPY => true,
self::CAPABILITY_ENTITY_MOVE => true,
self::CAPABILITY_ENTITY_UPDATE => true,
self::CAPABILITY_ENTITY_DELETE => true,
];
public function __construct(
private Store $store,
) {}
public function jsonSerialize(): mixed {
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 {
public function initialize(string $tenantId, string $userId): self {
$this->serviceTenantId = $tenantId;
$this->serviceUserId = $userId;
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 {
if (isset($this->serviceAbilities[$value])) {
return (bool)$this->serviceAbilities[$value];
@@ -112,12 +116,13 @@ class PersonalService implements IServiceBase, IServiceCollectionMutable, IServi
return $this->serviceAbilities;
}
public function in(): string{
return self::SERVICE_PROVIDER;
}
public function provider(): string
{
return self::PROVIDER_IDENTIFIER;
}
public function id(): string {
return self::SERVICE_ID;
public function identifier(): string {
return self::SERVICE_IDENTIFIER;
}
public function getLabel(): string {
@@ -128,7 +133,31 @@ class PersonalService implements IServiceBase, IServiceCollectionMutable, IServi
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);
$this->serviceCollectionCache = $entries;
return $entries ?? [];
@@ -142,24 +171,42 @@ class PersonalService implements IServiceBase, IServiceCollectionMutable, IServi
return new Sort($this->serviceAbilities[self::CAPABILITY_COLLECTION_LIST_SORT] ?? []);
}
public function collectionExtant(string|int $id): bool {
// determine if collection is cached
if (isset($this->serviceCollectionCache[$id])) {
return true;
public function collectionExtant(string|int $location, string|int ...$identifiers): array {
$resolvedIdentifiers = $identifiers !== [] ? $identifiers : [$location];
$response = [];
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
if (isset($this->serviceCollectionCache[$id])) {
return $this->serviceCollectionCache[$id];
if (isset($this->serviceCollectionCache[$identifier])) {
return $this->serviceCollectionCache[$identifier];
}
// 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) {
$this->serviceCollectionCache[$collection->id()] = $collection;
$collectionIdentifier = $collection->identifier();
if ($collectionIdentifier !== null) {
$this->serviceCollectionCache[(string) $collectionIdentifier] = $collection;
}
return $collection;
}
return null;
@@ -169,7 +216,7 @@ class PersonalService implements IServiceBase, IServiceCollectionMutable, IServi
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
if (!($collection instanceof Collection)) {
$nativeCollection = new Collection();
@@ -179,12 +226,15 @@ class PersonalService implements IServiceBase, IServiceCollectionMutable, IServi
}
// create collection in store
$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;
}
public function collectionModify(string|int $id, ICollectionMutable $collection): Collection {
public function collectionUpdate(string|int $id, CollectionMutableInterface $collection): CollectionBaseInterface {
// validate id
if (!is_string($id)) {
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
if (!($collection instanceof Collection)) {
$nativeCollection = new Collection();
$nativeCollection->jsonDeserialize($collection->jsonSerialize());
$data = $collection->jsonSerialize();
$data[Collection::JSON_PROPERTY_IDENTIFIER] = $id;
$nativeCollection->jsonDeserialize($data);
} else {
$nativeCollection = clone $collection;
if ($nativeCollection->identifier() === null) {
$data = $nativeCollection->jsonSerialize();
$data[Collection::JSON_PROPERTY_IDENTIFIER] = $id;
$nativeCollection->jsonDeserialize($data);
}
}
// modify collection in store
$result = $this->store->collectionModify($this->serviceTenantId, $this->serviceUserId, $nativeCollection);
return $result;
}
public function collectionDestroy(string|int $id): bool {
public function collectionDelete(string|int $id, bool $force = false, bool $recursive = false): bool {
// validate id
if (!is_string($id)) {
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);
}
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
if (!is_string($collection)) {
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}'");
}
// 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 {
@@ -300,7 +364,7 @@ class PersonalService implements IServiceBase, IServiceCollectionMutable, IServi
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
if (!is_string($collection)) {
throw new InvalidParameterException("Invalid: Collection identifier '$collection' is not valid");
@@ -322,7 +386,7 @@ class PersonalService implements IServiceBase, IServiceCollectionMutable, IServi
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
if (!is_string($collection)) {
throw new InvalidParameterException("Invalid: Collection identifier '$collection' is not valid");
@@ -355,7 +419,7 @@ class PersonalService implements IServiceBase, IServiceCollectionMutable, IServi
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
if (!is_string($collection)) {
throw new InvalidParameterException("Invalid: Collection identifier '$collection' is not valid");

View File

@@ -10,126 +10,90 @@ declare(strict_types=1);
namespace KTXM\ProviderLocalChrono\Providers;
use Psr\Container\ContainerInterface;
use KTXF\Chrono\Provider\IProviderBase;
use KTXF\Chrono\Service\IServiceBase;
use KTXF\Resource\Provider\ProviderInterface;
use KTXF\Chrono\Provider\ProviderBaseInterface;
use KTXF\Chrono\Service\ServiceBaseInterface;
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_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 = [];
public function __construct(
private readonly ContainerInterface $container,
) {
$this->providerAbilities = [
self::CAPABILITY_SERVICE_LIST => true,
self::CAPABILITY_SERVICE_FETCH => true,
self::CAPABILITY_SERVICE_EXTANT => true,
];
) {}
public function jsonSerialize(): array
{
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 {
// if no filter is provided, return all services
if ($filter === []) {
@@ -143,45 +107,7 @@ class Provider implements IProviderBase, ProviderInterface {
return array_intersect_key($this->servicesCache[$userId],array_flip($filter));
}
/**
* 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 {
public function serviceFetch(string $tenantId, string $userId, string|int $identifier): ?ServiceBaseInterface {
// check if services are cached
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 = [];
foreach ($cursor as $entry) {
$entry = (new Collection())->fromStore($entry);
$list[$entry->id()] = $entry;
$identifier = $entry->identifier();
if ($identifier !== null) {
$list[(string) $identifier] = $entry;
}
}
return $list;
}
@@ -220,6 +223,7 @@ class Store {
$data['tid'] = $tenantId;
$data['uid'] = $userId;
$data['cid'] = UUID::v4();
$data['signature'] = isset($data['signature']) && is_numeric($data['signature']) ? (int)$data['signature'] : 0;
$data['createdOn'] = date('c');
$data['modifiedOn'] = $data['createdOn'];
// create entry
@@ -245,7 +249,10 @@ class Store {
// convert entity to store format
$data = $entity->toStore();
// 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');
unset($data['_id'], $data['tid'], $data['uid'], $data['cid']);
// modify entry
@@ -266,7 +273,11 @@ class Store {
* @return 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 = [];
foreach ($cursor as $entry) {
$entity = (new Entity())->fromStore($entry);
$list[$entity->id()] = $entity;
$identifier = $entity->identifier();
if ($identifier !== null) {
$list[(string) $identifier] = $entity;
}
}
return $list;
}
@@ -405,7 +419,10 @@ class Store {
$list = [];
foreach ($cursor as $entry) {
$entity = (new Entity())->fromStore($entry);
$list[$entity->id()] = $entity;
$identifier = $entity->identifier();
if ($identifier !== null) {
$list[(string) $identifier] = $entity;
}
}
return $list;
@@ -459,7 +476,7 @@ class Store {
if ($result->getInsertedCount() === 1) {
$eid = $data['eid'];
$entity->fromStore(['eid' => $eid, 'tid' => $tenantId, 'uid' => $userId, 'cid' => $collectionId]);
$entity->fromStore($data);
// Chronicle the creation (operation 1)
$this->chronicleDocument($tenantId, $collectionId, $eid, 1);
}
@@ -512,18 +529,21 @@ class Store {
* @return 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([
'tid' => $tenantId,
'uid' => $userId,
'cid' => $collectionId,
'eid' => $identifier
'eid' => (string) $identifier
]);
if ($result->getDeletedCount() === 1) {
// Chronicle the deletion (operation 3)
$this->chronicleDocument($tenantId, $collectionId, $identifier, 3);
$this->chronicleDocument($tenantId, $collectionId, (string) $identifier, 3);
}
return $entity;
@@ -570,12 +590,19 @@ class Store {
private function chronicleDocument(string $tid, string $cid, string $eid, int $operation): void {
// retrieve current token from collection
$collection = $this->_store->selectCollection($this->_CollectionTable)->findOne([
'tid' => $tid,
'cid' => $cid
], [
'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
$this->_store->selectCollection($this->_ChronicleTable)->insertOne([
@@ -587,10 +614,10 @@ class Store {
'mutatedOn' => time(),
]);
// increment token atomically
// update signature as normalized numeric value
$this->_store->selectCollection($this->_CollectionTable)->updateOne(
['cid' => $cid],
['$inc' => ['signature' => 1]]
['tid' => $tid, 'cid' => $cid],
['$set' => ['signature' => $signature + 1]]
);
}