<?
/**
* HebCurrency File
* This class handles currencies from the bank of israel xml file
* @author Hadar Porat <hide@address.com>
* @version 1.21
*/
/**
* HebCurrency Class
* @access public
* @author Hadar Porat <hide@address.com>
* @version 1.21
*/
class HebCurrency {
var $xmlFile = 'http://www.bankisrael.gov.il/heb.shearim/currency.php';
var $dom;
var $root;
var $currencies = array();
var $locale = 'en';
var $locales = array (
'he' => array (
'SAL' => 'ñì îèáòåú',
'USD' => 'ãåìø',
'GBP' => 'ìéù"è',
'JPY' => 'ééï',
'EUR' => 'àéøå',
'AUD' => 'ãåìø àåñèøìé',
'CAD' => 'ãåìø ÷ðãé',
'DKK' => 'ëúø ãðîø÷',
'NOK' => 'ëúø ðåøååâéä',
'ZAR' => 'øðã',
'SEK' => 'ëúø ùååãéä',
'CHF' => 'ôøð÷',
'JOD' => 'ãéðø',
'LBP' => 'ìéøä ìáðåðéú',
'EGP' => 'ìéøä îöøéú',
)
);
/**
* @desc HebCurrency main fuction
* @param Locale $locale locale code
* @return void
*/
function HebCurrency($locale = 'en') {
// check for XML module
if (!function_exists('domxml_open_file')) {
$this -> errorMessage("DOM XML module not found");
}
// open the file
if (!$this -> domFile = domxml_open_file($this -> xmlFile)) {
$this -> errorMessage("Error while parsing the document");
}
// set locale
$this -> locale = $locale;
$elements = $this -> domFile -> get_elements_by_tagname("CURRENCY");
while (list($key, $value) = each($elements)) {
$array = array();
$arrayName = '';
$child = $value->first_child();
while ($child) {
if ($child -> node_name()=='NAME') $array['name'] = $child -> get_content();
else if ($child -> node_name()=='RATE') $array['rate'] = $child -> get_content();
else if ($child -> node_name()=='UNIT') $array['unit'] = $child -> get_content();
else if ($child -> node_name()=='COUNTRY') $array['country'] = $child -> get_content();
else if ($child -> node_name()=='CURRENCYCODE') $arrayName = $child -> get_content();
$child = $child->next_sibling();
}
// locale
if ($this -> locale<>'en') $array['name'] = $this -> locales[$this -> locale][$arrayName];
// add the item to main array
$this -> currencies[$arrayName] = $array;
}
}
/**
* @desc returns last update
* @return string
*/
function getLastUpdate() {
$elements = $this -> domFile -> get_elements_by_tagname("LAST_UPDATE");
$element = $elements[0];
return $element -> get_content();
}
/**
* @desc get currency by code
* @param string $code currency code
* @return array
*/
function getCurrency($code = 'USD') {
if ($this -> isCurrency($code)) return $this -> currencies[$code];
else return false;
}
/**
* @desc converts currency to NIS
* @param string $code currency code
* @param double $value money value
* @return double
*/
function convertFrom($code = 'USD', $value) {
if ($this -> isCurrency($code)) return ($this -> currencies[$code]['rate'] * $value) / $this -> currencies[$code]['unit'] ;
else return false;
}
/**
* @desc converts NIS to other currency
* @param string $code currency code
* @param double $value money value
* @return double
*/
function convertTo($code = 'USD', $value) {
if ($this -> isCurrency($code)) return ($value / $this -> currencies[$code]['rate']) * $this -> currencies[$code]['unit'] ;
else return false;
}
/**
* @desc finds out if currency exists
* @param string $code currency code
* @return boolean
*/
function isCurrency($code = 'USD') {
if (isset($this -> currencies[$code]) and (is_array($this -> currencies[$code]))) return true;
else return false;
}
/**
* @desc get all currencies
* @return array
*/
function getAll() {
return $this -> currencies;
}
/**
* @desc prints error message
* @param string $message error message
* @return void
*/
function errorMessage($message) {
echo '<b>FATAL ERROR:</b> ' . $message;
exit;
}
}