amount = self::normalize($amount); $this->currency = $currency; $this->position = $position; } public static function normalize(float $amount): float { return round($amount, 2); } public static function decimals(string $currency = ''): int { return in_array(strtoupper($currency), self::ZERO_DECIMAL_CURRENCIES, true) ? 0 : 2; } public static function value(float $amount, string $currency = ''): string { return number_format(self::normalize($amount), self::decimals($currency), '.', ''); } public function setAmount(float $amount): Price { $this->amount = self::normalize($amount); return $this; } public function setCurrency(string $currency): Price { $this->currency = $currency; return $this; } public function setPosition(string $position): Price { $this->position = $position; return $this; } public function byOffering(Offering $offering): Price { $this->setCurrency($offering->getCurrency()); $this->setPosition($offering->getCurrencyPosition()); return $this; } /** * Get Currency Sign * * @param string $currency * @return string */ public static function sign(string $currency): string { $currencies = Currencies::get(); $sign = Arr::truthy($currencies, $currency) ? $currencies[$currency] : $currency; return apply_filters('DayTidy/F__DayTidy/Render/Price__sign/sign', $sign, $currency, $currencies); } public function render(): string { $thousand_separator = ','; $decimal_separator = '.'; // Rendered Price $rendered = number_format($this->amount, 2, $decimal_separator, $thousand_separator); // Currency Sign $sign = $this->sign($this->currency); // Add Currency Sign if ($this->position === Currencies::POSITION_AFTER) $rendered = $rendered . $sign; else if ($this->position == Currencies::POSITION_AFTER_WITH_SPACE) $rendered = $rendered . ' ' . $sign; else if ($this->position == Currencies::POSITION_BEFORE_WITH_SPACE) $rendered = $sign . ' ' . $rendered; else $rendered = $sign . $rendered; // Return Rendered Price return $rendered; } }