public function getRates() { $file = file_get_contents(self::URL); $xml = new \SimpleXMLElement($file); Rates::setRate('EUR', 1); foreach ($xml->Cube->Cube->Cube as $item) { $currency = (string) $item->attributes()->currency; $rate = (double) $item->attributes()->rate; if (isset($currency) && isset($rate)) { Rates::setRate($currency, $rate); } } }
/** * @param float $amountFrom * @param string $CurrencyFrom * @param string $CurrencyTo */ public function calculate($amountFrom, $CurrencyFrom, $CurrencyTo) { $ratioFrom = Rates::getRate($CurrencyFrom); $ratioTo = Rates::getRate($CurrencyTo); //Set amount 1 if less than zero was set $amountFrom = $amountFrom > 0 ? $amountFrom : 1; if ($ratioFrom <= 0 && $ratioTo <= 0) { throw new \OutOfRangeException('No information about current rates'); } $this->amount = round($amountFrom * ($ratioTo / $ratioFrom), 4); $this->rateTo = round($ratioTo / $ratioFrom, 4); $this->rateFrom = round($ratioFrom / $ratioTo, 4); }