Advertisement
Guest User

Untitled

a guest
Feb 16th, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.05 KB | None | 0 0
  1. Class FixerConverter implements Converter {
  2.  
  3.     private $baseCurrency;
  4.  
  5.     private $convertedCurrencies = [];
  6.  
  7.     public function __construct(string $baseCurrency)
  8.     {
  9.         $this->baseCurrency = $baseCurrency;
  10.     }
  11.  
  12.     private function getConversionRate(string $currency): string
  13.     {
  14.         if (!isset($savedConversionRates[$currency])) {
  15.  
  16.             $conversionRateJson = @file_get_contents('http://api.fixer.io/latest?base=' . $this->baseCurrency . '&symbols=' . $currency));
  17.  
  18.             if ($conversionRateJson === false) {
  19.                 echo 'Error: unexpected error retrieving currency conversion rate from API service.';
  20.             }
  21.  
  22.             $conversionRateArr = json_decode($conversionRateJson, TRUE);
  23.  
  24.             $conversionRate = $conversionRateArr['rates'][$currency];
  25.  
  26.             $this->savedConversionRates[$currency] = $conversionRate;
  27.  
  28.             return $conversionRate;
  29.            
  30.         } else {
  31.             return $savedConversionRates[$currency];
  32.         }
  33.     }
  34.  
  35.     public function convertToCurrency(string $amount, string $currency): float
  36.     {
  37.         return $amount * $this->getConversionRate($currency);
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement