Guest User

Untitled

a guest
Feb 16th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.75 KB | None | 0 0
  1. Class FixerConverter implements Converter {
  2.  
  3.     public function loadConversionRate(string $baseCurrency, string $toCurrency): string
  4.     {
  5.         $conversionRateJson = @file_get_contents('http://api.fixer.io/latest?base=' . $baseCurrency . '&symbols=' . $toCurrency));
  6.  
  7.         if ($conversionRateJson === false) {
  8.             echo 'Error: unexpected error retrieving currency conversion rate from API service.';
  9.         }
  10.  
  11.         $conversionRateArr = json_decode($conversionRateJson, TRUE);
  12.  
  13.         $this->conversionRate = $conversionRateArr['rates'][$currency];
  14.     }
  15.  
  16.     public function convertCurrency(string $amount): float
  17.     {
  18.         if (isset($this->conversionRate)) {
  19.             return $amount * $this->conversionRate;        
  20.         }
  21.  
  22.         echo 'Error: There is no conversion rate loaded.';
  23.     }
  24.  
  25. }
Add Comment
Please, Sign In to add comment