5 Commits
7 changed files with 60 additions and 26 deletions

No files matched your search

+1
View File
@@ -10,4 +10,5 @@ interface ApiHandlerInterface
{ {
public function handleRequest(RequestInterface $request, ApiAction $attribute) : RequestInterface; public function handleRequest(RequestInterface $request, ApiAction $attribute) : RequestInterface;
public function handleResponse(ResponseInterface $response, ApiAction $attribute) : ResponseInterface; public function handleResponse(ResponseInterface $response, ApiAction $attribute) : ResponseInterface;
# public function debugResponse(\Ulmus\Api\Transport\CurlTransport $transport, ApiRepository $repository) : void;
} }
+26 -3
View File
@@ -6,6 +6,7 @@ use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\UriInterface; use Psr\Http\Message\UriInterface;
use Ulmus\Api\Attribute\Property\Bind;
use Ulmus\Api\Common\MethodEnum; use Ulmus\Api\Common\MethodEnum;
use Ulmus\Api\RequestBuilder\Filter; use Ulmus\Api\RequestBuilder\Filter;
use Ulmus\Api\RequestBuilder\UrlParameter; use Ulmus\Api\RequestBuilder\UrlParameter;
@@ -60,7 +61,13 @@ class ApiRepository extends \Ulmus\Repository
return $this->loadAllFromAttribute(Attribute\Obj\Api\Search::class); return $this->loadAllFromAttribute(Attribute\Obj\Api\Search::class);
} }
#[\Deprecated]
public function save(object|array $entity, ?array $fieldsAndValue = null, bool $replace = false): bool 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()); $response = $this->executeRequest(Attribute\Obj\Api\Create::class, $entity->entityGetDataset());
@@ -71,6 +78,17 @@ class ApiRepository extends \Ulmus\Repository
return $response->getStatusCode() === 200; 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 public function destroy(object $entity) : bool
{ {
if ( ! $this->matchEntity($entity) ) { if ( ! $this->matchEntity($entity) ) {
@@ -135,6 +153,7 @@ class ApiRepository extends \Ulmus\Repository
$search = []; $search = [];
foreach($matches as $item) { foreach($matches as $item) {
unset($value);
$default = null; $default = null;
$variable = $item[1]; $variable = $item[1];
@@ -153,12 +172,16 @@ class ApiRepository extends \Ulmus\Repository
if ($default ?? false) { if ($default ?? false) {
$value = $default; $value = $default;
} }
elseif ( strpos($route, "[{$matches[0][0]}]") !== false && $this->enforceExistingArguments) { else {
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'])); throw new \RuntimeException(sprintf("Error while preparing route %s : could not match variable '%s' into given arguments %s", $route, $variable, json_encode($arguments)));
} }
} }
$search[$item[0]] = rawurlencode($value ?? ""); if (is_object($value) && enum_exists($value::class)) {
$value = $value->value;
}
$search[$item[0]] = rawurlencode($value);
} }
$route = str_replace(array_keys($search), array_values($search), $route); $route = str_replace(array_keys($search), array_values($search), $route);
+1 -1
View File
@@ -5,7 +5,7 @@ namespace Ulmus\Api\Attribute\Obj\Api;
use Ulmus\Api\Common\MethodEnum; use Ulmus\Api\Common\MethodEnum;
#[\Attribute] #[\Attribute]
abstract class ApiAction class ApiAction
{ {
public function __construct( public function __construct(
public string $url, public string $url,
+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,
) {}
}
+14 -14
View File
@@ -53,7 +53,7 @@ class Uri implements UriInterface
$this->setFragment($parts['fragment'] ?? null); $this->setFragment($parts['fragment'] ?? null);
} }
public function getScheme() public function getScheme(): string
{ {
return $this->scheme; return $this->scheme;
} }
@@ -94,7 +94,7 @@ class Uri implements UriInterface
return ( $info = $this->getUserInfo() ) ? "{$info}@" : ""; return ( $info = $this->getUserInfo() ) ? "{$info}@" : "";
} }
public function getHost() public function getHost(): string
{ {
return $this->host; return $this->host;
} }
@@ -106,7 +106,7 @@ class Uri implements UriInterface
return $this; return $this;
} }
public function getPort() public function getPort(): ?int
{ {
return $this->port; return $this->port;
} }
@@ -118,7 +118,7 @@ class Uri implements UriInterface
return $port && ! in_array($port, $ignore) ? ":{$port}" : ""; return $port && ! in_array($port, $ignore) ? ":{$port}" : "";
} }
public function getPath() public function getPath(): string
{ {
$path = implode('/', array_map('rawurlencode', explode('/', $this->path))); $path = implode('/', array_map('rawurlencode', explode('/', $this->path)));
@@ -130,7 +130,7 @@ class Uri implements UriInterface
return $this->getPath(); return $this->getPath();
} }
public function getQuery() public function getQuery(): string
{ {
return http_build_query($this->query, "", null, PHP_QUERY_RFC3986); return http_build_query($this->query, "", null, PHP_QUERY_RFC3986);
} }
@@ -142,7 +142,7 @@ class Uri implements UriInterface
return $query ? "?{$query}" : ""; return $query ? "?{$query}" : "";
} }
public function getFragment() public function getFragment() : string
{ {
return rawurlencode($this->fragment ? "#{$this->fragment}" : ""); return rawurlencode($this->fragment ? "#{$this->fragment}" : "");
} }
@@ -152,12 +152,12 @@ class Uri implements UriInterface
return $this->getFragment(); return $this->getFragment();
} }
public function withScheme($scheme) public function withScheme($scheme) : UriInterface
{ {
return (clone $this)->setScheme($scheme); return (clone $this)->setScheme($scheme);
} }
public function withUserInfo($user, $password = null) public function withUserInfo($user, $password = null) : UriInterface
{ {
return (clone $this)->setUserInfo($user, $password); return (clone $this)->setUserInfo($user, $password);
} }
@@ -170,12 +170,12 @@ class Uri implements UriInterface
return $this; return $this;
} }
public function withHost($host) public function withHost($host) : UriInterface
{ {
return (clone $this)->setHost($host); return (clone $this)->setHost($host);
} }
public function withPort($port) public function withPort($port) : UriInterface
{ {
return (clone $this)->setPort($port); return (clone $this)->setPort($port);
} }
@@ -187,7 +187,7 @@ class Uri implements UriInterface
return $this; return $this;
} }
public function withPath($path) public function withPath($path) : UriInterface
{ {
return (clone $this)->setPath($path); return (clone $this)->setPath($path);
} }
@@ -199,7 +199,7 @@ class Uri implements UriInterface
return $this; return $this;
} }
public function withQuery($query) public function withQuery(null|array|string $query) : UriInterface
{ {
return $this->setQuery($query); return $this->setQuery($query);
} }
@@ -215,7 +215,7 @@ class Uri implements UriInterface
return $this; return $this;
} }
public function withFragment($fragment) public function withFragment($fragment) : UriInterface
{ {
return (clone $this)->setFragment($fragment); return (clone $this)->setFragment($fragment);
} }
@@ -227,7 +227,7 @@ class Uri implements UriInterface
return $this; return $this;
} }
public function __toString() public function __toString() : string
{ {
return $this->render(); return $this->render();
} }
+5 -5
View File
@@ -45,7 +45,7 @@ class Request extends Message implements ServerRequestInterface
return $target ?: '/'; return $target ?: '/';
} }
public function withRequestTarget($requestTarget) public function withRequestTarget($requestTarget) : RequestInterface
{ {
return (clone $this)->setRequestTarget($requestTarget); return (clone $this)->setRequestTarget($requestTarget);
} }
@@ -57,12 +57,12 @@ class Request extends Message implements ServerRequestInterface
return $this; return $this;
} }
public function getMethod() public function getMethod(): string
{ {
return $this->method->value; return $this->method->value;
} }
public function withMethod($method) public function withMethod($method) : RequestInterface
{ {
return (clone $this)->setMethod($method); return (clone $this)->setMethod($method);
} }
@@ -79,7 +79,7 @@ class Request extends Message implements ServerRequestInterface
return $this->uri; return $this->uri;
} }
public function withUri(UriInterface $uri, $preserveHost = false) public function withUri(UriInterface $uri, $preserveHost = false) : RequestInterface
{ {
return (clone $this)->setUri($uri, $preserveHost); return (clone $this)->setUri($uri, $preserveHost);
} }
@@ -95,7 +95,7 @@ class Request extends Message implements ServerRequestInterface
return $this; return $this;
} }
protected function renderHostHeader(UriInterface $fromUri = null) : string protected function renderHostHeader(? UriInterface $fromUri = null) : string
{ {
$fromUri ??= $this->uri; $fromUri ??= $this->uri;
+3 -3
View File
@@ -20,17 +20,17 @@ class Response extends Message implements ResponseInterface
$this->setHeaders($headers); $this->setHeaders($headers);
} }
public function getStatusCode() /* : int */ public function getStatusCode() : int
{ {
return $this->code; return $this->code;
} }
public function withStatus($code, $reasonPhrase = null)/* : ResponseInterface*/ public function withStatus($code, $reasonPhrase = null) : ResponseInterface
{ {
return (clone $this)->setStatusCode($code, $reasonPhrase); return (clone $this)->setStatusCode($code, $reasonPhrase);
} }
public function getReasonPhrase() /* : string */ public function getReasonPhrase() : string
{ {
return $this->message; return $this->message;
} }