<?php
/*Class Ecb
* Zum Umrechnen von Geld Wechselkurs an Hand von aktuelen
* European Central Bank Daten.
* die Werte werden für eine Stunde gecacht um der Zugriff
* auf E.C.B Daten sparsam zu betreiben.
* @license Free for non-commercial use
* @example See below /sieh unten
**/
class Ecb{
#Member
//url from xml-data ECB
private $currencie_url="http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml";
private $cur_array=
array();
private static $cur_descript=
array('EUR'=>
'Euro',
'USD'=>
'US dollar',
'JPY'=>'Japanese yen','BGN'=>'Bulgarian lev',
'CYP'=>'Cyprus pound','CZK'=>'Czech koruna',
'DKK'=>'Danish krone','EEK'=>'Estonian kroon',
'GBP'=>'Pound sterling','HUF'=>'Hungarian forint',
'LTL'=>'Lithuanian litas','LVL'=>'Latvian lats',
'MTL'=>'Maltese lira','PLN'=>'Polish zloty',
'RON'=>'New Romanian leu 2','SEK'=>'Swedish krona',
'SKK'=>'Slovak koruna','CHF'=>'Swiss franc',
'ISK'=>'Icelandic krona','NOK'=>'Norwegian krone',
'HRK'=>'Croatian kuna','RUB'=>'Russian rouble',
'TRY'=>'New Turkish lira 3','AUD'=>'Australian dollar',
'CAD'=>'Canadian dollar','CNY'=>'Chinese yuan renminbi',
'HKD'=>'Hong Kong dollar','IDR'=>'Indonesian rupiah',
'KRW'=>'South Korean won','MYR'=>'Malaysian ringgit',
'NZD'=>'New Zealand dollar','PHP'=>'Philippine peso',
'SGD'=>'Singapore dollar','THB'=>'Thai baht',
'ZAR'=>'South African rand' );
protected $cach_file_name= 'currency.txt';//standard in gleichem ordner.
//chach time
protected $cach_time=3600;//eine Stunde cachen.
# /Member------------------------
/* Construct, Konstruktor */
public function __construct()
{
$this->
cach_file_name=
dirname( __FILE__ ).DIRECTORY_SEPARATOR.
$this->
cach_file_name;
(file_exists($this->
cach_file_name) &&
(filemtime($this->
cach_file_name)+
$this->
cach_time)<time
() ) )
{
$this->init_currencie();
}
else
{
//cach
}
$this->cur_array['EUR']=1.0;
}
#end construct----------------
#public Methods
/** !!!!!
* function calculate 'calculate $amount from, to' ,berchnet wechselkurs.
* @param $amoung: float, Geldbetrag
* @param $curr: string, Shortcut From , Kürzel für Geld, von dem gewechselt wird.
* @param $curr_to: string, Shortcut TO, Kürzel für Geld, in das gewechselt wird.
* $curr and $curr_to use keys from $this->cur_descript
* @return float
* @example echo '10 US Dolar='.$instance_of_Ecb->calculate(10,'USD','GBP').' Pound sterling';
**/
public function calculate($amount,$curr,$curr_to)
{
return $this->getRate($curr_to)*$amount/$this->getRate($curr);
}#end calculate
/** getDescription
* @access public, @static
* @param $val, optional use keys from $this->cur_descript, benutze keys aus $this->cur_descript
* @return float or mixed, if empty $val array(USD'=>'US dollar',....), else description on key $val.
* liefert bei eingesetzter $val eine description zu dem passendem Key in $this->cur_descript, oder der
* ganzer Inhalt von $this->cur_descript, wenn kein parameter übergeben wurde.
**/
public static function getDescription
($val=
false)
{
return $val?(self::$cur_descript[$val]) : (self::$cur_descript);
}#end getDescription
/**
* function getRate, rate currencie to EUR
* @acces public
* @param $currencie_short: string, GBR or USD ....
* @return :float
**/
public function getRate($currencie_short)
{
return isset($this->
cur_array[$currencie_short])?
$this->
cur_array[$currencie_short]:
false;
}#end getRate
/*
* @return :array , array('USD'=>1.404,.....)
**/
public function getCurrencieAll()
{
return $this->cur_array;
}#end getCurrencieAll
#end public Methods---------
//private Methods
private function init_currencie()
{
{
$this->cur_array=array_combine($array[1],$array[2]);
file_put_contents
($this->
cach_file_name,
serialize($this->
cur_array));
}
else die('Bug in '.
__FILE__.
' Line:'.
__LINE__);
}
}
############## End Ecb####################
//example
//require_once 'class.ecb.php';
//new Instance
$geld=new Ecb;
echo '<html><head><title>test</title></head><body>';
#simple example
echo 'für 10 '.Ecb::
getDescription('USD').
' bekommen Sie '.
round($geld->
calculate(10,
'USD',
'GBP'),
2).
' '.Ecb::
getDescription('GBP').
'<br />';
####formular###
echo '<form method="post">';
//betrag
echo 'Betrag: <input type="text" name="betrag" value="1.0" /><br />';
//selects
$sel='<select name="%s">';
foreach(Ecb::getDescription() as $k=>$v)
{
$sel.="<option value=\"{$k}\">{$k} : {$v}</option>";
}
$sel.='</select>';
printf('Von:'.
$sel.
' In:'.
$sel,
'von',
'in');
echo '<br /><input type="submit" /></form>';
if(isset($_POST['von'],
$_POST['in'],
$_POST['betrag']) &&
is_numeric($_POST['betrag']))
{
echo '<h2>für '.
$_POST['betrag'].
' '.Ecb::
getDescription($_POST['von']).
' bekommen Sie '.
round($geld->
calculate($_POST['betrag'],
$_POST['von'],
$_POST['in']),
2).
' '.
Ecb::getDescription($_POST['in']).'</h2>';
}
###########
#teble with rate
echo '<h2>Aktuele Tabelle</h2><table border="1"><tr><th>Kuerzel</th><th>Name</th><th>Euro rate</th></tr>';
foreach($geld->getCurrencieAll() as $geldk=>$value)
{
echo "<tr><td>{$geldk}</td><td>".Ecb::
getDescription($geldk).
"</td><td>{$value}</td></tr>";
}