public function ExchangerProviderRates::__construct in Commerce Exchanger 8
Constructs a new ExchangerProviderRates instance.
Parameters
array $definition: The definition.
File
- src/
ExchangerProviderRates.php, line 46
Class
- ExchangerProviderRates
- Represents remote exchange rates data with base currency.
Namespace
Drupal\commerce_exchangerCode
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'];
}