refactor: standardize design

Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
2026-02-25 15:33:59 -05:00
parent 149c342d34
commit 7e743542d0
6 changed files with 56 additions and 77 deletions

View File

@@ -156,8 +156,7 @@ class PersonalService implements ServiceBaseInterface, ServiceCollectionMutableI
// Collection operations
public function collectionList(string|int|null $location, ?IFilter $filter = null, ?ISort $sort = null): array
{
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 ?? [];
@@ -212,14 +211,14 @@ class PersonalService implements ServiceBaseInterface, ServiceCollectionMutableI
return null;
}
public function collectionFresh(): Collection {
return new Collection();
public function collectionFresh(): CollectionResource {
return new CollectionResource();
}
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();
if (!($collection instanceof CollectionResource)) {
$nativeCollection = new CollectionResource();
$nativeCollection->jsonDeserialize($collection->jsonSerialize());
} else {
$nativeCollection = clone $collection;
@@ -244,16 +243,16 @@ class PersonalService implements ServiceBaseInterface, ServiceCollectionMutableI
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();
if (!($collection instanceof CollectionResource)) {
$nativeCollection = new CollectionResource();
$data = $collection->jsonSerialize();
$data[Collection::JSON_PROPERTY_IDENTIFIER] = $id;
$data[CollectionResource::JSON_PROPERTY_IDENTIFIER] = $id;
$nativeCollection->jsonDeserialize($data);
} else {
$nativeCollection = clone $collection;
if ($nativeCollection->identifier() === null) {
$data = $nativeCollection->jsonSerialize();
$data[Collection::JSON_PROPERTY_IDENTIFIER] = $id;
$data[CollectionResource::JSON_PROPERTY_IDENTIFIER] = $id;
$nativeCollection->jsonDeserialize($data);
}
}
@@ -279,6 +278,8 @@ class PersonalService implements ServiceBaseInterface, ServiceCollectionMutableI
return false;
}
// Entity operations
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)) {
@@ -293,7 +294,6 @@ class PersonalService implements ServiceBaseInterface, ServiceCollectionMutableI
return $entries ?? [];
}
public function entityListFilter(): Filter {
return new Filter($this->serviceAbilities[self::CAPABILITY_ENTITY_LIST_FILTER] ?? []);
}
@@ -360,11 +360,11 @@ class PersonalService implements ServiceBaseInterface, ServiceCollectionMutableI
return $entries ?? [];
}
public function entityFresh(): Entity {
return new Entity();
public function entityFresh(): EntityResource {
return new EntityResource();
}
public function entityCreate(string|int $collection, EntityMutableInterface $entity, array $options = []): Entity {
public function entityCreate(string|int $collection, EntityMutableInterface $entity, array $options = []): EntityResource {
// validate collection identifier
if (!is_string($collection)) {
throw new InvalidParameterException("Invalid: Collection identifier '$collection' is not valid");
@@ -374,7 +374,7 @@ class PersonalService implements ServiceBaseInterface, ServiceCollectionMutableI
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)) {
if (!($entity instanceof EntityResource)) {
$nativeEntity = $this->entityFresh();
$nativeEntity->jsonDeserialize($entity->jsonSerialize());
} else {
@@ -386,7 +386,7 @@ class PersonalService implements ServiceBaseInterface, ServiceCollectionMutableI
return $result;
}
public function entityUpdate(string|int $collection, string|int $identifier, EntityMutableInterface $entity): Entity {
public function entityUpdate(string|int $collection, string|int $identifier, EntityMutableInterface $entity): EntityResource {
// validate collection identifier
if (!is_string($collection)) {
throw new InvalidParameterException("Invalid: Collection identifier '$collection' is not valid");
@@ -406,7 +406,7 @@ class PersonalService implements ServiceBaseInterface, ServiceCollectionMutableI
}
// convert entity to a native type if needed
if (!($entity instanceof Entity)) {
if (!($entity instanceof EntityResource)) {
$nativeEntity = $this->entityFresh();
$nativeEntity->jsonDeserialize($entity->jsonSerialize());
} else {
@@ -419,7 +419,7 @@ class PersonalService implements ServiceBaseInterface, ServiceCollectionMutableI
return $result;
}
public function entityDelete(string|int $collection, string|int $identifier): EntityMutableInterface {
public function entityDelete(string|int $collection, string|int $identifier): EntityResource {
// validate collection identifier
if (!is_string($collection)) {
throw new InvalidParameterException("Invalid: Collection identifier '$collection' is not valid");