- First working version of ulmus-api package; at working state only
This commit is contained in:
1 parent
0f9495e07a
commit
7e69a09e7f
27 files changed
+1406
-101
No files matched your search
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
namespace Ulmus\Api\Adapter;
|
||||
|
||||
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 AuthenticationEnum $auth;
|
||||
|
||||
protected string $username;
|
||||
|
||||
protected string $password;
|
||||
|
||||
protected string $token;
|
||||
|
||||
protected array $headers = [];
|
||||
|
||||
protected array $options = [];
|
||||
|
||||
public readonly string $url;
|
||||
|
||||
public function connect(): object
|
||||
{
|
||||
return new CurlClient($this->headers, $this->options, ( new CurlAuthorization())->fromAuthenticationEnum($this->auth, $this->username ?? false, $this->password ?? false, $this->token ?? false) );
|
||||
}
|
||||
|
||||
public function buildDataSourceName(): string
|
||||
{
|
||||
return "https";
|
||||
}
|
||||
|
||||
public function setup(array $configuration): void
|
||||
{
|
||||
$this->auth = AuthenticationEnum::from($configuration['auth'] ?? AuthenticationEnum::Basic->value);
|
||||
$this->url = rtrim($configuration['url'], '/');
|
||||
|
||||
foreach([ 'username', 'password', 'token', 'headers', 'options' ] as $conf) {
|
||||
if ($configuration[$conf] ?? false) {
|
||||
$this->$conf = $configuration[$conf];
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user