class ExchangerProviderRates in Commerce Exchanger 8
Represents remote exchange rates data with base currency.
@package Drupal\commerce_exchanger
Hierarchy
- class \Drupal\commerce_exchanger\ExchangerProviderRates
Expanded class hierarchy of ExchangerProviderRates
2 files declare their use of ExchangerProviderRates
- ExchangerProviderRemoteBase.php in src/
Plugin/ Commerce/ ExchangerProvider/ ExchangerProviderRemoteBase.php - ExchangerRemoteProviderTest.php in tests/
src/ Kernel/ ExchangerRemoteProviderTest.php
File
- src/
ExchangerProviderRates.php, line 10
Namespace
Drupal\commerce_exchangerView source
class ExchangerProviderRates {
/**
* The base currency.
*
* @var string
*/
protected $baseCurrency;
/**
* The provided rates from external provider.
*
* @var array
*/
protected $rates;
/**
* The list of enabled currencies.
*
* @var array
*/
protected $currencies;
/**
* Determine if transform prices is needed.
*
* @var bool
*/
protected $transform;
/**
* Constructs a new ExchangerProviderRates instance.
*
* @param array $definition
* The definition.
*/
public function __construct(array $definition) {
foreach ([
'base',
] as $required_property) {
if (empty($definition[$required_property])) {
throw new \InvalidArgumentException(sprintf('Missing required property "%s".', $required_property));
}
}
if (!is_array($definition['rates'])) {
throw new \InvalidArgumentException('The property "rates" must be an array.');
}
$this->currencies = $definition['currencies'] ?? NULL;
$this->baseCurrency = $definition['base'];
$this->transform = $definition['transform'] ?? FALSE;
if ($this->currencies && !isset($this->currencies[$this->baseCurrency])) {
throw new \InvalidArgumentException('The base currency need to be among enabled one in Drupal Commerce');
}
foreach ($definition['rates'] as $currency => $rate) {
if (!is_string($currency)) {
throw new \InvalidArgumentException('The currency code must be an string.');
}
if (!is_numeric($rate)) {
throw new \InvalidArgumentException('The rate must be an float or a numeric string.');
}
// Filter data.
if ($this->currencies && !isset($this->currencies[$currency])) {
unset($definition['rates'][$currency]);
continue;
}
$definition['rates'][$currency] = round($this->transform ? 1 / $rate : $rate, 6);
}
$this->rates = $definition['rates'];
}
/**
* Get base currency upon rates are built.
*
* @return string
* Return currency ISO code.
*/
public function getBaseCurrency() {
return $this->baseCurrency;
}
/**
* List all rate values keyed by currency code.
*
* @return array
* Keyed array by currency code, and value for rate. ['HRK' => '0.5']
*/
public function getRates() {
return $this->rates;
}
/**
* Determine if price where transformed.
*
* @return bool
* Return true if price where transformed.
*/
public function isTransform() {
return $this->transform;
}
/**
* List enabled currencies.
*
* @return array
* Return keyed currencies by currency code.
*/
public function getCurrencies() {
return $this->currencies;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ExchangerProviderRates:: |
protected | property | The base currency. | |
ExchangerProviderRates:: |
protected | property | The list of enabled currencies. | |
ExchangerProviderRates:: |
protected | property | The provided rates from external provider. | |
ExchangerProviderRates:: |
protected | property | Determine if transform prices is needed. | |
ExchangerProviderRates:: |
public | function | Get base currency upon rates are built. | |
ExchangerProviderRates:: |
public | function | List enabled currencies. | |
ExchangerProviderRates:: |
public | function | List all rate values keyed by currency code. | |
ExchangerProviderRates:: |
public | function | Determine if price where transformed. | |
ExchangerProviderRates:: |
public | function | Constructs a new ExchangerProviderRates instance. |