docs/api/source-system.library.cart.currency.html
| 1: | <?php |
| 2: | namespace Opencart\System\Library\Cart; |
| 3: | /** |
| 4: | * Class Currency |
| 5: | * |
| 6: | * @package Opencart\System\Library\Cart |
| 7: | */ |
| 8: | class Currency { |
| 9: | /** |
| 10: | * @var object |
| 11: | */ |
| 12: | private object $db; |
| 13: | /** |
| 14: | * @var object |
| 15: | */ |
| 16: | private object $language; |
| 17: | /** |
| 18: | * @var array<string, array<string, mixed>> |
| 19: | */ |
| 20: | private array $currencies = []; |
| 21: | |
| 22: | /** |
| 23: | * Constructor |
| 24: | * |
| 25: | * @param \Opencart\System\Engine\Registry $registry |
| 26: | */ |
| 27: | public function __construct(\Opencart\System\Engine\Registry $registry) { |
| 28: | $this->db = $registry->get('db'); |
| 29: | $this->language = $registry->get('language'); |
| 30: | |
| 31: | $query = $this->db->query("SELECT * FROM " . DB\_PREFIX . "currency"); |
| 32: | |
| 33: | foreach ($query->rows as $result) { |
| 34: | $this->currencies[$result['code']] = ['currency_id' => $result['currency_id'], 'title' => $result['title'], 'symbol_left' => $result['symbol_left'], 'symbol_right' => $result['symbol_right'], 'decimal_place' => $result['decimal_place'], 'value' => $result['value']]; |
| 35: | } |
| 36: | } |
| 37: | |
| 38: | /** |
| 39: | * Format |
| 40: | * |
| 41: | * @param float $number |
| 42: | * @param string $currency |
| 43: | * @param float $value |
| 44: | * @param bool $format |
| 45: | * |
| 46: | * @return float|string |
| 47: | */ |
| 48: | public function format(float $number, string $currency, float $value = 0, bool $format = true) { |
| 49: | if (!isset($this->currencies[$currency])) { |
| 50: | return ''; |
| 51: | } |
| 52: | |
| 53: | $symbol_left = $this->currencies[$currency]['symbol_left']; |
| 54: | $symbol_right = $this->currencies[$currency]['symbol_right']; |
| 55: | $decimal_place = $this->currencies[$currency]['decimal_place']; |
| 56: | |
| 57: | if (!$value) { |
| 58: | $value = $this->currencies[$currency]['value']; |
| 59: | } |
| 60: | |
| 61: | $amount = $value ? (float)$number * $value : (float)$number; |
| 62: | |
| 63: | $amount = round($amount, $decimal_place); |
| 64: | |
| 65: | if (!$format) { |
| 66: | return $amount; |
| 67: | } |
| 68: | |
| 69: | $string = ''; |
| 70: | |
| 71: | if ($symbol_left) { |
| 72: | $string .= $symbol_left; |
| 73: | } |
| 74: | |
| 75: | $string .= number_format($amount, $decimal_place, $this->language->get('decimal_point'), $this->language->get('thousand_point')); |
| 76: | |
| 77: | if ($symbol_right) { |
| 78: | $string .= $symbol_right; |
| 79: | } |
| 80: | |
| 81: | return $string; |
| 82: | } |
| 83: | |
| 84: | /** |
| 85: | * Convert |
| 86: | * |
| 87: | * @param float $value |
| 88: | * @param string $from |
| 89: | * @param string $to |
| 90: | * |
| 91: | * @return float |
| 92: | */ |
| 93: | public function convert(float $value, string $from, string $to): float { |
| 94: | if (isset($this->currencies[$from])) { |
| 95: | $from = $this->currencies[$from]['value']; |
| 96: | } else { |
| 97: | $from = 1; |
| 98: | } |
| 99: | |
| 100: | if (isset($this->currencies[$to])) { |
| 101: | $to = $this->currencies[$to]['value']; |
| 102: | } else { |
| 103: | $to = 1; |
| 104: | } |
| 105: | |
| 106: | return $value * ($to / $from); |
| 107: | } |
| 108: | |
| 109: | /** |
| 110: | * getId |
| 111: | * |
| 112: | * @param string $currency |
| 113: | * |
| 114: | * @return int |
| 115: | */ |
| 116: | public function getId(string $currency): int { |
| 117: | if (isset($this->currencies[$currency])) { |
| 118: | return $this->currencies[$currency]['currency_id']; |
| 119: | } else { |
| 120: | return 0; |
| 121: | } |
| 122: | } |
| 123: | |
| 124: | /** |
| 125: | * getSymbolLeft |
| 126: | * |
| 127: | * @param string $currency |
| 128: | * |
| 129: | * @return string |
| 130: | */ |
| 131: | public function getSymbolLeft(string $currency): string { |
| 132: | if (isset($this->currencies[$currency])) { |
| 133: | return $this->currencies[$currency]['symbol_left']; |
| 134: | } else { |
| 135: | return ''; |
| 136: | } |
| 137: | } |
| 138: | |
| 139: | /** |
| 140: | * getSymbolRight |
| 141: | * |
| 142: | * @param string $currency |
| 143: | * |
| 144: | * @return string |
| 145: | */ |
| 146: | public function getSymbolRight(string $currency): string { |
| 147: | if (isset($this->currencies[$currency])) { |
| 148: | return $this->currencies[$currency]['symbol_right']; |
| 149: | } else { |
| 150: | return ''; |
| 151: | } |
| 152: | } |
| 153: | |
| 154: | /** |
| 155: | * getDecimalPlace |
| 156: | * |
| 157: | * @param string $currency |
| 158: | * |
| 159: | * @return int |
| 160: | */ |
| 161: | public function getDecimalPlace(string $currency): int { |
| 162: | if (isset($this->currencies[$currency])) { |
| 163: | return (int)$this->currencies[$currency]['decimal_place']; |
| 164: | } else { |
| 165: | return 0; |
| 166: | } |
| 167: | } |
| 168: | |
| 169: | /** |
| 170: | * getValue |
| 171: | * |
| 172: | * @param string $currency |
| 173: | * |
| 174: | * @return float |
| 175: | */ |
| 176: | public function getValue(string $currency): float { |
| 177: | if (isset($this->currencies[$currency])) { |
| 178: | return $this->currencies[$currency]['value']; |
| 179: | } else { |
| 180: | return 0; |
| 181: | } |
| 182: | } |
| 183: | |
| 184: | /** |
| 185: | * Has |
| 186: | * |
| 187: | * @param string $currency |
| 188: | * |
| 189: | * @return bool |
| 190: | */ |
| 191: | public function has(string $currency): bool { |
| 192: | return isset($this->currencies[$currency]); |
| 193: | } |
| 194: | } |
| 195: | |
OpenCart API API documentation generated by ApiGen dev-master