Initial commit
This commit is contained in:
29
.gitignore
vendored
Normal file
29
.gitignore
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
# Frontend development
|
||||
node_modules/
|
||||
*.local
|
||||
.env.local
|
||||
.env.*.local
|
||||
.cache/
|
||||
.vite/
|
||||
.temp/
|
||||
.tmp/
|
||||
|
||||
# Frontend build
|
||||
/static/
|
||||
|
||||
# Backend development
|
||||
/lib/vendor/
|
||||
coverage/
|
||||
phpunit.xml.cache
|
||||
.phpunit.result.cache
|
||||
.php-cs-fixer.cache
|
||||
.phpstan.cache
|
||||
.phpactor/
|
||||
|
||||
# Editors
|
||||
.DS_Store
|
||||
.vscode/
|
||||
.idea/
|
||||
|
||||
# Logs
|
||||
*.log
|
||||
26
composer.json
Normal file
26
composer.json
Normal file
@@ -0,0 +1,26 @@
|
||||
{
|
||||
"name": "ktrix/chrono-provider-local",
|
||||
"type": "project",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Sebastian Krupinski",
|
||||
"email": "krupinski01@gmail.com"
|
||||
}
|
||||
],
|
||||
"config": {
|
||||
"optimize-autoloader": true,
|
||||
"platform": {
|
||||
"php": "8.2"
|
||||
},
|
||||
"autoloader-suffix": "ChronoProviderLocal",
|
||||
"vendor-dir": "lib/vendor"
|
||||
},
|
||||
"require": {
|
||||
"php": ">=8.2 <=8.5"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"KTXM\\ChronoProviderLocal\\": "lib/"
|
||||
}
|
||||
}
|
||||
}
|
||||
23
composer.lock
generated
Normal file
23
composer.lock
generated
Normal file
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"_readme": [
|
||||
"This file locks the dependencies of your project to a known state",
|
||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "4aa5faa1a29f2cdf4846836b624c1b48",
|
||||
"packages": [],
|
||||
"packages-dev": [],
|
||||
"aliases": [],
|
||||
"minimum-stability": "stable",
|
||||
"stability-flags": [],
|
||||
"prefer-stable": false,
|
||||
"prefer-lowest": false,
|
||||
"platform": {
|
||||
"php": ">=8.2 <=8.5"
|
||||
},
|
||||
"platform-dev": [],
|
||||
"platform-overrides": {
|
||||
"php": "8.2"
|
||||
},
|
||||
"plugin-api-version": "2.3.0"
|
||||
}
|
||||
72
lib/Module.php
Normal file
72
lib/Module.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace KTXM\ChronoProviderLocal;
|
||||
|
||||
use KTXC\Resource\ProviderManager;
|
||||
use KTXF\Module\ModuleBrowserInterface;
|
||||
use KTXF\Module\ModuleInstanceAbstract;
|
||||
use KTXF\Resource\Provider\ProviderInterface;
|
||||
use KTXM\ChronoProviderLocal\Providers\Provider;
|
||||
|
||||
/**
|
||||
* Chrono Provider Local Module
|
||||
*/
|
||||
class Module extends ModuleInstanceAbstract implements ModuleBrowserInterface
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ProviderManager $providerManager,
|
||||
) {}
|
||||
|
||||
public function handle(): string
|
||||
{
|
||||
return 'chrono_provider_local';
|
||||
}
|
||||
|
||||
public function label(): string
|
||||
{
|
||||
return 'Chrono Provider Local';
|
||||
}
|
||||
|
||||
public function author(): string
|
||||
{
|
||||
return 'Ktrix';
|
||||
}
|
||||
|
||||
public function description(): string
|
||||
{
|
||||
return 'Chrono provider module for Ktrix - provides local calendar/event storage';
|
||||
}
|
||||
|
||||
public function version(): string
|
||||
{
|
||||
return '0.0.1';
|
||||
}
|
||||
|
||||
public function permissions(): array
|
||||
{
|
||||
return [
|
||||
'chrono_provider_local' => [
|
||||
'label' => 'Access Chrono Provider Local',
|
||||
'description' => 'View and access the local chrono provider module',
|
||||
'group' => 'Calendar Providers'
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function boot(): void
|
||||
{
|
||||
$this->providerManager->register(ProviderInterface::TYPE_CHRONO, 'default', Provider::class);
|
||||
}
|
||||
|
||||
public function registerBI(): array {
|
||||
return [
|
||||
'handle' => $this->handle(),
|
||||
'namespace' => 'ChronoProviderLocal',
|
||||
'version' => $this->version(),
|
||||
'label' => $this->label(),
|
||||
'author' => $this->author(),
|
||||
'description' => $this->description()
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
252
lib/Providers/Personal/Collection.php
Normal file
252
lib/Providers/Personal/Collection.php
Normal file
@@ -0,0 +1,252 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
namespace KTXM\ChronoProviderLocal\Providers\Personal;
|
||||
|
||||
use KTXF\Chrono\Collection\CollectionContent;
|
||||
use KTXF\Chrono\Collection\CollectionPermissions;
|
||||
use KTXF\Chrono\Collection\CollectionRoles;
|
||||
use KTXF\Chrono\Collection\ICollectionMutable;
|
||||
|
||||
class Collection implements ICollectionMutable {
|
||||
|
||||
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 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'];
|
||||
} elseif (isset($data['_id'])) {
|
||||
if (is_object($data['_id']) && method_exists($data['_id'], '__toString')) {
|
||||
$this->collectionId = (string) $data['_id'];
|
||||
} elseif (is_array($data['_id']) && isset($data['_id']['$oid'])) {
|
||||
$this->collectionId = $data['_id']['$oid'];
|
||||
} else {
|
||||
$this->collectionId = (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->collectionEnabled = $data['enabled'] ?? true;
|
||||
$this->collectionSignature = isset($data['signature']) ? md5((string)$data['signature']) : null;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function toStore(): array
|
||||
{
|
||||
$data = [
|
||||
'uid' => $this->userId,
|
||||
'uuid' => $this->collectionUuid,
|
||||
'label' => $this->collectionLabel,
|
||||
'description' => $this->collectionDescription,
|
||||
'color' => $this->collectionColor,
|
||||
'created' => $this->collectionCreatedOn,
|
||||
'modified' => $this->collectionModifiedOn,
|
||||
'signature' => $this->collectionSignature,
|
||||
'enabled' => $this->collectionEnabled,
|
||||
];
|
||||
|
||||
// Only include _id if it exists (for updates)
|
||||
if ($this->collectionId !== null) {
|
||||
$data['_id'] = $this->collectionId;
|
||||
}
|
||||
|
||||
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 {
|
||||
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;
|
||||
}
|
||||
|
||||
public function setEnabled(bool $value): self {
|
||||
$this->collectionEnabled = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getPermissions(): array {
|
||||
return [$this->userId => $this->collectionPermissions];
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
164
lib/Providers/Personal/Entity.php
Normal file
164
lib/Providers/Personal/Entity.php
Normal file
@@ -0,0 +1,164 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
namespace KTXM\ChronoProviderLocal\Providers\Personal;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use KTXF\Chrono\Entity\IEntityBase;
|
||||
use KTXF\Chrono\Entity\IEntityMutable;
|
||||
use KTXF\Chrono\Event\EventObject;
|
||||
|
||||
/**
|
||||
* Entity wrapper - contains metadata and EntityData
|
||||
*/
|
||||
class Entity implements IEntityBase, IEntityMutable {
|
||||
|
||||
// Metadata fields (system-managed)
|
||||
private ?string $entityId = null;
|
||||
private ?string $tenantId = null;
|
||||
private ?string $userId = null;
|
||||
private ?string $collectionId = null;
|
||||
private ?string $createdOn = null;
|
||||
private ?string $modifiedOn = null;
|
||||
private ?string $entitySignature = null;
|
||||
|
||||
// Entity display properties
|
||||
private ?int $entityPriority = null;
|
||||
private ?bool $entityVisibility = null;
|
||||
private ?string $entityColor = null;
|
||||
private string|array|null $entityData = null;
|
||||
|
||||
public function jsonSerialize(): mixed {
|
||||
return [
|
||||
self::JSON_PROPERTY_TYPE => self::JSON_TYPE,
|
||||
self::JSON_PROPERTY_IN => $this->collectionId,
|
||||
self::JSON_PROPERTY_ID => $this->entityId,
|
||||
self::JSON_PROPERTY_DATA => $this->entityData,
|
||||
self::JSON_PROPERTY_SIGNATURE => $this->entitySignature,
|
||||
];
|
||||
}
|
||||
|
||||
public function jsonDeserialize(array|string $data): static
|
||||
{
|
||||
if (is_string($data)) {
|
||||
$data = json_decode($data, true);
|
||||
}
|
||||
|
||||
$this->entityId = $data[self::JSON_PROPERTY_ID] ?? null;
|
||||
$this->collectionId = $data[self::JSON_PROPERTY_IN] ?? null;
|
||||
$this->entitySignature = $data[self::JSON_PROPERTY_SIGNATURE] ?? null;
|
||||
$this->entityData = $data[self::JSON_PROPERTY_DATA] ?? null;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function fromStore(array|object $document): self {
|
||||
// Convert object to array if needed
|
||||
if (is_object($document)) {
|
||||
$document = (array) $document;
|
||||
}
|
||||
|
||||
// Load metadata
|
||||
$this->entityId = $document['eid'] ?? null;
|
||||
$this->tenantId = $document['tid'] ?? null;
|
||||
$this->userId = $document['uid'] ?? null;
|
||||
$this->collectionId = $document['cid'] ?? null;
|
||||
$this->createdOn = $document['createdOn'] ?? null;
|
||||
$this->modifiedOn = $document['modifiedOn'] ?? null;
|
||||
$this->entityData = $document['data'] ?? null;
|
||||
$this->entitySignature = md5(json_encode($this->entityData));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function toStore(): array {
|
||||
$document = [
|
||||
'tid' => $this->tenantId,
|
||||
'uid' => $this->userId,
|
||||
'cid' => $this->collectionId,
|
||||
'eid' => $this->entityId,
|
||||
'createdOn' => $this->createdOn ?? date('c'),
|
||||
'modifiedOn' => date('c'),
|
||||
'data' => $this->entityData,
|
||||
];
|
||||
|
||||
return $document;
|
||||
}
|
||||
|
||||
public function in(): string|int {
|
||||
return $this->collectionId ?? '';
|
||||
}
|
||||
|
||||
public function id(): string|int {
|
||||
return $this->entityId ?? '';
|
||||
}
|
||||
|
||||
public function created(): ?DateTimeImmutable {
|
||||
return $this->createdOn ? new DateTimeImmutable($this->createdOn) : null;
|
||||
}
|
||||
|
||||
public function modified(): ?DateTimeImmutable {
|
||||
return $this->modifiedOn ? new DateTimeImmutable($this->modifiedOn) : null;
|
||||
}
|
||||
|
||||
public function signature(): ?string {
|
||||
return $this->entitySignature;
|
||||
}
|
||||
|
||||
public function getPriority(): ?int {
|
||||
return $this->entityPriority;
|
||||
}
|
||||
|
||||
public function setPriority(?int $value): static {
|
||||
$this->entityPriority = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getVisibility(): ?bool {
|
||||
return $this->entityVisibility;
|
||||
}
|
||||
|
||||
public function setVisibility(?bool $value): static {
|
||||
$this->entityVisibility = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getColor(): ?string {
|
||||
return $this->entityColor;
|
||||
}
|
||||
|
||||
public function setColor(?string $value): static {
|
||||
$this->entityColor = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getDataObject(): EventObject|null {
|
||||
return $this->entityData ? (new EventObject)->jsonDeserialize($this->entityData) : null;
|
||||
}
|
||||
|
||||
public function setDataObject(object $value): static
|
||||
{
|
||||
if ($value instanceof EventObject) {
|
||||
$this->entityData = $value->jsonSerialize();
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getDataJson(): array|string|null {
|
||||
return $this->entityData;
|
||||
}
|
||||
|
||||
public function setDataJson(array|string $value): static
|
||||
{
|
||||
$this->entityData = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
386
lib/Providers/Personal/PersonalService.php
Normal file
386
lib/Providers/Personal/PersonalService.php
Normal file
@@ -0,0 +1,386 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
namespace KTXM\ChronoProviderLocal\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\Resource\Exceptions\InvalidParameterException;
|
||||
use KTXF\Resource\Filter\Filter;
|
||||
use KTXF\Resource\Filter\IFilter;
|
||||
use KTXF\Resource\Range\IRange;
|
||||
use KTXF\Resource\Range\RangeDate;
|
||||
use KTXF\Resource\Range\RangeTally;
|
||||
use KTXF\Resource\Range\RangeType;
|
||||
use KTXF\Resource\Sort\ISort;
|
||||
use KTXF\Resource\Sort\Sort;
|
||||
use KTXM\ChronoProviderLocal\Store\Personal\Store;
|
||||
|
||||
class PersonalService implements IServiceBase, IServiceCollectionMutable, IServiceEntityMutable {
|
||||
|
||||
protected const SERVICE_ID = 'personal';
|
||||
protected const SERVICE_LABEL = 'Personal Calendar Service';
|
||||
protected const SERVICE_PROVIDER = 'default';
|
||||
|
||||
protected array $serviceCollectionCache = [];
|
||||
protected ?string $serviceTenantId = null;
|
||||
protected ?string $serviceUserId = null;
|
||||
protected ?bool $serviceEnabled = true;
|
||||
|
||||
protected 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_LIST_SORT => [
|
||||
self::CAPABILITY_SORT_ID,
|
||||
self::CAPABILITY_SORT_URID,
|
||||
self::CAPABILITY_SORT_LABEL,
|
||||
self::CAPABILITY_SORT_PRIORITY
|
||||
],
|
||||
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_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_LIST_SORT => [
|
||||
self::CAPABILITY_SORT_LABEL,
|
||||
],
|
||||
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_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,
|
||||
];
|
||||
|
||||
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 {
|
||||
$this->serviceTenantId = $tenantId;
|
||||
$this->serviceUserId = $userId;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function capable(string $value): bool {
|
||||
if (isset($this->serviceAbilities[$value])) {
|
||||
return (bool)$this->serviceAbilities[$value];
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function capabilities(): array {
|
||||
return $this->serviceAbilities;
|
||||
}
|
||||
|
||||
public function in(): string{
|
||||
return self::SERVICE_PROVIDER;
|
||||
}
|
||||
|
||||
public function id(): string {
|
||||
return self::SERVICE_ID;
|
||||
}
|
||||
|
||||
public function getLabel(): string {
|
||||
return (string)self::SERVICE_LABEL;
|
||||
}
|
||||
|
||||
public function getEnabled(): bool {
|
||||
return (bool)$this->serviceEnabled;
|
||||
}
|
||||
|
||||
public function collectionList(?IFilter $filter = null, ?ISort $sort = null): array {
|
||||
$entries = $this->store->collectionList($this->serviceTenantId, $this->serviceUserId, $filter, $sort);
|
||||
$this->serviceCollectionCache = $entries;
|
||||
return $entries ?? [];
|
||||
}
|
||||
|
||||
public function collectionListFilter(): Filter {
|
||||
return new Filter($this->serviceAbilities[self::CAPABILITY_COLLECTION_LIST_FILTER] ?? []);
|
||||
}
|
||||
|
||||
public function collectionListSort(): Sort {
|
||||
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;
|
||||
}
|
||||
// retrieve from store
|
||||
return $this->store->collectionExtant($this->serviceTenantId, $this->serviceUserId, $id);
|
||||
}
|
||||
|
||||
public function collectionFetch(string|int $id): ?Collection {
|
||||
// determine if collection is cached
|
||||
if (isset($this->serviceCollectionCache[$id])) {
|
||||
return $this->serviceCollectionCache[$id];
|
||||
}
|
||||
// retrieve from store
|
||||
$collection = $this->store->collectionFetch($this->serviceTenantId, $this->serviceUserId, $id);
|
||||
if ($collection !== null) {
|
||||
$this->serviceCollectionCache[$collection->id()] = $collection;
|
||||
return $collection;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public function collectionFresh(): Collection {
|
||||
return new Collection();
|
||||
}
|
||||
|
||||
public function collectionCreate(string|int $location, ICollectionMutable $collection, array $options): Collection {
|
||||
// convert collection to a native type if needed
|
||||
if (!($collection instanceof Collection)) {
|
||||
$nativeCollection = new Collection();
|
||||
$nativeCollection->jsonDeserialize($collection->jsonSerialize());
|
||||
} else {
|
||||
$nativeCollection = clone $collection;
|
||||
}
|
||||
// create collection in store
|
||||
$result = $this->store->collectionCreate($this->serviceTenantId, $this->serviceUserId, $nativeCollection);
|
||||
$this->serviceCollectionCache[$result->id()] = $result;
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function collectionModify(string|int $id, ICollectionMutable $collection): Collection {
|
||||
// validate id
|
||||
if (!is_string($id)) {
|
||||
throw new InvalidParameterException("Invalid: Collection identifier '$id' is not valid");
|
||||
}
|
||||
// validate ownership
|
||||
if ($this->collectionExtant($id) === false) {
|
||||
throw new InvalidParameterException("Invalid: Collection identifier '$id' does not exist or does not belong to user '{$this->serviceUserId}'");
|
||||
}
|
||||
// convert collection to a native type if needed
|
||||
if (!($collection instanceof Collection)) {
|
||||
$nativeCollection = new Collection();
|
||||
$nativeCollection->jsonDeserialize($collection->jsonSerialize());
|
||||
} else {
|
||||
$nativeCollection = clone $collection;
|
||||
}
|
||||
// modify collection in store
|
||||
$result = $this->store->collectionModify($this->serviceTenantId, $this->serviceUserId, $nativeCollection);
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function collectionDestroy(string|int $id): bool {
|
||||
// validate id
|
||||
if (!is_string($id)) {
|
||||
throw new InvalidParameterException("Invalid: Collection identifier '$id' is not valid");
|
||||
}
|
||||
// validate ownership
|
||||
if ($this->collectionExtant($id) === false) {
|
||||
throw new InvalidParameterException("Invalid: Collection identifier '$id' does not exist or does not belong to user '{$this->serviceUserId}'");
|
||||
}
|
||||
// destroy collection in store
|
||||
if ($this->store->collectionDestroyById($this->serviceTenantId, $this->serviceUserId, $id)) {
|
||||
unset($this->serviceCollectionCache[$id]);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function entityList(string|int $collection, ?IFilter $filter = null, ?ISort $sort = null, ?IRange $range = null, ?array $options = null): array {
|
||||
// validate id
|
||||
if (!is_string($collection)) {
|
||||
throw new InvalidParameterException("Invalid: Collection identifier '$collection' is not valid");
|
||||
}
|
||||
// validate ownership
|
||||
if ($this->collectionExtant($collection) === false) {
|
||||
throw new InvalidParameterException("Invalid: Collection identifier '$collection' does not exist or does not belong to user '{$this->serviceUserId}'");
|
||||
}
|
||||
// retrieve entities from store
|
||||
$entries = $this->store->entityList($this->serviceTenantId, $this->serviceUserId, $collection, $filter, $sort, $range, $options);
|
||||
return $entries ?? [];
|
||||
}
|
||||
|
||||
|
||||
public function entityListFilter(): Filter {
|
||||
return new Filter($this->serviceAbilities[self::CAPABILITY_ENTITY_LIST_FILTER] ?? []);
|
||||
}
|
||||
|
||||
public function entityListSort(): Sort {
|
||||
return new Sort($this->serviceAbilities[self::CAPABILITY_ENTITY_LIST_SORT] ?? []);
|
||||
}
|
||||
|
||||
public function entityListRange(RangeType $type): IRange {
|
||||
// validate type
|
||||
if ($type === RangeType::TALLY) {
|
||||
return new RangeTally();
|
||||
}
|
||||
if ($type === RangeType::DATE) {
|
||||
return new RangeDate();
|
||||
}
|
||||
throw new InvalidParameterException("Invalid: Entity range of type '{$type->value}' is not valid");
|
||||
}
|
||||
|
||||
public function entityExtant(string|int $collection, string|int ...$identifiers): array {
|
||||
// validate id
|
||||
if (!is_string($collection)) {
|
||||
throw new InvalidParameterException("Invalid: Collection identifier '$collection' is not valid");
|
||||
}
|
||||
// validate ownership
|
||||
if ($this->collectionExtant($collection) === false) {
|
||||
throw new InvalidParameterException("Invalid: Collection identifier '$collection' does not exist or does not belong to user '{$this->serviceUserId}'");
|
||||
}
|
||||
// retrieve entity status from store
|
||||
return $this->store->entityExtant($collection, ...$identifiers);
|
||||
}
|
||||
|
||||
public function entityDelta(string|int $collection, string $signature, string $detail = 'ids'): array {
|
||||
// validate id
|
||||
if (!is_string($collection)) {
|
||||
throw new InvalidParameterException("Invalid: Collection identifier '$collection' is not valid");
|
||||
}
|
||||
// validate ownership
|
||||
if ($this->collectionExtant($collection) === false) {
|
||||
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);
|
||||
}
|
||||
|
||||
public function entityFetch(string|int $collection, string|int ...$identifiers): array {
|
||||
// validate id
|
||||
if (!is_string($collection)) {
|
||||
throw new InvalidParameterException("Invalid: Collection identifier '$collection' is not valid");
|
||||
}
|
||||
// validate ownership
|
||||
if ($this->collectionExtant($collection) === false) {
|
||||
throw new InvalidParameterException("Invalid: Collection identifier '$collection' does not exist or does not belong to user '{$this->serviceUserId}'");
|
||||
}
|
||||
// retrieve entity from store
|
||||
$entries = $this->store->entityFetch($this->serviceTenantId, $this->serviceUserId, $collection, ...$identifiers);
|
||||
return $entries ?? [];
|
||||
}
|
||||
|
||||
public function entityFresh(): Entity {
|
||||
return new Entity();
|
||||
}
|
||||
|
||||
public function entityCreate(string|int $collection, IEntityMutable $entity, array $options): Entity {
|
||||
// validate collection identifier
|
||||
if (!is_string($collection)) {
|
||||
throw new InvalidParameterException("Invalid: Collection identifier '$collection' is not valid");
|
||||
}
|
||||
// validate collection extant and ownership
|
||||
if ($this->collectionExtant($collection) === false) {
|
||||
throw new InvalidParameterException("Invalid: Collection identifier '$collection' does not exist or does not belong to user '{$this->serviceUserId}'");
|
||||
}
|
||||
// convert entity to a native type if needed
|
||||
if (!($entity instanceof Entity)) {
|
||||
$nativeEntity = $this->entityFresh();
|
||||
$nativeEntity->jsonDeserialize($entity->jsonSerialize());
|
||||
} else {
|
||||
$nativeEntity = clone $entity;
|
||||
}
|
||||
// create entity in store (store will handle userId and collection)
|
||||
$result = $this->store->entityCreate($this->serviceTenantId, $this->serviceUserId, $collection, $nativeEntity);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function entityModify(string|int $collection, string|int $identifier, IEntityMutable $entity): Entity {
|
||||
// validate collection identifier
|
||||
if (!is_string($collection)) {
|
||||
throw new InvalidParameterException("Invalid: Collection identifier '$collection' is not valid");
|
||||
}
|
||||
// validate entity identifier
|
||||
if (!is_string($identifier)) {
|
||||
throw new InvalidParameterException("Invalid: Entity identifier '$identifier' is not valid");
|
||||
}
|
||||
// validate collection extant and ownership
|
||||
if ($this->collectionExtant($collection) === false) {
|
||||
throw new InvalidParameterException("Invalid: Collection identifier '$collection' does not exist or does not belong to user '{$this->serviceUserId}'");
|
||||
}
|
||||
// validate entity extant and ownership
|
||||
$extant = $this->store->entityExtant($this->serviceTenantId, $this->serviceUserId, $collection, $identifier);
|
||||
if (!isset($extant[$identifier]) || $extant[$identifier] === false) {
|
||||
throw new InvalidParameterException("Invalid: Entity identifier '$identifier' does not exist or does not belong to collection '$collection' or user '{$this->serviceUserId}'");
|
||||
}
|
||||
|
||||
// convert entity to a native type if needed
|
||||
if (!($entity instanceof Entity)) {
|
||||
$nativeEntity = $this->entityFresh();
|
||||
$nativeEntity->jsonDeserialize($entity->jsonSerialize());
|
||||
} else {
|
||||
$nativeEntity = clone $entity;
|
||||
}
|
||||
|
||||
// modify entity in store
|
||||
$result = $this->store->entityModify($this->serviceTenantId, $this->serviceUserId, $collection, $identifier, $nativeEntity);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function entityDestroy(string|int $collection, string|int $identifier): IEntityMutable {
|
||||
// validate collection identifier
|
||||
if (!is_string($collection)) {
|
||||
throw new InvalidParameterException("Invalid: Collection identifier '$collection' is not valid");
|
||||
}
|
||||
// validate entity identifier
|
||||
if (!is_string($identifier)) {
|
||||
throw new InvalidParameterException("Invalid: Entity identifier '$identifier' is not valid");
|
||||
}
|
||||
// validate collection extant and ownership
|
||||
if ($this->collectionExtant($collection) === false) {
|
||||
throw new InvalidParameterException("Invalid: Collection identifier '$collection' does not exist or does not belong to user '{$this->serviceUserId}'");
|
||||
}
|
||||
// validate entity extant and ownership
|
||||
$extant = $this->store->entityExtant($this->serviceTenantId, $this->serviceUserId, $collection, $identifier);
|
||||
if (!isset($extant[$identifier]) || $extant[$identifier] === false) {
|
||||
throw new InvalidParameterException("Invalid: Entity identifier '$identifier' does not exist or does not belong to collection '$collection' or user '{$this->serviceUserId}'");
|
||||
}
|
||||
// fetch entity before destruction to return it
|
||||
$entities = $this->store->entityFetch($this->serviceTenantId, $this->serviceUserId, $collection, $identifier);
|
||||
$entity = $entities[$identifier] ?? $this->entityFresh();
|
||||
|
||||
// destroy entity in store
|
||||
$this->store->entityDestroyById($this->serviceTenantId, $this->serviceUserId, $collection, $identifier);
|
||||
|
||||
return $entity;
|
||||
}
|
||||
|
||||
}
|
||||
199
lib/Providers/Provider.php
Normal file
199
lib/Providers/Provider.php
Normal file
@@ -0,0 +1,199 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
namespace KTXM\ChronoProviderLocal\Providers;
|
||||
|
||||
use Psr\Container\ContainerInterface;
|
||||
use KTXF\Chrono\Provider\IProviderBase;
|
||||
use KTXF\Chrono\Service\IServiceBase;
|
||||
use KTXF\Resource\Provider\ProviderInterface;
|
||||
use KTXM\ChronoProviderLocal\Providers\Personal\PersonalService;
|
||||
|
||||
class Provider implements IProviderBase, ProviderInterface {
|
||||
|
||||
protected const PROVIDER_ID = 'default';
|
||||
protected const PROVIDER_LABEL = 'Default Chrono Provider';
|
||||
protected const PROVIDER_DESCRIPTION = 'Provides local calendar/event storage';
|
||||
protected const PROVIDER_ICON = 'calendar';
|
||||
|
||||
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,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 === []) {
|
||||
$filter = ['personal'];
|
||||
}
|
||||
// check if services are cached
|
||||
if (in_array('personal', $filter, true) && !isset($this->servicesCache[$userId]['personal'])) {
|
||||
$this->servicesCache[$userId]['personal'] = $this->serviceInstancePersonal($tenantId, $userId);
|
||||
}
|
||||
// return requested services
|
||||
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 {
|
||||
|
||||
// check if services are cached
|
||||
if (isset($this->servicesCache[$userId][$identifier])) {
|
||||
return $this->servicesCache[$userId][$identifier];
|
||||
}
|
||||
// convert to service object
|
||||
if ($identifier === 'personal') {
|
||||
$this->servicesCache[$userId][$identifier] = $this->serviceInstancePersonal($tenantId, $userId);
|
||||
}
|
||||
|
||||
return $this->servicesCache[$userId][$identifier];
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
728
lib/Store/Personal/Store.php
Normal file
728
lib/Store/Personal/Store.php
Normal file
@@ -0,0 +1,728 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
namespace KTXM\ChronoProviderLocal\Store\Personal;
|
||||
|
||||
use KTXC\Db\DataStore;
|
||||
use KTXF\Resource\Filter\Filter;
|
||||
use KTXF\Resource\Filter\FilterComparisonOperator;
|
||||
use KTXF\Resource\Filter\FilterConjunctionOperator;
|
||||
use KTXF\Resource\Range\Range;
|
||||
use KTXF\Resource\Range\RangeType;
|
||||
use KTXF\Resource\Sort\Sort;
|
||||
use KTXF\Utile\UUID;
|
||||
use KTXM\ChronoProviderLocal\Providers\Personal\Collection;
|
||||
use KTXM\ChronoProviderLocal\Providers\Personal\Entity;
|
||||
|
||||
class Store {
|
||||
|
||||
protected string $_CollectionTable = 'chrono_provider_local_collection';
|
||||
protected string $_CollectionClass = 'KTXM\ChronoProviderLocal\Providers\Personal\Collection';
|
||||
protected string $_EntityTable = 'chrono_provider_local_entity';
|
||||
protected string $_EntityClass = 'KTXM\ChronoProviderLocal\Providers\Personal\Entity';
|
||||
protected string $_ChronicleTable = 'chrono_provider_local_chronicle';
|
||||
|
||||
protected array $_CollectionFilterAttributeMap = [
|
||||
'id' => 'cid',
|
||||
'label' => 'label',
|
||||
'description' => 'description',
|
||||
];
|
||||
|
||||
protected array $_CollectionFilterAttributeComparatorDefault = [
|
||||
'id' => FilterComparisonOperator::IN,
|
||||
'label' => FilterComparisonOperator::LIKE,
|
||||
'description' => FilterComparisonOperator::LIKE,
|
||||
];
|
||||
|
||||
protected array $_EntityFilterAttributeMap = [
|
||||
'id' => 'eid',
|
||||
'label' => 'data.label',
|
||||
'startsOn' => 'data.startsOn',
|
||||
'endsOn' => 'data.endsOn',
|
||||
'locations' => 'data.locations',
|
||||
'participants' => 'data.participants',
|
||||
];
|
||||
|
||||
public function __construct(
|
||||
protected readonly DataStore $_store
|
||||
) { }
|
||||
|
||||
protected function constructFilter(array $map, Filter $filter): array {
|
||||
$mongoFilter = [];
|
||||
|
||||
foreach ($filter->conditions() as $entry) {
|
||||
if (!isset($map[$entry['attribute']])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$attribute = $map[$entry['attribute']];
|
||||
$value = $entry['value'];
|
||||
$comparator = $entry['comparator'] ?? FilterComparisonOperator::EQ;
|
||||
|
||||
$condition = match ($comparator) {
|
||||
FilterComparisonOperator::EQ => $value,
|
||||
FilterComparisonOperator::NEQ => ['$ne' => $value],
|
||||
FilterComparisonOperator::GT => ['$gt' => $value],
|
||||
FilterComparisonOperator::GTE => ['$gte' => $value],
|
||||
FilterComparisonOperator::LT => ['$lt' => $value],
|
||||
FilterComparisonOperator::LTE => ['$lte' => $value],
|
||||
FilterComparisonOperator::IN => ['$in' => is_array($value) ? $value : [$value]],
|
||||
FilterComparisonOperator::NIN => ['$nin' => is_array($value) ? $value : [$value]],
|
||||
FilterComparisonOperator::LIKE => ['$regex' => $value, '$options' => 'i'],
|
||||
FilterComparisonOperator::NLIKE => ['$not' => ['$regex' => $value, '$options' => 'i']],
|
||||
default => $value
|
||||
};
|
||||
|
||||
if (isset($mongoFilter[$attribute])) {
|
||||
// Handle conjunction
|
||||
if ($entry['conjunction'] === FilterConjunctionOperator::OR) {
|
||||
$mongoFilter['$or'][] = [$attribute => $condition];
|
||||
} else {
|
||||
// AND conjunction - merge with existing
|
||||
if (is_array($mongoFilter[$attribute]) && !isset($mongoFilter[$attribute]['$and'])) {
|
||||
$mongoFilter[$attribute] = ['$and' => [$mongoFilter[$attribute], $condition]];
|
||||
} else {
|
||||
$mongoFilter[$attribute] = $condition;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$mongoFilter[$attribute] = $condition;
|
||||
}
|
||||
}
|
||||
|
||||
return $mongoFilter;
|
||||
}
|
||||
|
||||
protected function constructSort(array $map, Sort $sort): array {
|
||||
$mongoSort = [];
|
||||
|
||||
foreach ($sort->conditions() as $entry) {
|
||||
if (!isset($map[$entry['attribute']])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$attribute = $map[$entry['attribute']];
|
||||
$direction = $entry['direction'] ? 1 : -1;
|
||||
$mongoSort[$attribute] = $direction;
|
||||
}
|
||||
|
||||
return $mongoSort;
|
||||
}
|
||||
|
||||
/**
|
||||
* retrieve collections from data store
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
* @param Filter $filter filter options
|
||||
* @param Sort $sort sort options
|
||||
*
|
||||
* @return array<string, Collection>
|
||||
*/
|
||||
public function collectionList(string $tenantId, string $userId, ?Filter $filter = null, ?Sort $sort = null): array {
|
||||
$query = ['tid' => $tenantId, 'uid' => $userId];
|
||||
|
||||
// Apply filter if provided
|
||||
if ($filter !== null) {
|
||||
$filterConditions = $this->constructFilter($this->_CollectionFilterAttributeMap, $filter);
|
||||
$query = array_merge($query, $filterConditions);
|
||||
}
|
||||
|
||||
$options = [];
|
||||
|
||||
// Apply sort if provided
|
||||
if ($sort !== null) {
|
||||
$sortConditions = $this->constructSort($this->_CollectionFilterAttributeMap, $sort);
|
||||
$options['sort'] = $sortConditions;
|
||||
}
|
||||
|
||||
$cursor = $this->_store->selectCollection($this->_CollectionTable)->find($query, $options);
|
||||
$list = [];
|
||||
foreach ($cursor as $entry) {
|
||||
$entry = (new Collection())->fromStore($entry);
|
||||
$list[$entry->id()] = $entry;
|
||||
}
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* confirm if collections exist in data store
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
* @param string $userId user id
|
||||
* @param string $id collection id
|
||||
*
|
||||
*/
|
||||
public function collectionExtant(string $tenantId, string $userId, string $identifier): bool {
|
||||
$cursor = $this->_store->selectCollection($this->_CollectionTable)->findOne([
|
||||
'tid' => $tenantId,
|
||||
'uid' => $userId,
|
||||
'cid' => $identifier
|
||||
]);
|
||||
return $cursor !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* retrieve collection from data store
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
* @param string $userId user identifier
|
||||
* @param string $identifier collection identifier
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function collectionFetch(string $tenantId, string $userId, string $identifier): ?Collection {
|
||||
$cursor = $this->_store->selectCollection($this->_CollectionTable)->findOne([
|
||||
'tid' => $tenantId,
|
||||
'uid' => $userId,
|
||||
'cid' => $identifier
|
||||
]);
|
||||
if ($cursor === null) {
|
||||
return null;
|
||||
}
|
||||
$entry = (new Collection())->fromStore($cursor);
|
||||
return $entry;
|
||||
}
|
||||
|
||||
/**
|
||||
* fresh instance of a collection entity
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function collectionFresh(): Collection {
|
||||
return new $this->_CollectionClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* create a collection entry in the data store
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
* @param string $userId user identifier
|
||||
* @param Collection $entity
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function collectionCreate(string $tenantId, string $userId, Collection $entity): Collection {
|
||||
// convert entity to store format
|
||||
$data = $entity->toStore();
|
||||
// prepare data for creation
|
||||
$data['tid'] = $tenantId;
|
||||
$data['uid'] = $userId;
|
||||
$data['cid'] = UUID::v4();
|
||||
$data['createdOn'] = date('c');
|
||||
$data['modifiedOn'] = $data['createdOn'];
|
||||
// create entry
|
||||
$result = $this->_store->selectCollection($this->_CollectionTable)->insertOne($data);
|
||||
if ($result->getInsertedCount() === 1) {
|
||||
$entity = new Collection();
|
||||
$entity->fromStore($data);
|
||||
}
|
||||
return $entity;
|
||||
}
|
||||
|
||||
/**
|
||||
* modify a collection entry in the data store
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
* @param string $userId user identifier
|
||||
* @param Collection $entity
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function collectionModify(string $tenantId, string $userId, Collection $entity): Collection {
|
||||
// convert entity to store format
|
||||
$data = $entity->toStore();
|
||||
// prepare data for modification
|
||||
$cid = $entity->id();
|
||||
$data['modifiedOn'] = date('c');
|
||||
unset($data['_id'], $data['tid'], $data['uid'], $data['cid']);
|
||||
// modify entry
|
||||
$this->_store->selectCollection($this->_CollectionTable)->updateOne(
|
||||
['tid' => $tenantId, 'uid' => $userId, 'cid' => $cid],
|
||||
['$set' => $data]
|
||||
);
|
||||
return $entity;
|
||||
}
|
||||
|
||||
/**
|
||||
* delete a collection entry from the data store
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
* @param Collection $entity
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function collectionDestroy(string $tenantId, string $userId, Collection $entity): Collection {
|
||||
return $this->collectionDestroyById($tenantId, $userId, $entity->id()) ? $entity : $entity;
|
||||
}
|
||||
|
||||
/**
|
||||
* delete a collection entry from the data store by ID and user
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
* @param string $userId user identifier
|
||||
* @param string $collectionId collection identifier
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function collectionDestroyById(string $tenantId, string $userId, string $collectionId): bool {
|
||||
$result = $this->_store->selectCollection($this->_CollectionTable)->deleteOne([
|
||||
'tid' => $tenantId,
|
||||
'uid' => $userId,
|
||||
'cid' => $collectionId
|
||||
]);
|
||||
|
||||
return $result->getDeletedCount() === 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* retrieve entities from data store
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
* @param string $collection collection identifier
|
||||
* @param Filter $filter filter options
|
||||
* @param Sort $sort sort options
|
||||
* @param Range $range range options
|
||||
*
|
||||
* @return array of entities
|
||||
*/
|
||||
public function entityList(string $tenantId, string $userId, string $collectionId, ?Filter $filter = null, ?Sort $sort = null, ?Range $range = null, ?array $options = null): array {
|
||||
$query = ['tid' => $tenantId, 'uid' => $userId, 'cid' => $collectionId];
|
||||
|
||||
// Apply filter if provided
|
||||
if ($filter !== null) {
|
||||
$filterConditions = $this->constructFilter($this->_EntityFilterAttributeMap, $filter);
|
||||
$query = array_merge($query, $filterConditions);
|
||||
}
|
||||
|
||||
$findOptions = [];
|
||||
|
||||
// Apply sort if provided
|
||||
if ($sort !== null) {
|
||||
$sortConditions = $this->constructSort($this->_EntityFilterAttributeMap, $sort);
|
||||
$findOptions['sort'] = $sortConditions;
|
||||
}
|
||||
|
||||
// Apply range/pagination if provided
|
||||
if ($range !== null) {
|
||||
if ($range->type() === RangeType::TALLY) {
|
||||
// For TALLY ranges, use position (skip) and tally (limit)
|
||||
/** @var \KTXF\Resource\Range\IRangeTally $rangeTally */
|
||||
$rangeTally = $range;
|
||||
$findOptions['skip'] = $rangeTally->getPosition();
|
||||
$findOptions['limit'] = $rangeTally->getTally();
|
||||
} elseif ($range->type() === RangeType::DATE) {
|
||||
// For DATE ranges, filter by date fields
|
||||
/** @var \KTXF\Resource\Range\IRangeDate $rangeDate */
|
||||
$rangeDate = $range;
|
||||
$query['data.startsOn'] = [
|
||||
'\$gte' => $rangeDate->getStart()->format('c'),
|
||||
'\$lte' => $rangeDate->getEnd()->format('c')
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
$cursor = $this->_store->selectCollection($this->_EntityTable)->find($query, $findOptions);
|
||||
$list = [];
|
||||
foreach ($cursor as $entry) {
|
||||
$entity = (new Entity())->fromStore($entry);
|
||||
$list[$entity->id()] = $entity;
|
||||
}
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* confirm if entity(ies) exist in data store
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
* @param string $collection collection identifier
|
||||
* @param string ...$identifiers entity identifiers (eid UUID strings)
|
||||
*
|
||||
* @return array<string,bool>
|
||||
*/
|
||||
public function entityExtant(string $tenantId, string $userId, string $collectionId, string ...$identifiers): array {
|
||||
|
||||
// Query for all entity IDs at once, but only retrieve the eid field (projection)
|
||||
$cursor = $this->_store->selectCollection($this->_EntityTable)->find(
|
||||
[
|
||||
'tid' => $tenantId,
|
||||
'uid' => $userId,
|
||||
'cid' => $collectionId,
|
||||
'eid' => ['$in' => $identifiers]
|
||||
],
|
||||
[
|
||||
'projection' => ['eid' => 1, '_id' => 0]
|
||||
]
|
||||
);
|
||||
|
||||
// Build flat array of found IDs
|
||||
$found = [];
|
||||
foreach ($cursor as $entry) {
|
||||
$found[] = $entry['eid'];
|
||||
}
|
||||
|
||||
$result = array_fill_keys($found, true);
|
||||
$result = array_merge($result, array_fill_keys(array_diff($identifiers, $found), false));
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* retrieve entity(ies) from data store
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
* @param string $collection collection identifier
|
||||
* @param string ...$identifiers entity identifiers (eid UUID strings)
|
||||
*
|
||||
* @return array<Entity>
|
||||
*/
|
||||
public function entityFetch(string $tenantId, string $userId, string $collectionId, string ...$identifiers): array {
|
||||
// Query for entities using eid field
|
||||
$cursor = $this->_store->selectCollection($this->_EntityTable)->find([
|
||||
'tid' => $tenantId,
|
||||
'uid' => $userId,
|
||||
'cid' => $collectionId,
|
||||
'eid' => ['$in' => $identifiers]
|
||||
]);
|
||||
|
||||
$list = [];
|
||||
foreach ($cursor as $entry) {
|
||||
$entity = (new Entity())->fromStore($entry);
|
||||
$list[$entity->id()] = $entity;
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* fresh instance of a entity
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
* @return Entity
|
||||
*/
|
||||
public function entityFresh(): Entity {
|
||||
return new Entity();
|
||||
}
|
||||
|
||||
/**
|
||||
* create a entity entry in the data store
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
* @param Entity $entity entity to create
|
||||
*
|
||||
* @return Entity
|
||||
*/
|
||||
/**
|
||||
* create a entity entry in the data store
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
* @param string $userId user identifier
|
||||
* @param string $collection collection identifier
|
||||
* @param Entity $entity entity to create
|
||||
*
|
||||
* @return Entity
|
||||
*/
|
||||
public function entityCreate(string $tenantId, string $userId, string $collectionId, Entity $entity): Entity {
|
||||
// convert entity to store format
|
||||
$data = $entity->toStore();
|
||||
// assign identifiers and timestamps
|
||||
$data['tid'] = $tenantId;
|
||||
$data['uid'] = $userId;
|
||||
$data['cid'] = $collectionId;
|
||||
$data['eid'] = UUID::v4();
|
||||
$data['createdOn'] = date('c');
|
||||
$data['createdBy'] = $userId;
|
||||
$data['modifiedOn'] = $data['createdOn'];
|
||||
$data['modifiedBy'] = $data['createdBy'];
|
||||
|
||||
$result = $this->_store->selectCollection($this->_EntityTable)->insertOne($data);
|
||||
|
||||
if ($result->getInsertedCount() === 1) {
|
||||
$eid = $data['eid'];
|
||||
$entity->fromStore(['eid' => $eid, 'tid' => $tenantId, 'uid' => $userId, 'cid' => $collectionId]);
|
||||
// Chronicle the creation (operation 1)
|
||||
$this->chronicleDocument($tenantId, $collectionId, $eid, 1);
|
||||
}
|
||||
|
||||
return $entity;
|
||||
}
|
||||
|
||||
/**
|
||||
* modify a entity entry in the data store
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
* @param string $userId user identifier
|
||||
* @param string $collection collection identifier
|
||||
* @param string $identifier entity identifier
|
||||
* @param Entity $entity entity to modify
|
||||
*
|
||||
* @return Entity
|
||||
*/
|
||||
public function entityModify(string $tenantId, string $userId, string $collectionId, string $identifier, Entity $entity): Entity {
|
||||
// convert entity to store format
|
||||
$data = $entity->toStore();
|
||||
$data['modifiedOn'] = date('c');
|
||||
$data['modifiedBy'] = $userId;
|
||||
// Remove identifiers from update data (they shouldn't change)
|
||||
unset($data['_id'], $data['tid'], $data['uid'], $data['cid'], $data['eid']);
|
||||
|
||||
$result = $this->_store->selectCollection($this->_EntityTable)->updateOne(
|
||||
['tid' => $tenantId, 'uid' => $userId, 'cid' => $collectionId, 'eid' => $identifier],
|
||||
['$set' => $data]
|
||||
);
|
||||
|
||||
if ($result->getModifiedCount() > 0) {
|
||||
// Chronicle the modification (operation 2)
|
||||
$this->chronicleDocument($tenantId, $collectionId, $identifier, 2);
|
||||
}
|
||||
|
||||
return $entity;
|
||||
}
|
||||
|
||||
/**
|
||||
* delete a entity from the data store
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
* @param string $userId user identifier
|
||||
* @param string $collection collection identifier
|
||||
* @param Entity $entity entity to delete
|
||||
*
|
||||
* @return Entity
|
||||
*/
|
||||
public function entityDestroy(string $tenantId, string $userId, string $collectionId, Entity $entity): Entity {
|
||||
$identifier = $entity->id();
|
||||
|
||||
$result = $this->_store->selectCollection($this->_EntityTable)->deleteOne([
|
||||
'tid' => $tenantId,
|
||||
'uid' => $userId,
|
||||
'cid' => $collectionId,
|
||||
'eid' => $identifier
|
||||
]);
|
||||
|
||||
if ($result->getDeletedCount() === 1) {
|
||||
// Chronicle the deletion (operation 3)
|
||||
$this->chronicleDocument($tenantId, $collectionId, $identifier, 3);
|
||||
}
|
||||
|
||||
return $entity;
|
||||
}
|
||||
|
||||
/**
|
||||
* delete a entity from the data store by ID
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
* @param string $userId user identifier
|
||||
* @param string $collection collection identifier
|
||||
* @param string $entityId entity identifier
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function entityDestroyById(string $tenantId, string $userId, string $collectionId, string $identifier): bool {
|
||||
$result = $this->_store->selectCollection($this->_EntityTable)->deleteOne([
|
||||
'tid' => $tenantId,
|
||||
'uid' => $userId,
|
||||
'cid' => $collectionId,
|
||||
'eid' => $identifier
|
||||
]);
|
||||
|
||||
if ($result->getDeletedCount() === 1) {
|
||||
// Chronicle the deletion (operation 3)
|
||||
$this->chronicleDocument($tenantId, $collectionId, $identifier, 3);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* chronicle a operation to an entity to the data store
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
* @param string $tid tenant identifier
|
||||
* @param string $cid collection identifier
|
||||
* @param string $eid entity identifier
|
||||
* @param int $operation operation type (1 - Created, 2 - Modified, 3 - Deleted)
|
||||
*/
|
||||
private function chronicleDocument(string $tid, string $cid, string $eid, int $operation): void {
|
||||
// retrieve current token from collection
|
||||
$collection = $this->_store->selectCollection($this->_CollectionTable)->findOne([
|
||||
'cid' => $cid
|
||||
], [
|
||||
'projection' => ['signature' => 1, '_id' => 0]
|
||||
]);
|
||||
|
||||
$signature = $collection['signature'] ?? 0;
|
||||
|
||||
// document operation in chronicle
|
||||
$this->_store->selectCollection($this->_ChronicleTable)->insertOne([
|
||||
'tid' => $tid,
|
||||
'cid' => $cid,
|
||||
'eid' => $eid,
|
||||
'operation' => $operation,
|
||||
'signature' => $signature,
|
||||
'mutatedOn' => time(),
|
||||
]);
|
||||
|
||||
// increment token atomically
|
||||
$this->_store->selectCollection($this->_CollectionTable)->updateOne(
|
||||
['cid' => $cid],
|
||||
['$inc' => ['signature' => 1]]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* reminisce operations to entities in data store
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
* @param string $cid collection id
|
||||
* @param bool $encode weather to encode the result
|
||||
*
|
||||
* @return int|float|string
|
||||
*/
|
||||
public function chronicleApex(string $tid, string $cid, bool $encode = true): int|float|string {
|
||||
|
||||
// Use aggregation pipeline to find max signature
|
||||
$cursor = $this->_store->selectCollection($this->_ChronicleTable)->aggregate([
|
||||
[
|
||||
'$match' => ['tid' => $tid, 'cid' => $cid]
|
||||
],
|
||||
[
|
||||
'$group' => [
|
||||
'_id' => null,
|
||||
'maxToken' => ['$max' => '$signature']
|
||||
]
|
||||
]
|
||||
]);
|
||||
|
||||
$result = $cursor->toArray();
|
||||
$stampApex = !empty($result) ? ($result[0]['maxToken'] ?? 0) : 0;
|
||||
|
||||
if ($encode) {
|
||||
return base64_encode((string)max(0, $stampApex));
|
||||
} else {
|
||||
return max(0, $stampApex);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* reminisce operations to entities in data store
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
* @param string $collection collection id
|
||||
* @param string $signature encoded token
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function chronicleReminisce(string $tenantId, string $collectionId, string $signature): array {
|
||||
|
||||
// retrieve apex signature
|
||||
$tokenApex = $this->chronicleApex($tenantId, $collectionId, false);
|
||||
// determine nadir signature
|
||||
$tokenNadir = !empty($signature) ? base64_decode($signature) : '';
|
||||
$initial = !is_numeric($tokenNadir);
|
||||
$tokenNadir = $initial ? 0 : (int)$tokenNadir;
|
||||
|
||||
// Build aggregation pipeline to retrieve additions/modifications/deletions
|
||||
$matchStage = [
|
||||
'$match' => [
|
||||
'tid' => $tenantId,
|
||||
'cid' => $collectionId
|
||||
]
|
||||
];
|
||||
|
||||
// If not initial sync, filter by signature range
|
||||
if (!$initial) {
|
||||
$matchStage['$match']['signature'] = [
|
||||
'$gt' => $tokenNadir,
|
||||
'$lte' => (int)$tokenApex
|
||||
];
|
||||
}
|
||||
|
||||
$pipeline = [
|
||||
$matchStage,
|
||||
[
|
||||
'$group' => [
|
||||
'_id' => '$eid',
|
||||
'operation' => ['$max' => '$operation'],
|
||||
'eid' => ['$first' => '$eid']
|
||||
]
|
||||
]
|
||||
];
|
||||
|
||||
// For initial sync, exclude deleted entries
|
||||
if ($initial) {
|
||||
$pipeline[] = [
|
||||
'$match' => [
|
||||
'operation' => ['$ne' => 3]
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
// define place holder
|
||||
$chronicle = ['additions' => [], 'modifications' => [], 'deletions' => [], 'signature' => base64_encode((string)$tokenApex)];
|
||||
|
||||
// execute aggregation
|
||||
$cursor = $this->_store->selectCollection($this->_ChronicleTable)->aggregate($pipeline);
|
||||
|
||||
// process result
|
||||
foreach ($cursor as $entry) {
|
||||
switch ($entry['operation']) {
|
||||
case 1:
|
||||
$chronicle['additions'][] = $entry['eid'];
|
||||
break;
|
||||
case 2:
|
||||
$chronicle['modifications'][] = $entry['eid'];
|
||||
break;
|
||||
case 3:
|
||||
$chronicle['deletions'][] = $entry['eid'];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// return chronicle
|
||||
return $chronicle;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* delete chronicle entries for a specific collection(s) from data store
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
* @param array $identifiers collection of identifiers
|
||||
*/
|
||||
private function chronicleExpungeByCollectionId(array $identifiers): void {
|
||||
// Delete chronicle entries for the specified collection identifiers
|
||||
$this->_store->selectCollection($this->_ChronicleTable)->deleteMany([
|
||||
'cid' => ['$in' => $identifiers]
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user