diff --git a/src/ApiHandlerInterface.php b/src/ApiHandlerInterface.php index d3bd9a9..cc54d09 100644 --- a/src/ApiHandlerInterface.php +++ b/src/ApiHandlerInterface.php @@ -10,4 +10,5 @@ interface ApiHandlerInterface { public function handleRequest(RequestInterface $request, ApiAction $attribute) : RequestInterface; public function handleResponse(ResponseInterface $response, ApiAction $attribute) : ResponseInterface; + # public function debugResponse(\Ulmus\Api\Transport\CurlTransport $transport, ApiRepository $repository) : void; } \ No newline at end of file diff --git a/src/ApiRepository.php b/src/ApiRepository.php index ee52b0a..527f541 100644 --- a/src/ApiRepository.php +++ b/src/ApiRepository.php @@ -6,6 +6,7 @@ use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\UriInterface; +use Ulmus\Api\Attribute\Property\Bind; use Ulmus\Api\Common\MethodEnum; use Ulmus\Api\RequestBuilder\Filter; use Ulmus\Api\RequestBuilder\UrlParameter; @@ -60,7 +61,13 @@ class ApiRepository extends \Ulmus\Repository return $this->loadAllFromAttribute(Attribute\Obj\Api\Search::class); } + #[\Deprecated] public function save(object|array $entity, ?array $fieldsAndValue = null, bool $replace = false): bool + { + return $this->create($entity, $replace); + } + + public function create(object|array $entity, bool $replace = false): bool { $response = $this->executeRequest(Attribute\Obj\Api\Create::class, $entity->entityGetDataset()); @@ -71,6 +78,17 @@ class ApiRepository extends \Ulmus\Repository return $response->getStatusCode() === 200; } + public function update(object|array $entity): bool + { + foreach($this->entityResolver->searchPropertyAttributeList(Bind::class) as $field => $attribute) { + $this->bindUrl($attribute[0]->name ?? $field, $entity->$field); + } + + $response = $this->executeRequest(Attribute\Obj\Api\Update::class, $this->generateWritableDataset($entity)); + + return $response->getStatusCode() === 200; + } + public function destroy(object $entity) : bool { if ( ! $this->matchEntity($entity) ) { @@ -135,6 +153,7 @@ class ApiRepository extends \Ulmus\Repository $search = []; foreach($matches as $item) { + unset($value); $default = null; $variable = $item[1]; @@ -153,8 +172,8 @@ class ApiRepository extends \Ulmus\Repository if ($default ?? false) { $value = $default; } - elseif ( strpos($route, "[{$matches[0][0]}]") !== false && $this->enforceExistingArguments) { - throw new \RuntimeException(sprintf("Error while preparing route %s : could not match variable '%s' into given arguments ( %s ) from %s::%s", $route, $variable, json_encode($arguments), $routeParam['class'], $routeParam['classMethod'])); + else { + throw new \RuntimeException(sprintf("Error while preparing route %s : could not match variable '%s' into given arguments %s", $route, $variable, json_encode($arguments))); } } @@ -162,7 +181,7 @@ class ApiRepository extends \Ulmus\Repository $value = $value->value; } - $search[$item[0]] = rawurlencode($value ?? ""); + $search[$item[0]] = rawurlencode($value); } $route = str_replace(array_keys($search), array_values($search), $route); diff --git a/src/Attribute/Property/Bind.php b/src/Attribute/Property/Bind.php new file mode 100644 index 0000000..93143a8 --- /dev/null +++ b/src/Attribute/Property/Bind.php @@ -0,0 +1,10 @@ +setQuery($query); }