Skip to content

Commit

Permalink
Merge pull request #14 from sashotoster/response-date
Browse files Browse the repository at this point in the history
Support access to the date property of the API response
  • Loading branch information
benmajor authored May 3, 2022
2 parents ba21427 + 51b1f20 commit 662196b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@ Returns a key/value pair array of the exchange rates that match against the requ
`getRate( string $code )`:<br />
Retrieves the exchange rate for a specific currency, or returns the exchange rate if only one rate is present in the response.

`getDate()`:<br />
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:

The library supports any currency currently available on the European Central Bank's web service, which can be found [here](https://exchangeratesapi.io/currencies/).
Expand Down
6 changes: 3 additions & 3 deletions src/ExchangeRatesAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
16 changes: 14 additions & 2 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Response
private $statusCode;
private $timestamp;
private $baseCurrency;
private $date;

private $rates = [ ];

Expand All @@ -32,6 +33,10 @@ function __construct( \GuzzleHttp\Psr7\Response $response = null )
$this->timestamp = date('c');
$this->baseCurrency = $this->body->base;
$this->rates = $this->body->rates;

if ($this->body->date) {
$this->date = date('Y-m-d', strtotime($this->body->date));
}
}

/****************************/
Expand All @@ -46,7 +51,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;
Expand All @@ -57,6 +62,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()
Expand Down Expand Up @@ -84,12 +95,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()
]);
Expand Down

0 comments on commit 662196b

Please sign in to comment.