1: | <?php
|
2: | namespace Opencart\System\Library\Cart;
|
3: | |
4: | |
5: | |
6: | |
7: |
|
8: | class Length {
|
9: | |
10: | |
11: |
|
12: | private object $db;
|
13: | |
14: | |
15: |
|
16: | private object $config;
|
17: | |
18: | |
19: |
|
20: | private array $lengths = [];
|
21: |
|
22: | |
23: | |
24: | |
25: | |
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: | |
45: | |
46: | |
47: | |
48: | |
49: | |
50: | |
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: | |
74: | |
75: | |
76: | |
77: | |
78: | |
79: | |
80: | |
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: | |
92: | |
93: | |
94: | |
95: | |
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: | |