. * */ namespace Vvveb\System\Cart; use Vvveb\Sql\Weight_typeSQL; use Vvveb\System\Cache; class Weight { private $weightRates = []; public static function getInstance($options = []) { static $inst = null; if ($inst === null) { $inst = new Weight($options); } return $inst; } public function __construct($options = []) { $this->setWeight(); } public function setWeight() { $cache = Cache::getInstance(); $weights = $cache->cache(APP,'weight_type' ,function () { $weightType = new Weight_typeSQL(); $weights = $weightType->getAll(['start' => 0, 'limit' => 100]); return $weights['weight_type'] ?? []; }, 259200); } public function clear() { $this->weightRates = []; } public function convert($value, $from, $to) : float { if ($from == $to) { return $value; } if (isset($this->weightRates[$from])) { $from = $this->weightRates[$from]['value']; } else { $from = 1; } if (isset($this->weightRates[$to])) { $to = $this->weightRates[$to]['value']; } else { $to = 1; } return $value * ($to / $from); } public function format($value, $weightTypeId, $decimalPoint = '.', $thousandPoint = ',') { $unit = $this->weightRates[$weightTypeId]['unit'] ?? ''; return number_format($value, 2, $decimalPoint, $thousandPoint) . $unit; } public function getUnit($weightTypeId) { $this->weightRates[$weightTypeId]['unit'] ?? ''; } }