CurrencyLayer extends ExchangeRate
Currency Layer Currency Exchange Rate Processor
Table of Contents
Properties
| $key | |
| $rates | |
| $timeout | |
| $url |
Methods
| __construct() | Initializes the exchange rate processor |
| getRate() | Fetches the exchange rate from currency A to currency B using the given amount |
| requiresKey() | Determines whether an API key is necessary to retrieve exchange rates via the processor |
| setKey() | Sets the API key necessary to retrieve exchange rates |
| convertRates() | Updates all currency rates given to convert the rates relative to the given currency |
| fetchRates() | Fetches the currency exchange rates via the API |
| formatRates() | Formats the rates into a common format |
| getExchangeRates() | Retrieves a list of exchange rates relative to the given $currency_from |
Properties
$key
protected
string
$key
The API key
$rates
private
array<string|int, mixed>
$rates
= []
The currency exchange rates keyed by the base currency
$timeout
private
static int
$timeout
= 30
The maximum number of seconds to wait for a response
$url
private
static string
$url
= 'http://apilayer.net/api/live'
The URL to the currency exchange rate resource
Methods
__construct()
Initializes the exchange rate processor
public
__construct(Http $Http) : mixed
Parameters
- $Http : Http
-
The Http component to more easily facilitate HTTP requests to fetch data
getRate()
Fetches the exchange rate from currency A to currency B using the given amount
public
getRate(string $currency_from, string $currency_to[, float $amount = 1.0 ]) : bool|array<string|int, mixed>
Parameters
- $currency_from : string
-
The ISO 4217 currency code to convert from
- $currency_to : string
-
The ISO 4217 currency code to convert to
- $amount : float = 1.0
-
The amount to convert
Return values
bool|array<string|int, mixed> —False on error or an array containing the exchange rate information including:
- rate The exchange rate for the supplied amount
- updated The date/time of the last update in YYYY-MM-DD HH:MM:SS format in UTC time
requiresKey()
Determines whether an API key is necessary to retrieve exchange rates via the processor
public
requiresKey() : bool
Return values
bool —True if an API key is required, or false otherwise
setKey()
Sets the API key necessary to retrieve exchange rates
public
setKey(string $key) : mixed
Parameters
- $key : string
-
The key for the exchange rate processor API
convertRates()
Updates all currency rates given to convert the rates relative to the given currency
protected
convertRates(string $to_currency, array<string|int, mixed> $rates) : array<string|int, mixed>
Parameters
- $to_currency : string
-
The ISO 4217 currency code to convert relative to
- $rates : array<string|int, mixed>
-
A key/value list of all currencies and their rates. The $to_currency must be in the list
Return values
array<string|int, mixed> —A key/value list of all currency exchange rates
fetchRates()
Fetches the currency exchange rates via the API
private
fetchRates(string $currency) : string
Parameters
- $currency : string
-
The ISO 4217 currency code to convert from
Return values
string —A JSON-formatted string representing the API response
formatRates()
Formats the rates into a common format
private
formatRates(array<string|int, mixed> $rates, string $currency_from) : array<string|int, mixed>
Parameters
- $rates : array<string|int, mixed>
-
An array of currency exchange rates
- $currency_from : string
-
The ISO 4217 currency code to convert relative from
Return values
array<string|int, mixed> —An array of formatted currency exchange rates
getExchangeRates()
Retrieves a list of exchange rates relative to the given $currency_from
private
getExchangeRates(string $currency_from) : array<string|int, mixed>
Parameters
- $currency_from : string
-
The ISO 4217 currency code to convert from
Return values
array<string|int, mixed> —An array of currency exchange rates relative to the given currency