1: <?php
2: namespace Opencart\System\Library\Cart;
3: /**
4: * Class Weight
5: *
6: * @package Opencart\System\Library\Cart
7: */
8: class Weight {
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 $weights = [];
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: $weight_class_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "weight_class` `wc` LEFT JOIN `" . DB_PREFIX . "weight_class_description` `wcd` ON (`wc`.`weight_class_id` = `wcd`.`weight_class_id`) WHERE `wcd`.`language_id` = '" . (int)$this->config->get('config_language_id') . "'");
32:
33: foreach ($weight_class_query->rows as $result) {
34: $this->weights[$result['weight_class_id']] = [
35: 'weight_class_id' => $result['weight_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->weights[$from])) {
58: $from = $this->weights[$from]['value'];
59: } else {
60: $from = 1;
61: }
62:
63: if (isset($this->weights[$to])) {
64: $to = $this->weights[$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 $weight_class_id
77: * @param string $decimal_point
78: * @param string $thousand_point
79: *
80: * @return string
81: */
82: public function format(float $value, int $weight_class_id, string $decimal_point = '.', string $thousand_point = ','): string {
83: if (isset($this->weights[$weight_class_id])) {
84: return number_format($value, 2, $decimal_point, $thousand_point) . $this->weights[$weight_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 $weight_class_id
94: *
95: * @return string
96: */
97: public function getUnit(int $weight_class_id): string {
98: if (isset($this->weights[$weight_class_id])) {
99: return $this->weights[$weight_class_id]['unit'];
100: } else {
101: return '';
102: }
103: }
104: }
105: