diff --git a/src/ApiRepository.php b/src/ApiRepository.php index 2258caf..7447352 100644 --- a/src/ApiRepository.php +++ b/src/ApiRepository.php @@ -6,7 +6,6 @@ use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\UriInterface; -use Ulmus\Api\Attribute\Obj\Api\ApiAction; use Ulmus\Api\Common\MethodEnum; use Ulmus\Api\RequestBuilder\Filter; use Ulmus\Api\RequestBuilder\UrlParameter; @@ -16,6 +15,7 @@ use Ulmus\Api\Stream\Stream; use Ulmus\Api\Request\JsonRequest; use Ulmus\Api\Transport\CurlClient; use Ulmus\Api\Transport\CurlTransport; +use Ulmus\Entity\EntityInterface; use Ulmus\EntityCollection; use Ulmus\SearchRequest\SearchRequestInterface; use Ulmus\Ulmus; @@ -66,7 +66,24 @@ class ApiRepository extends \Ulmus\Repository { $response = $this->executeRequest(Attribute\Obj\Api\Create::class, $entity->entityGetDataset()); - $entity->fromArray($response->getParsedBody()); + if ($replace && $entity instanceof EntityInterface) { + $entity->fromArray($response->getParsedBody()); + } + + return $response->getStatusCode() === 200; + } + + public function destroy(object $entity) : bool + { + if ( ! $this->matchEntity($entity) ) { + throw new \Exception("Your entity class `" . get_class($entity) . "` cannot match entity type of repository `{$this->entityClass}`"); + } + + $this->wherePrimaryKeyValue($entity); + + $this->eventExecute(\Ulmus\Event\Query\Delete::class, $this, $entity); + + $response = $this->executeRequest(Attribute\Obj\Api\Delete::class); return $response->getStatusCode() === 200; } @@ -195,12 +212,12 @@ class ApiRepository extends \Ulmus\Repository return $this->entityClass::resolveEntity()->getAttributeImplementing($type); } - protected function callApiRequestCallback(RequestInterface $request, ApiAction $attribute) : ServerRequestInterface + protected function callApiRequestCallback(RequestInterface $request, Attribute\Obj\Api\ApiAction $attribute) : ServerRequestInterface { return $this->adapter->apiHandler->handleRequest($request, $attribute); } - protected function callApiResponseCallback(ResponseInterface $response, ApiAction $attribute) : ResponseInterface + protected function callApiResponseCallback(ResponseInterface $response, Attribute\Obj\Api\ApiAction $attribute) : ResponseInterface { return $this->adapter->apiHandler->handleResponse($response, $attribute); } @@ -211,26 +228,6 @@ class ApiRepository extends \Ulmus\Repository $this->adapter->apiHandler->debugResponse($transport, $this); } } -/* - public function collectionFromQuery(? string $entityClass = null) : EntityCollection - { - $entityClass ??= $this->entityClass; - - $entityCollection = $entityClass::entityCollection(); - - $this->finalizeQuery(); - - foreach(Ulmus::iterateQueryBuilder($this->queryBuilder, $this->adapter) as $entityData) { - $entity = $this->instanciateEntity($entityClass); - $entity->loadedFromAdapter = $this->adapter->name; - - $entityCollection->append( $entity->resetVirtualProperties()->entityFillFromDataset($entityData) ); - } - - $this->eventExecute(\Ulmus\Event\Repository\CollectionFromQueryInterface::class, $this, $entityCollection); - - return $entityCollection; - }*/ public function filterServerRequest(SearchRequestInterface $searchRequest, bool $count = true) : \Ulmus\Repository { @@ -261,4 +258,36 @@ class ApiRepository extends \Ulmus\Repository return $this; } + + public function wherePrimaryKey(mixed $value, null|string|bool $alias = self::DEFAULT_ALIAS) : self + { + $primaryKeyField = Ulmus::resolveEntity($this->entityClass)->getPrimaryKeyField(); + + if ( $primaryKeyField === null ) { + throw new \Ulmus\Exception\EntityPrimaryKeyUnknown(sprintf("No primary key found for entity %s", $this->entityClass)); + } + + $pkField = key($primaryKeyField); + + return $this->where($pkField, $value); + } + + public function wherePrimaryKeyValue(EntityInterface $entity) : self + { + $primaryKeyField = Ulmus::resolveEntity($this->entityClass)->getPrimaryKeyField(); + + if ( $primaryKeyField === null ) { + throw new \Ulmus\Exception\EntityPrimaryKeyUnknown(sprintf("No primary key found for entity %s", $this->entityClass)); + } + + $pkField = key($primaryKeyField); + + $value = $entity->$pkField; + + if ( $value !== 0 && empty($value) ) { + throw new \Ulmus\Exception\EntityPrimaryKeyUnknown(sprintf("A primary key value has to be defined to delete an item of entity %s.", $entity::class)); + } + + return $this->where($pkField, $value); + } } \ No newline at end of file diff --git a/src/Attribute/Obj/Api/Create.php b/src/Attribute/Obj/Api/Create.php index d5392a5..f05d280 100644 --- a/src/Attribute/Obj/Api/Create.php +++ b/src/Attribute/Obj/Api/Create.php @@ -11,5 +11,6 @@ class Create extends ApiAction public string $url, public MethodEnum $method = MethodEnum::Post, public int $timeout = 30, + public MethodEnum $searchMethod = MethodEnum::Get, ) {} } \ No newline at end of file diff --git a/src/Attribute/Obj/Api/Delete.php b/src/Attribute/Obj/Api/Delete.php index 9d87c50..b21fc28 100644 --- a/src/Attribute/Obj/Api/Delete.php +++ b/src/Attribute/Obj/Api/Delete.php @@ -11,5 +11,6 @@ class Delete extends ApiAction public string $url, public MethodEnum $method = MethodEnum::Delete, public int $timeout = 30, + public MethodEnum $searchMethod = MethodEnum::Get, ) {} } \ No newline at end of file diff --git a/src/Attribute/Obj/Api/Update.php b/src/Attribute/Obj/Api/Update.php index 4f1ed1a..22e03c3 100644 --- a/src/Attribute/Obj/Api/Update.php +++ b/src/Attribute/Obj/Api/Update.php @@ -11,5 +11,6 @@ class Update extends ApiAction public string $url, public MethodEnum $method = MethodEnum::Patch, public int $timeout = 30, + public MethodEnum $searchMethod = MethodEnum::Get, ) {} } \ No newline at end of file diff --git a/src/ConnectionAdapter.php b/src/ConnectionAdapter.php index c51c23f..9f3fb15 100644 --- a/src/ConnectionAdapter.php +++ b/src/ConnectionAdapter.php @@ -68,18 +68,12 @@ class ConnectionAdapter extends \Ulmus\ConnectionAdapter protected function instanciateAdapter($name): AdapterInterface { - $class = substr($name, 0, 2) === "\\" ? $name : "\\Ulmus\\Api\\Adapter\\$name"; + if (class_exists($name)) { + return new $name(); + } + + $class = sprintf("\\%s\\Adapter\\%s", __NAMESPACE__, $name); return new $class(); } - - #public function apiResponseCallback(callable $callback = null) : callable|null - #{ - # return $callback ? $this->apiResponseCallback = $callback : $this->apiResponseCallback ?? fn(ResponseInterface $response) => $response; - #} - - #public function apiRequestCallback(callable $callback = null) : callable|null - #{ - # return $callback ? $this->apiRequestCallback = $callback : $this->apiRequestCallback ?? fn(RequestInterface $request) => $request; - #} }