chore: standardize chrono provider

Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
2026-02-17 03:11:54 -05:00
parent f3d2e2690b
commit 5c6d508cac
7 changed files with 451 additions and 529 deletions

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");