2016-05-31 134 views
0

我有一個類,目前拉一個貨幣的信息USD/EUR。我想添加更多像AUD/EURCAD/EUR等,...然後通過下拉菜單用戶可以選擇看什麼。到目前爲止,這是類多種貨幣請求類

class API 
{ 
    public static function getUSDRate() { 
     $urldata = get_curl_content('https://example.com/'); 
     $rates = json_decode($urldata, TRUE);   
     if (!$rates) { 
      return '-'; 
     } 

     $usdRate = @$rates['USD']['sell']; 
     if (!$usdRate) {   
      return '-'; 
     }   
     return $usdRate; 
    } 

    public static function convertUSDToEUR($usdPrice) { 
     $usdRate = self::getUSDRate(); 
     if ($usdRate === '-') { 
      return '-'; 
    } 

    $usdRate = doubleval($usdRate); 
    return round($usdPrice/$usdRate, 8); 
} 

所以我想補充例如該

$audRate = @$rates['AUD']['sell']; 
if (!$audRate) {   
     return '-'; 
}   
return $audRate; 

public static function convertAUDToEUR($audPrice) { 
    $audRate = self::getAUDRate(); 
    if ($audRate === '-') { 
     return '-'; 
} 

$audRate = doubleval($audRate); 
return round($audPrice/$audRate, 8); 

我是否需要寫每個除了貨幣呢?或者有更多'聰明'的方式?或者我必須爲每種貨幣創建課程?

+1

備註:從不在調試階段抑制錯誤!這取決於你可以結合使用'getUSDRate()'和'getAUDRate()'或不使用 – Raptor

+0

謝謝。注意採取:) –

+0

我不明白你的意思? –

回答

1

這是一個支持從任何貨幣轉換的類的想法。您必須在創建類的實例時提供幣種,然後才能使用該對象的方法進行轉換。

就像在你的代碼中,匯率被讀取到一個靜態變量,只有當尚未完成之前:如果您收到的所有貨幣

// define the currency of your object. 
// You could pass 'AUD' or any other supported currency 
$usd = new CurrencyRate('USD'); 

// set amount in that currency 
$usd->amount(12.34); 

// get amount in that currency (is the same) 
echo "amount: " . $usd->amount() . " USD<br>"; 

// get rate from that currency to EUR 
echo "rate: " . $usd->rate() . "<br>"; 

// get amount in EUR 
echo "Converted: " . $usd->euro() . " EUR<br>"; 
1

class CurrencyRate { 
    public static $rates; 
    private $conversionRate; 
    private $currency; 
    private $amount; 

    function __construct($currFrom = "EUR") { 
     // Currency to be provided when creating instance. EUR is default. 
     if (empty(static::$rates)) { 
      // Only request rates when not already loaded 
      $urldata = get_curl_content('https://example.com/'); 
      self::$rates = json_decode($urldata, TRUE); 
      // Add dummy rate for converting EUR to EUR 
      self::$rates['EUR'] = ['sell' => '1']; 
     } 
     $this->currency = $currFrom; 
     // Get exchange rate to EUR for this currency 
     if (isset(self::$rates[$currFrom]['sell'])) { 
      $this->conversionRate = doubleval(self::$rates[$currFrom]['sell']); 
     } else { 
      $this->conversionRate = 0; // not found 
     } 
    } 

    public function currency() { 
     return $this->currency; 
    } 

    public function rate() { 
     return $this->conversionRate; 
    } 

    public function amount($newAmount = null) { 
     // If argument is provided then this is a setter, otherwise a getter 
     if (isset($newAmount)) { 
      $this->amount = $newAmount; 
      return $this; // allow "chaining" 
     } 
     return $this->amount; 
    } 

    public function euro() { 
     // Convert object's amount to EUR 
     if ($this->conversionRate == 0) return "-"; // cannot convert 
     return round($this->amount/$this->conversionRate, 8); 
    } 
} 

使用示例從你的服務我可以建議像這樣的東西:

<?php 
    class API 
    { 
     // caching rates service results 
     protected $ratesCache; 

     // requesting rates service 
     // (we can force API request again by passing true) 
     public function getRates($cacheClear = false) { 
      // if we don't need to clear the cache and we already 
      // did request to rate service 
      if ($cacheClear != true && !empty($this->ratesCache)) { 
       // returning cached result 
       return $this->ratesCache; 
      } 

      // request to rate service 
      $urldata = get_curl_content('https://example.com/'); 
      $rates = json_decode($urldata, TRUE);   
      if (empty($rates)) { 
       return '-'; 
      } 

      // caching result 
      $this->ratesCache = $rates; 

      return $this->ratesCache; 
     } 

     // return the list of available rates 
     public function getRatesList() { 
      $rates = $this->getRates(); 
      // you can add here foreach to check which rates has 'sell' value 
      // so you list only possible to convert currencies 
      return array_keys($rates); 
     } 

     // convert currencies value to EUR 
     public function convertToEUR($currency, $price) { 
      $rates = $this->getRates(); 
      if (empty($rates[$currency]['sell'])) { 
       return '-'; 
      } 

      $rate = doubleval($rates[$currency]); 
      return round($price/$rate, 8); 
     } 
    } 

和使用將會像:

<?php 
$rateConvertor = new Api(); 
$availableRates = $rateConvertor->getRates(); 
// using foreach you can build <select> with currencies 
// ... 
// user select currency, set price (or you get it from somewhere else) 
$amount = $rateConvertor->convertToEUR($currency, $price);