102 lines
2.4 KiB
PHP
102 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace Ulmus\Api\Adapter;
|
|
|
|
use Negundo\Client\Transport\Curl;
|
|
use Ulmus\Adapter\AdapterInterface;
|
|
use Ulmus\Api\ApiRepository;
|
|
use Ulmus\Api\Common\AuthenticationEnum;
|
|
use Ulmus\Api\Common\ContentTypeEnum;
|
|
use Ulmus\Api\RequestBuilder;
|
|
use Ulmus\Api\Transport\CurlAuthorization;
|
|
use Ulmus\Api\Transport\CurlClient;
|
|
use Ulmus\Ulmus;
|
|
|
|
class Rest implements AdapterInterface
|
|
{
|
|
protected CurlAuthorization $authorization;
|
|
|
|
protected AuthenticationEnum $authorizationMethod;
|
|
|
|
protected array $headers = [];
|
|
|
|
protected array $options = [];
|
|
|
|
protected array $parameters = [];
|
|
|
|
public readonly string $url;
|
|
|
|
public function setup(array $configuration): void
|
|
{
|
|
$this->url = rtrim($configuration['url'], '/');
|
|
|
|
$this->authorizationMethod = AuthenticationEnum::from($configuration['auth'] ?: AuthenticationEnum::Basic->value);
|
|
$this->authorization = new CurlAuthorization($configuration);
|
|
|
|
$this->headers = $configuration['headers'] ?? [];
|
|
$this->options = $configuration['options'] ?? [];
|
|
$this->parameters = $configuration['parameters'] ?? [];
|
|
}
|
|
|
|
public function connect(): object
|
|
{
|
|
$this->authorization = $this->authorization->fromAuthenticationEnum($this->authorizationMethod);
|
|
|
|
return new CurlClient([], $this->options, $this->authorization);
|
|
}
|
|
|
|
public function buildDataSourceName(): string
|
|
{
|
|
return "https";
|
|
}
|
|
|
|
public function writableValue(mixed $value) : mixed
|
|
{
|
|
switch (true) {
|
|
case $value instanceof \UnitEnum:
|
|
return Ulmus::convertEnum($value);
|
|
|
|
case is_object($value):
|
|
return Ulmus::convertObject($value);
|
|
|
|
# case is_array($value):
|
|
# return json_encode($value);
|
|
|
|
case is_bool($value):
|
|
return (int) $value;
|
|
}
|
|
|
|
return $value;
|
|
}
|
|
|
|
public function repositoryClass(): string
|
|
{
|
|
return ApiRepository::class;
|
|
}
|
|
|
|
public function queryBuilderClass(): string
|
|
{
|
|
return RequestBuilder::class;
|
|
}
|
|
|
|
public function getCurlOptions() : array
|
|
{
|
|
return $this->options;
|
|
}
|
|
|
|
public function getHeaders() : array
|
|
{
|
|
return $this->headers;
|
|
}
|
|
|
|
public function getParameters() : array
|
|
{
|
|
return $this->parameters;
|
|
}
|
|
|
|
public function escapeIdentifier(string $segment, int $type): string
|
|
{
|
|
return $segment;
|
|
}
|
|
}
|