144314100b686db946ff68c7ae1065d1

I know that variable naming isn't good but anything else? Are there any ways to make it better?

// parsing rates from http://www.cbr.ru/scripts/XML_daily.asp
// where date - past or current date d/m/Y (current date by default)

<?php
    public function get_usd_value($date)
    {
        $allow_currency = array('USD', 'EUR', 'GBP');
        $link = "http://www.cbr.ru/scripts/XML_daily.asp?date_req=$date";
        $text = @file_get_contents($link);

        $xml = new SimpleXMLElement($text);

        foreach ($xml->Valute as $curr)
        {
            if(in_array($curr->CharCode, $allow_currency))
            {
                $currency[] = UTF8::str_ireplace(',', '.', (string)$curr->Value);
            }
        }

		$USD = $currency[0];
		$EUR = round($currency[2]/$USD, 5);
		$GBP = round($currency[2]/$USD, 5);
		
	
		$rate_array = array("USD" => $USD/$USD, "EUR" => $EUR, "GBP" => $GBP);
		return $rate_array;
    }
?>

Refactorings

No refactoring yet !

55502f40dc8b7c769880b10874abc9d0

michal-pochwala.myopenid.com

August 26, 2010, August 26, 2010 07:09, permalink

2 ratings. Login to rate!
<?php
class Currency{
	const USD = 'USD';
	const EUR = 'EUR';
	const GBP = 'GBP';
	const AUD = 'AUD';
}
<?php
class ExchangeRate{
	private $fromCurrency;
	private $toCurrency;
	private $rate;
	public function __construct($fromCurrency, $toCurrency, $rate){
		$this->fromCurrency = $fromCurrency;
		$this->toCurrency = $toCurrency;
		$this->rate = $rate;
	}
	public function getExchangeRate(){
		return $this->rate;
	}
	public function getFrom(){
		return $this->fromCurrency;
	}
	public function getTo(){
		return $this->fromCurrency;
	}
}
<?php
class ExchangeRateRepository{
	private $baseUrl;
	public function __construct($baseUrl){
		$this->baseUrl = $baseUrl;
	}
	public function getExchangeRatesByDate($date,$acceptedCurrencies){
		$xml = $this->getXMLDataFromRepositoryByDate($date);		
		return $this->getExchangeRatesFromXML($xml,$acceptedCurrencies);
	} 
	private function getExchangeRatesFromXML($xml,$acceptedCurrencies){
		$exchangeRates= array();
		foreach ( $xml->Valute as $currencyNode){
			if( in_array($currencyNode->CharCode,$acceptedCurrencies ) ){
				$rate = $this->convertCurrencyFormat( (string)$currencyNode->Value);
				$exchangeRates[] = new ExchangeRate(Currency::USD,$currencyNode->CharCode,$rate);
			}
		}
		return $exchangeRates;
	}
	private function getXMLDataFromRepositoryByDate($date){
		$xmlFileContent= file_get_contents( $this->buildURIToRepository($date) );		
		return new SimpleXMLElement($xmlFileContent);
	}
	private function buildURIToRepository($date){
		return $this->baseUrl.$date;
	}
	public function findExchangeRateForCurrency($date, $currency){
		return $this->getExchangeRatesByDate($date,array( $currency ));
	}
	private function convertCurrencyFormat($value){
		return str_ireplace(',','.',$value);
	}
}
<?php
$urlToRepository = "http://www.cbr.ru/scripts/XML_daily.asp?date_req=";
$exchangeRateRepository = new ExchangeRateRepository($urlToRepository);

$date = date('d-m-Y');
$acceptedCurrencies = array(Currency::USD,Currency::EUR,Currency::GBP);

$exchangeRates = $exchangeRateRepository->getExchangeRatesByDate($date,$acceptedCurrencies);
//you can also search for only one exchange rate
$audExchangeRate = $exchangeRateRepository->findExchangeRateForCurrency($date,Currency::AUD);
2e365ac3ac207524ec99a482ae3737e7

Felipe Alcacibar

October 22, 2010, October 22, 2010 23:25, permalink

No rating. Login to rate!

More tiny =D, it returns an object with properties USD, and the "also" currency names.

<?php
    function get_usd_value($also, $date='')
    {
        $also           = explode(',', strtoupper($also));
        array_unshift($also, 'USD');

        $link           = "http://www.cbr.ru/scripts/XML_daily.asp?date_req=$date";
        $xml            = new SimpleXMLElement(@file_get_contents($link));

        foreach($xml->Valute as $curr) {
            if(in_array($curr->CharCode, $also)) {
                $currency[(string) $curr->CharCode] = (float) str_replace(',','.', (string) $curr->Value);
            }
        }

        foreach($currency as $key => $value) {
                if($key != 'USD')
                        $currency[$key] = round($currency[$key] / $currency['USD'], 5);
        }

        return (object) $currency;
    }
?>

Your refactoring





Format Copy from initial code

or Cancel