I have a default currency as USD.
The part of the class below allow to convert different currencies but my problem is the conversion is always based in EUR as default.
How to update the function to use USD as default if it selected ?
Thank you
EUR = 1 (default) USD = 1.10 This approach works very well for any currencies
EUR = 0.9 USD = 1 (default) This approach do not work because USD is in default and the result is always like above.
Note :
$currenciesAdmin->getAll()
take all the currencies with the code (EUR) and the title (Euro) for example.
The value of EUR is always null because the convertion is based on EUR as default (see link ecb.europa.eu for the values)
public function getConvertCurrency()
{
$currenciesAdmin = new CurrenciesAdmin();
$XML = HTTP::getResponse([
'url' => 'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml'
]);
if (empty($XML)) {
throw new \Exception('Can not load currency rates from the European Central Bank website');
}
$currencies = [];
foreach ($currenciesAdmin->getAll() as $c) {
$currencies[$c['id']] = null;
}
$XML = new \SimpleXMLElement($XML);
foreach ($XML->Cube->Cube->Cube as $rate) {
if (array_key_exists((string)$rate['currency'], $currencies)) {
$currencies[(string)$rate['currency']] = (float)$rate['rate'];
}
}
foreach ($currencies as $code => $value) {
if (!is_null($value)) {
try {
$this->db->save('currencies', [
'value' => $value,
'last_updated' => 'now()'
], [
'code' => $code
]);
} catch (\PDOException $e) {
trigger_error($e->getMessage());
}
}
}
}