Skip to content
This repository has been archived by the owner on Oct 14, 2023. It is now read-only.

add api-exchange configs #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ abstract class API
*/
protected $base_url = null;

/**
* @var null
*/
protected $path = null;

/**
* @var null
*/
protected $accessKey = null;

/**
* @var array
*/
Expand All @@ -30,8 +40,10 @@ abstract class API
*
* @param Client|null $client
*/
public function __construct(?Client $client = null)
public function __construct(?Client $client = null, array $config = [])
{
$this->base_url = $config['base_url'] ?? $this->base_url;
$this->accessKey = $config['access_key'] ?? $this->accessKey;
$this->client = $client;
}

Expand All @@ -57,6 +69,10 @@ protected function buildQueryParams()
$this->query_params[$key] = $param;
}
}

if (!is_null($this->accessKey)) {
$this->query_params['access_key'] = $this->accessKey;
}
}

/**
Expand All @@ -68,7 +84,7 @@ public function get()
$this->buildQueryParams();

$response = $this->request(
$this->base_url,
$this->base_url . $this->path,
$this->query_params
);

Expand Down Expand Up @@ -97,4 +113,4 @@ public function when($condition, callable $callback)

return $this;
}
}
}
15 changes: 10 additions & 5 deletions src/CurrencyConversion.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ class CurrencyConversion extends API
/**
* @var string
*/
protected $base_url = 'https://api.exchangerate.host/convert';
protected $base_url = 'https://api.exchangerate.host';

/**
* @var string
*/
protected $path = '/convert';

/**
* Required base currency
Expand Down Expand Up @@ -56,9 +61,9 @@ class CurrencyConversion extends API
*
* @param Client|null $client
*/
public function __construct(?Client $client = null)
public function __construct(?Client $client = null, $config = [])
{
parent::__construct($client);
parent::__construct($client, $config);

$this->setQueryParams(function () {
if (!$this->from) {
Expand All @@ -72,7 +77,7 @@ public function __construct(?Client $client = null)
$params = [
'from' => $this->from,
'to' => $this->to,
'amount' => $this->amount
'amount' => $this->amount,
];

if ($this->places) {
Expand Down Expand Up @@ -136,4 +141,4 @@ protected function getResults(object $response)
{
return $response->result ?? null;
}
}
}
16 changes: 13 additions & 3 deletions src/CurrencyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,24 @@

class CurrencyFactory
{
/**
* @var array
*/
protected $config = [];

public function __construct(array $config = [])
{
$this->config = $config;
}

/**
* @param Client|null $client
*
* @return CurrencyConversion
*/
public function convert(?Client $client = null)
{
return new CurrencyConversion($client);
return new CurrencyConversion($client, $this->config);
}

/**
Expand All @@ -22,6 +32,6 @@ public function convert(?Client $client = null)
*/
public function rates()
{
return new CurrencyRatesProxy();
return new CurrencyRatesProxy($this->config);
}
}
}
9 changes: 5 additions & 4 deletions src/CurrencyFluctuations.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ class CurrencyFluctuations extends CurrencyRates
* @param string $end_date
* @param Client|null $client
*/
public function __construct(string $start_date, string $end_date, ?Client $client = null)
public function __construct(string $start_date, string $end_date, ?Client $client = null, $config = [])
{
parent::__construct($client);
$this->base_url = "https://api.exchangerate.host/fluctuation";
parent::__construct($client, $config);
$this->base_url = "https://api.exchangerate.host";
$this->path = "/fluctuation";
$this->params['start_date'] = $start_date;
$this->params['end_date'] = $end_date;
}
Expand All @@ -39,4 +40,4 @@ protected function getResults(object $response)

return null;
}
}
}
9 changes: 5 additions & 4 deletions src/CurrencyHistoricalRates.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ class CurrencyHistoricalRates extends CurrencyRates
* @param string $date
* @param Client|null $client
*/
public function __construct(string $date, ?Client $client = null)
public function __construct(string $date, ?Client $client = null, $config = [])
{
parent::__construct($client);
$this->base_url = "https://api.exchangerate.host/{$date}";
parent::__construct($client, $config);
$this->base_url = "https://api.exchangerate.host";
$this->path= "/{$date}";
}
}
}
9 changes: 5 additions & 4 deletions src/CurrencyLatestRates.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ class CurrencyLatestRates extends CurrencyRates
*
* @param Client|null $client
*/
public function __construct(?Client $client = null)
public function __construct(?Client $client = null, $config = [])
{
parent::__construct($client);
$this->base_url = "https://api.exchangerate.host/latest";
parent::__construct($client, $config);
$this->base_url = "https://api.exchangerate.host";
$this->path = "/latest";
}
}
}
6 changes: 3 additions & 3 deletions src/CurrencyRates.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ class CurrencyRates extends API
*
* @param Client|null $client
*/
public function __construct(?Client $client = null)
public function __construct(?Client $client = null, $config = [])
{
parent::__construct($client);
parent::__construct($client, $config);

$this->setQueryParams(function () {
$params = ['amount' => $this->amount];
Expand Down Expand Up @@ -109,4 +109,4 @@ protected function getResults(object $response)

return null;
}
}
}
20 changes: 15 additions & 5 deletions src/CurrencyRatesProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,24 @@

class CurrencyRatesProxy
{
/**
* @var array
*/
protected $config = [];

public function __construct($config = [])
{
$this->config = $config;
}

/**
* @param Client|null $client
*
* @return CurrencyLatestRates
*/
public function latest(?Client $client = null)
{
return new CurrencyLatestRates($client);
return new CurrencyLatestRates($client, $this->config);
}

/**
Expand All @@ -24,7 +34,7 @@ public function latest(?Client $client = null)
*/
public function historical(string $date, ?Client $client = null)
{
return new CurrencyHistoricalRates($date, $client);
return new CurrencyHistoricalRates($date, $client, $this->config);
}

/**
Expand All @@ -36,7 +46,7 @@ public function historical(string $date, ?Client $client = null)
*/
public function timeSeries(string $date_from, string $date_to, ?Client $client = null)
{
return new CurrencyTimeSeriesRates($date_from, $date_to, $client);
return new CurrencyTimeSeriesRates($date_from, $date_to, $client, $this->config);
}

/**
Expand All @@ -48,6 +58,6 @@ public function timeSeries(string $date_from, string $date_to, ?Client $client =
*/
public function fluctuations(string $date_from, string $date_to, ?Client $client = null)
{
return new CurrencyFluctuations($date_from, $date_to, $client);
return new CurrencyFluctuations($date_from, $date_to, $client, $this->config);
}
}
}
9 changes: 5 additions & 4 deletions src/CurrencyTimeSeriesRates.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ class CurrencyTimeSeriesRates extends CurrencyRates
* @param string $end_date
* @param Client|null $client
*/
public function __construct(string $start_date, string $end_date, ?Client $client = null)
public function __construct(string $start_date, string $end_date, ?Client $client = null, $config = [])
{
parent::__construct($client);
$this->base_url = "https://api.exchangerate.host/timeseries";
parent::__construct($client, $config);
$this->base_url = "https://api.exchangerate.host";
$this->path = "/timeseries";
$this->params['start_date'] = $start_date;
$this->params['end_date'] = $end_date;
}
Expand All @@ -39,4 +40,4 @@ protected function getResults(object $response)

return null;
}
}
}