Skip to content

Commit

Permalink
Merge pull request #9 from cytecbg/master
Browse files Browse the repository at this point in the history
Library update to facilitate the new exchangeratesapi.io requirements
  • Loading branch information
benmajor authored Apr 13, 2021
2 parents 0915a21 + 7962b20 commit 4d64835
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 7 deletions.
33 changes: 28 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,38 +48,49 @@ use \BenMajor\ExchangeRatesAPI\ExchangeRatesAPI;
use \BenMajor\ExchangeRatesAPI\Response;
use \BenMajor\ExchangeRatesAPI\Exception;
$lookup = new ExchangeRatesAPI();
$access_key = '<YOUR ACCESS KEY>';
$use_ssl = false; # Free plans are restricted to non-SSL only.
$lookup = new ExchangeRatesAPI($access_key, $use_ssl);
$rates = $lookup->fetch();
```

**Historical data:**<br />
Get historical rates for any day since 1999:

```
$lookup = new ExchangeRatesAPI();
$access_key = '<YOUR ACCESS KEY>';
$lookup = new ExchangeRatesAPI($access_key);
$rates = $lookup->setFetchDate('2015-01-20')->fetch();
```

Get historical rates for a time period:

```
$lookup = new ExchangeRatesAPI();
$access_key = '<YOUR ACCESS KEY>';
$lookup = new ExchangeRatesAPI($access_key);
$rates = $lookup->addDateFrom('2015-01-20')->addDateTo('2015-01-21')->fetch();
```

**Set the base currency:**<br />
By default, the base currency is set to Euro (EUR), but it can be changed:

```
$lookup = new ExchangeRatesAPI();
$access_key = '<YOUR ACCESS KEY>';
$lookup = new ExchangeRatesAPI($access_key);
$rates = $lookup->setBaseCurrency('GBP')->fetch();
```

**Fetch specific rates:**<br />
If you do not want all current rates, it's possible to specify only the currencies you want using `addRate()`. The following code fetches only the exchange rate between GBP and EUR:

```
$lookup = new ExchangeRatesAPI();
$access_key = '<YOUR ACCESS KEY>';
$lookup = new ExchangeRatesAPI($access_key);
$rates = $lookup->addRate('EUR')->setBaseCurrency('GBP')->fetch();
```

Expand Down Expand Up @@ -108,6 +119,12 @@ Set the end date for the retrieval of historic rates. `$to` should be a string c
`getDateTo()`:<br />
Returns the specified end date for the retrieval of historic rates. Returns `null` if none is specified.

`getAccessKey()`:<br />
Returns the `access_key` that is currently in use.

`getUseSSL()`:<br />
Returns a boolean flag that determines which API URL will be used to perform requests. Free plans are restricted to non-SSL usage.

`removeDateTo()`:<br />
Removes the specified end date for the retrieval of historic rates.

Expand Down Expand Up @@ -139,6 +156,12 @@ Removes multiple currencies that has already been added to the retrieval list.
Removes a currency that has already been added to the retrieval list. `$code` should be passed an ISO 4217 code (e.g. `EUR`).<br />
`$code` must be one of the [supported currency codes](#5-supported-currencies).

`setAccessKey( string $access_key )`:<br />
Sets `access_key` to be used in all requests.

`setUseSSL( bool $use_ssl )`:<br />
Sets the API URL according to the selected mode (SSL or non-SSL). Free plans are restricted to non-SSL usage.

`fetch( bool $returnJSON = false, bool $parseJSON = true )`:<br />
Send off the request to the API and return either a `Response` object, or the raw JSON response. If `$returnJSON` is set to `true`, a standard PHP object will be returned, rather than the `ExchangeRatesAPI\Response` object.

Expand Down
58 changes: 56 additions & 2 deletions src/ExchangeRatesAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@

class ExchangeRatesAPI
{
# Default API URL:
const API_URL_SSL = 'https://api.exchangeratesapi.io/';

# Free plan API URL:
const API_URL_NON_SSL = 'http://api.exchangeratesapi.io/';

# Fetch date
private $fetchDate;

Expand All @@ -32,7 +38,7 @@ class ExchangeRatesAPI
private $client;

# The URL of the API:
private $apiURL = 'https://api.exchangeratesapi.io/';
private $apiURL = self::API_URL_SSL;

# Regular Expression for the date:
private $dateRegExp = '/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/';
Expand All @@ -57,8 +63,14 @@ class ExchangeRatesAPI
'format.invalid_rounding' => 'Rounding precision must be specified as a numeric value.'
];

function __construct( )
# ExchangeRatesAPI Access Key:
private $access_key;

function __construct( string $access_key = null, bool $use_ssl = true )
{
$this->setAccessKey($access_key);
$this->setUseSSL($use_ssl);

$this->client = new \GuzzleHttp\Client([ 'base_uri' => $this->apiURL ]);
}

Expand Down Expand Up @@ -104,6 +116,18 @@ public function getRates( string $concat = null )
return (! is_null($concat) ) ? implode($concat, $this->rates) : $this->rates;
}

# Get access key:
public function getAccessKey()
{
return $this->access_key;
}

# Get boolean flag for SSL usage:
public function getUseSSL()
{
return ($this->apiURL == self::API_URL_SSL);
}

/****************************/
/* */
/* SETTERS / DATA METHODS */
Expand Down Expand Up @@ -259,6 +283,30 @@ public function removeRate( string $currency )
return $this;
}

# Set access key:
public function setAccessKey( string $access_key = null )
{
$this->access_key = $access_key;

# Return object to preseve method chaining:
return $this;
}

# Set SSL flag and API URL:
public function setUseSSL( bool $use_ssl = true )
{
if ( $use_ssl )
{
$this->apiURL = self::API_URL_SSL;
}
else
{
$this->apiURL = self::API_URL_NON_SSL;
}

return $this;
}

/****************************/
/* */
/* API FUNCTION CALLS */
Expand Down Expand Up @@ -297,6 +345,12 @@ public function fetch( $returnJSON = false, $parseJSON = true )
{
# Build the URL:
$params = [ ];

# Set access key if available:
if ( !is_null($this->getAccessKey()) )
{
$params['access_key'] = $this->getAccessKey();
}

# Set the relevant endpoint:
if( is_null($this->dateFrom) )
Expand Down

0 comments on commit 4d64835

Please sign in to comment.