From dbf992423d04837ab83fbfe2b364c88e6c20cf46 Mon Sep 17 00:00:00 2001 From: Catalin Vasiliu Date: Fri, 6 Oct 2023 14:02:06 +0300 Subject: [PATCH 1/2] add api-exchange configs --- src/API.php | 22 +++++++++++++++++++--- src/CurrencyConversion.php | 16 +++++++++++----- src/CurrencyFactory.php | 16 +++++++++++++--- src/CurrencyFluctuations.php | 9 +++++---- src/CurrencyHistoricalRates.php | 9 +++++---- src/CurrencyLatestRates.php | 9 +++++---- src/CurrencyRates.php | 6 +++--- src/CurrencyRatesProxy.php | 20 +++++++++++++++----- src/CurrencyTimeSeriesRates.php | 9 +++++---- 9 files changed, 81 insertions(+), 35 deletions(-) diff --git a/src/API.php b/src/API.php index 55a721c..c977635 100644 --- a/src/API.php +++ b/src/API.php @@ -15,6 +15,16 @@ abstract class API */ protected $base_url = null; + /** + * @var null + */ + protected $path = null; + + /** + * @var null + */ + protected $accessKey = null; + /** * @var array */ @@ -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; } @@ -57,6 +69,10 @@ protected function buildQueryParams() $this->query_params[$key] = $param; } } + + if (!is_null($this->accessKey)) { + $this->query_params['access_key'] = $this->accessKey; + } } /** @@ -68,7 +84,7 @@ public function get() $this->buildQueryParams(); $response = $this->request( - $this->base_url, + $this->base_url . $this->path, $this->query_params ); @@ -97,4 +113,4 @@ public function when($condition, callable $callback) return $this; } -} \ No newline at end of file +} diff --git a/src/CurrencyConversion.php b/src/CurrencyConversion.php index 743c8ed..af2cc40 100644 --- a/src/CurrencyConversion.php +++ b/src/CurrencyConversion.php @@ -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 @@ -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) { @@ -72,7 +77,8 @@ public function __construct(?Client $client = null) $params = [ 'from' => $this->from, 'to' => $this->to, - 'amount' => $this->amount + 'amount' => $this->amount, + 'access_key' => env('API_EXCHANGE_API_KEY'), ]; if ($this->places) { @@ -136,4 +142,4 @@ protected function getResults(object $response) { return $response->result ?? null; } -} \ No newline at end of file +} diff --git a/src/CurrencyFactory.php b/src/CurrencyFactory.php index 6ae18fb..d0edb5a 100644 --- a/src/CurrencyFactory.php +++ b/src/CurrencyFactory.php @@ -6,6 +6,16 @@ class CurrencyFactory { + /** + * @var array + */ + protected $config = []; + + public function __construct(array $config = []) + { + $this->config = $config; + } + /** * @param Client|null $client * @@ -13,7 +23,7 @@ class CurrencyFactory */ public function convert(?Client $client = null) { - return new CurrencyConversion($client); + return new CurrencyConversion($client, $this->config); } /** @@ -22,6 +32,6 @@ public function convert(?Client $client = null) */ public function rates() { - return new CurrencyRatesProxy(); + return new CurrencyRatesProxy($this->config); } -} \ No newline at end of file +} diff --git a/src/CurrencyFluctuations.php b/src/CurrencyFluctuations.php index 5a4a2ef..554112b 100644 --- a/src/CurrencyFluctuations.php +++ b/src/CurrencyFluctuations.php @@ -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; } @@ -39,4 +40,4 @@ protected function getResults(object $response) return null; } -} \ No newline at end of file +} diff --git a/src/CurrencyHistoricalRates.php b/src/CurrencyHistoricalRates.php index 56021af..d34c00d 100644 --- a/src/CurrencyHistoricalRates.php +++ b/src/CurrencyHistoricalRates.php @@ -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}"; } -} \ No newline at end of file +} diff --git a/src/CurrencyLatestRates.php b/src/CurrencyLatestRates.php index bfaeb5f..ca42300 100644 --- a/src/CurrencyLatestRates.php +++ b/src/CurrencyLatestRates.php @@ -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"; } -} \ No newline at end of file +} diff --git a/src/CurrencyRates.php b/src/CurrencyRates.php index e9e487e..03f4901 100644 --- a/src/CurrencyRates.php +++ b/src/CurrencyRates.php @@ -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]; @@ -109,4 +109,4 @@ protected function getResults(object $response) return null; } -} \ No newline at end of file +} diff --git a/src/CurrencyRatesProxy.php b/src/CurrencyRatesProxy.php index 3b1f9dd..88de60f 100644 --- a/src/CurrencyRatesProxy.php +++ b/src/CurrencyRatesProxy.php @@ -6,6 +6,16 @@ class CurrencyRatesProxy { + /** + * @var array + */ + protected $config = []; + + public function __construct($config = []) + { + $this->config = $config; + } + /** * @param Client|null $client * @@ -13,7 +23,7 @@ class CurrencyRatesProxy */ public function latest(?Client $client = null) { - return new CurrencyLatestRates($client); + return new CurrencyLatestRates($client, $this->config); } /** @@ -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); } /** @@ -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); } /** @@ -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); } -} \ No newline at end of file +} diff --git a/src/CurrencyTimeSeriesRates.php b/src/CurrencyTimeSeriesRates.php index 164cb48..38608bb 100644 --- a/src/CurrencyTimeSeriesRates.php +++ b/src/CurrencyTimeSeriesRates.php @@ -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; } @@ -39,4 +40,4 @@ protected function getResults(object $response) return null; } -} \ No newline at end of file +} From 7a242d1ddfee31cc8784432a3bf49b169786363a Mon Sep 17 00:00:00 2001 From: Catalin Vasiliu Date: Tue, 10 Oct 2023 10:38:33 +0300 Subject: [PATCH 2/2] remove unnecessary api key --- src/CurrencyConversion.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/CurrencyConversion.php b/src/CurrencyConversion.php index af2cc40..6335138 100644 --- a/src/CurrencyConversion.php +++ b/src/CurrencyConversion.php @@ -78,7 +78,6 @@ public function __construct(?Client $client = null, $config = []) 'from' => $this->from, 'to' => $this->to, 'amount' => $this->amount, - 'access_key' => env('API_EXCHANGE_API_KEY'), ]; if ($this->places) {