shop.balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

currency.php (2813B)


      1 <?php
      2 namespace Cart;
      3 class Currency {
      4 	private $currencies = array();
      5 
      6 	public function __construct($registry) {
      7 		$this->db = $registry->get('db');
      8 		$this->language = $registry->get('language');
      9 
     10 		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "currency");
     11 
     12 		foreach ($query->rows as $result) {
     13 			$this->currencies[$result['code']] = array(
     14 				'currency_id'   => $result['currency_id'],
     15 				'title'         => $result['title'],
     16 				'symbol_left'   => $result['symbol_left'],
     17 				'symbol_right'  => $result['symbol_right'],
     18 				'decimal_place' => $result['decimal_place'],
     19 				'value'         => $result['value']
     20 			);
     21 		}
     22 	}
     23 
     24 	public function format($number, $currency, $value = '', $format = true) {
     25 		$symbol_left = $this->currencies[$currency]['symbol_left'];
     26 		$symbol_right = $this->currencies[$currency]['symbol_right'];
     27 		$decimal_place = $this->currencies[$currency]['decimal_place'];
     28 
     29 		if (!$value) {
     30 			$value = $this->currencies[$currency]['value'];
     31 		}
     32 
     33 		$amount = $value ? (float)$number * $value : (float)$number;
     34 		
     35 		$amount = round($amount, (int)$decimal_place);
     36 		
     37 		if (!$format) {
     38 			return $amount;
     39 		}
     40 
     41 		$string = '';
     42 
     43 		if ($symbol_left) {
     44 			$string .= $symbol_left;
     45 		}
     46 
     47 		$string .= number_format($amount, (int)$decimal_place, $this->language->get('decimal_point'), $this->language->get('thousand_point'));
     48 
     49 		if ($symbol_right) {
     50 			$string .= $symbol_right;
     51 		}
     52 
     53 		return $string;
     54 	}
     55 
     56 	public function convert($value, $from, $to) {
     57 		if (isset($this->currencies[$from])) {
     58 			$from = $this->currencies[$from]['value'];
     59 		} else {
     60 			$from = 1;
     61 		}
     62 
     63 		if (isset($this->currencies[$to])) {
     64 			$to = $this->currencies[$to]['value'];
     65 		} else {
     66 			$to = 1;
     67 		}
     68 
     69 		return $value * ($to / $from);
     70 	}
     71 	
     72 	public function getId($currency) {
     73 		if (isset($this->currencies[$currency])) {
     74 			return $this->currencies[$currency]['currency_id'];
     75 		} else {
     76 			return 0;
     77 		}
     78 	}
     79 
     80 	public function getSymbolLeft($currency) {
     81 		if (isset($this->currencies[$currency])) {
     82 			return $this->currencies[$currency]['symbol_left'];
     83 		} else {
     84 			return '';
     85 		}
     86 	}
     87 
     88 	public function getSymbolRight($currency) {
     89 		if (isset($this->currencies[$currency])) {
     90 			return $this->currencies[$currency]['symbol_right'];
     91 		} else {
     92 			return '';
     93 		}
     94 	}
     95 
     96 	public function getDecimalPlace($currency) {
     97 		if (isset($this->currencies[$currency])) {
     98 			return $this->currencies[$currency]['decimal_place'];
     99 		} else {
    100 			return 0;
    101 		}
    102 	}
    103 
    104 	public function getValue($currency) {
    105 		if (isset($this->currencies[$currency])) {
    106 			return $this->currencies[$currency]['value'];
    107 		} else {
    108 			return 0;
    109 		}
    110 	}
    111 
    112 	public function has($currency) {
    113 		return isset($this->currencies[$currency]);
    114 	}
    115 }