2016-05-31 8 views
0

現在、1通貨の情報を取得するクラスがあります。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

ここでは、任意の通貨からの変換をサポートするクラスの考え方を示します。クラスのインスタンスを作成するときに通貨を提供し、そのオブジェクトのメソッドを使用して変換を行うことができます。

あなたのコードのように、為替レートは、静的変数を読み込み、すでに前に行われていないときにのみされています

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); 
    } 
} 

使用例:

// 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

あなたはすべての通貨を取得している場合あなたのサービスから、私は何かのようなものを提案することができます:

<?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);