- Added a new update() method to ApiRepository, added a new Bind property too in attributes

This commit is contained in:
Dave Mc Nicoll committed 2026-11-26 18:35:33 +00:00
1 parent 99c0148a20
commit 9711e44feb
4 files changed
+34 -4

No files matched your search

+1
View File
@@ -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;
}
+22 -3
View File
@@ -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);
+10
View File
@@ -0,0 +1,10 @@
<?php
namespace Ulmus\Api\Attribute\Property;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Bind {
public function __construct(
public ? string $name = null,
) {}
}
+1 -1
View File
@@ -199,7 +199,7 @@ class Uri implements UriInterface
return $this;
}
public function withQuery($query) : UriInterface
public function withQuery(null|array|string $query) : UriInterface
{
return $this->setQuery($query);
}