From 0c3853a1ee8dbb422fea92aaf9a0900565498c24 Mon Sep 17 00:00:00 2001 From: Alexandra Rys Date: Mon, 2 May 2022 20:26:19 +0300 Subject: [PATCH 1/4] Support access to the date property of the API response (as a string, in the format we control) --- README.md | 3 +++ src/Response.php | 13 +++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 709223d..8922436 100644 --- a/README.md +++ b/README.md @@ -212,6 +212,9 @@ Returns a key/value pair array of the exchange rates that match against the requ `getRate( string $code )`:
Retrieves the exchange rate for a specific currency, or returns the exchange rate if only one rate is present in the response. +`getDate()`:
+Retrieves the date returned as part of the response (formatted as Y-m-d). + ### 4. Supported Currencies: The library supports any currency currently available on the European Central Bank's web service, which can be found [here](https://exchangeratesapi.io/currencies/). diff --git a/src/Response.php b/src/Response.php index c865bca..3e4ad25 100644 --- a/src/Response.php +++ b/src/Response.php @@ -16,6 +16,7 @@ class Response private $statusCode; private $timestamp; private $baseCurrency; + private $date; private $rates = [ ]; @@ -32,6 +33,7 @@ function __construct( \GuzzleHttp\Psr7\Response $response = null ) $this->timestamp = date('c'); $this->baseCurrency = $this->body->base; $this->rates = $this->body->rates; + $this->date = date('Y-m-d', strtotime($this->body->date)); } /****************************/ @@ -46,7 +48,7 @@ public function getStatusCode() return (int) $this->statusCode; } - #ÊGet the timestamp of the request: + # Get the timestamp of the request: public function getTimestamp() { return $this->timestamp; @@ -57,6 +59,12 @@ public function getBaseCurrency() { return $this->baseCurrency; } + + # Get the date specified in the response: + public function getDate() + { + return $this->date; + } # Get the exchange rates: public function getRates() @@ -84,12 +92,13 @@ public function getRate( $code = null ) return null; } - #ÊConvert the response to JSON: + # Convert the response to JSON: public function toJSON() { return json_encode([ 'statusCode' => $this->getStatusCode(), 'timestamp' => $this->getTimestamp(), + 'date' => $this->getDate(), 'baseCurrency' => $this->getBaseCurrency(), 'rates' => $this->getRates() ]); From 953a19141c826e0dd2bfd16202498986a88c044d Mon Sep 17 00:00:00 2001 From: Alexandra Rys Date: Mon, 2 May 2022 20:43:10 +0300 Subject: [PATCH 2/4] Fix endpoint and parameter names for a timeseries request --- src/ExchangeRatesAPI.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ExchangeRatesAPI.php b/src/ExchangeRatesAPI.php index bf3419e..cd7bc97 100644 --- a/src/ExchangeRatesAPI.php +++ b/src/ExchangeRatesAPI.php @@ -408,19 +408,19 @@ public function fetch( $returnJSON = false, $parseJSON = true ) } else { - $endpoint = 'history'; + $endpoint = 'timeseries'; } # Add dateFrom if specified: if( ! is_null($this->getDateFrom()) ) { - $params['start_at'] = $this->getDateFrom(); + $params['start_date'] = $this->getDateFrom(); } # Add a dateTo: if( ! is_null($this->getDateTo()) ) { - $params['end_at'] = $this->getDateTo(); + $params['end_date'] = $this->getDateTo(); } # Set the base currency: From 7af864165d9da2b394134095a968b8a8e09c02e1 Mon Sep 17 00:00:00 2001 From: Alexandra Rys Date: Mon, 2 May 2022 20:48:04 +0300 Subject: [PATCH 3/4] Don't set a string date if it's not part of the response (possible for timeseries endpoint) --- src/Response.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Response.php b/src/Response.php index 3e4ad25..2f4241f 100644 --- a/src/Response.php +++ b/src/Response.php @@ -33,7 +33,10 @@ function __construct( \GuzzleHttp\Psr7\Response $response = null ) $this->timestamp = date('c'); $this->baseCurrency = $this->body->base; $this->rates = $this->body->rates; - $this->date = date('Y-m-d', strtotime($this->body->date)); + + if ($this->body->date) { + $this->date = date('Y-m-d', strtotime($this->body->date)); + } } /****************************/ From 51b1f2094b48e92c8622e179450d4c9ce106d564 Mon Sep 17 00:00:00 2001 From: Alexandra Rys Date: Mon, 2 May 2022 20:55:44 +0300 Subject: [PATCH 4/4] Make it clear that response date property can be null --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8922436..b5acc07 100644 --- a/README.md +++ b/README.md @@ -213,7 +213,7 @@ Returns a key/value pair array of the exchange rates that match against the requ Retrieves the exchange rate for a specific currency, or returns the exchange rate if only one rate is present in the response. `getDate()`:
-Retrieves the date returned as part of the response (formatted as Y-m-d). +Retrieves the date returned as part of the response (formatted as Y-m-d). If response has no `date` property, returns null. ### 4. Supported Currencies: