- First working version of ulmus-api package; at working state only

This commit is contained in:
mcndave committed 2023-10-26 17:22:52 +00:00
1 parent 0f9495e07a
commit 7e69a09e7f
27 files changed
+1406 -101

No files matched your search

+124
View File
@@ -0,0 +1,124 @@
<?php
namespace Ulmus\Api;
use Psr\Http\Message\ResponseInterface;
use Ulmus\Api\Attribute\Obj\Api\ApiAction;
use Ulmus\Api\Transport\CurlClient;
use Ulmus\EntityCollection;
class ApiRepository extends \Ulmus\Repository
{
const DEFAULT_ALIAS = "";
public CurlClient $client;
protected array $urlParameters = [];
public ResponseInterface $lastResponse;
public function __construct(string $entity, string $alias = self::DEFAULT_ALIAS, ConnectionAdapter $adapter = null)
{
parent::__construct($entity, $alias, $adapter);
$this->client = $adapter->adapter()->connect();
}
public function loadOne(): ?object
{
# Must manager URL here !
$attribute = $this->getApiAttribute(Attribute\Obj\Api\Read::class);
$this->lastResponse = $response = $this->client->request($attribute->method, $this->buildRequestUrl($attribute->url));
$dataset = $this->callApiResponseCallback($response, $attribute);
return $this->instanciateEntity()->fromArray($dataset);
}
public function loadAll() : EntityCollection
{
$attribute = $this->getApiAttribute(Attribute\Obj\Api\Collection::class);
$this->lastResponse = $response = $this->client->request($attribute->method, $this->buildRequestUrl($attribute->url));
$collection = $this->callApiResponseCallback($response, $attribute);
return $this->instanciateEntityCollection($collection);
}
public function save(object|array $entity, ?array $fieldsAndValue = null, bool $replace = false): bool
{
$attribute = $this->getApiAttribute(Attribute\Obj\Api\Create::class);
$this->lastResponse = $response = $this->client->request($attribute->method, $this->buildRequestUrl($attribute->url), $entity->entityLoadedDataset);
$ret = $this->callApiResponseCallback($response, $attribute);
$entity->fromArray($ret);
return true;
}
public function bindUrl(string $parameter, mixed $value) : self
{
$this->urlParameters[$parameter] = $value;
return $this;
}
protected function buildRequestUrl(string $uri) :string
{
$uri = $this->prepareUri($uri, $this->urlParameters);
return $this->adapter->adapter()->url . '/' . ltrim($uri, '/');
}
protected function prepareUri(string $route, array $arguments) : string
{
if ( preg_match_all('~{(.*?)}~si', $route, $matches, PREG_SET_ORDER) ) {
$search = [];
foreach($matches as $item) {
$default = null;
$variable = $item[1];
# Handles default
if (strpos($variable, "=") !== false) {
list($variable, $default) = explode('=', $item[1]);
}
if ( array_key_exists($variable, $arguments) ) {
$value = $arguments[ $variable ];
unset($arguments[ $variable ]);
}
else {
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']));
}
}
$search[$item[0]] = rawurlencode($value ?? "");
}
$route = str_replace(array_keys($search), array_values($search), $route);
}
return $route;
}
public function getApiAttribute(string $type) : object
{
return $this->entityClass::resolveEntity()->getAttributeImplementing($type);
}
protected function callApiResponseCallback(ResponseInterface $response, ApiAction $attribute) : mixed
{
return call_user_func_array($this->adapter->apiResponseCallback(), func_get_args());
}
}