docs/api/source-system.library.cart.length.html
| 1: | <?php |
| 2: | namespace Opencart\System\Library\Cart; |
| 3: | /** |
| 4: | * Class Length |
| 5: | * |
| 6: | * @package Opencart\System\Library\Cart |
| 7: | */ |
| 8: | class Length { |
| 9: | /** |
| 10: | * @var object |
| 11: | */ |
| 12: | private object $db; |
| 13: | /** |
| 14: | * @var object |
| 15: | */ |
| 16: | private object $config; |
| 17: | /** |
| 18: | * @var array<int, array<string, mixed>> |
| 19: | */ |
| 20: | private array $lengths = []; |
| 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->config = $registry->get('config'); |
| 30: | |
| 31: | $length_class_query = $this->db->query("SELECT * FROM " . DB\_PREFIX . "length\_class mc LEFT JOIN " . DB\_PREFIX . "length\_class\_description mcd ON (mc.length\_class\_id = mcd.length\_class\_id) WHERE mcd.language\_id = '" . (int)$this->config->get('config_language_id') . "'"); |
| 32: | |
| 33: | foreach ($length_class_query->rows as $result) { |
| 34: | $this->lengths[$result['length_class_id']] = [ |
| 35: | 'length_class_id' => $result['length_class_id'], |
| 36: | 'title' => $result['title'], |
| 37: | 'unit' => $result['unit'], |
| 38: | 'value' => $result['value'] |
| 39: | ]; |
| 40: | } |
| 41: | } |
| 42: | |
| 43: | /** |
| 44: | * Convert |
| 45: | * |
| 46: | * @param float $value |
| 47: | * @param int $from |
| 48: | * @param int $to |
| 49: | * |
| 50: | * @return float |
| 51: | */ |
| 52: | public function convert(float $value, int $from, int $to): float { |
| 53: | if ($from == $to) { |
| 54: | return $value; |
| 55: | } |
| 56: | |
| 57: | if (isset($this->lengths[$from])) { |
| 58: | $from = $this->lengths[$from]['value']; |
| 59: | } else { |
| 60: | $from = 1; |
| 61: | } |
| 62: | |
| 63: | if (isset($this->lengths[$to])) { |
| 64: | $to = $this->lengths[$to]['value']; |
| 65: | } else { |
| 66: | $to = 1; |
| 67: | } |
| 68: | |
| 69: | return $value * ($to / $from); |
| 70: | } |
| 71: | |
| 72: | /** |
| 73: | * Format |
| 74: | * |
| 75: | * @param float $value |
| 76: | * @param int $length_class_id |
| 77: | * @param string $decimal_point |
| 78: | * @param string $thousand_point |
| 79: | * |
| 80: | * @return string |
| 81: | */ |
| 82: | public function format(float $value, int $length_class_id, string $decimal_point = '.', string $thousand_point = ','): string { |
| 83: | if (isset($this->lengths[$length_class_id])) { |
| 84: | return number_format($value, 2, $decimal_point, $thousand_point) . $this->lengths[$length_class_id]['unit']; |
| 85: | } else { |
| 86: | return number_format($value, 2, $decimal_point, $thousand_point); |
| 87: | } |
| 88: | } |
| 89: | |
| 90: | /** |
| 91: | * getUnit |
| 92: | * |
| 93: | * @param int $length_class_id |
| 94: | * |
| 95: | * @return string |
| 96: | */ |
| 97: | public function getUnit(int $length_class_id): string { |
| 98: | if (isset($this->lengths[$length_class_id])) { |
| 99: | return $this->lengths[$length_class_id]['unit']; |
| 100: | } else { |
| 101: | return ''; |
| 102: | } |
| 103: | } |
| 104: | } |
| 105: | |
OpenCart API API documentation generated by ApiGen dev-master