Source of file EcommerceMoney.php
Size: 5,008 Bytes - Last Modified: 2021-12-23T10:39:35+00:00
/var/www/docs.ssmods.com/process/src/src/Money/EcommerceMoney.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 | <?php namespace Sunnysideup\Ecommerce\Money; use SilverStripe\Core\Extension; use SilverStripe\ORM\FieldType\DBField; use SilverStripe\ORM\FieldType\DBMoney; use Sunnysideup\Ecommerce\Config\EcommerceConfig; class EcommerceMoney extends Extension { /** * @var string */ private static $default_format = 'NiceShortSymbol'; /** * returns the symbol for a currency, e.g. $. * * @param string $currency * * @return string */ public static function get_default_symbol(?string $currency = 'NZD') { $money = DBMoney::create(); $money->setCurrency($currency); return $money->getSymbol(); } /** * returns the short symbol for a currency * This is shorter than the default one. * * @param string $currency * * @return string */ public static function get_short_symbol(?string $currency = 'NZD') { $symbol = self::get_default_symbol($currency); if ($symbol) { $i = 0; while ($i < mb_strlen($symbol) && substr($symbol, $i, 1) === substr($currency, $i, 1)) { ++$i; } return substr($symbol, $i); } return '$'; } /** * returns the long symbol for a currency. * * @param string $currency * * @return string */ public static function get_long_symbol(?string $currency = 'NZD') { $symbol = self::get_default_symbol($currency); if ($symbol && mb_strlen($symbol) < 3) { $symbol = substr($currency, 0, 3 - mb_strlen($symbol)) . $symbol; } return $symbol; } /** * returns the default symbol for a site. * with or without html. * * @param bool $html * * @return string */ public function NiceDefaultSymbol($html = true) { return self::get_default_symbol($this->getOwner()->currency) === self::get_short_symbol($this->getOwner()->currency) ? $this->NiceShortSymbol($html) : $this->NiceLongSymbol($html); } /** * returns the short symbol for a site. * with or without html. * * @param bool $html * * @return string */ public function NiceShortSymbol($html = true) { $symbol = self::get_short_symbol($this->getOwner()->currency); if ($html) { $symbol = "<span class=\"currencyHolder currencyHolderShort currency{$this->getOwner()->currency}\"><span class=\"currencySymbol\">{$symbol}</span></span>"; } $amount = $this->getOwner()->getAmount(); $formatter = $this->getOwner()->getFormatter(); $data = $formatter->format($amount); return DBField::create_field('HTMLText', $data); } /** * returns the long symbol for a site. * with or without html. * * @param bool $html * * @return string */ public function NiceLongSymbol($html = true) { $symbol = self::get_long_symbol($this->getOwner()->currency); $short = self::get_short_symbol($this->getOwner()->currency); $pre = substr($symbol, 0, mb_strlen($symbol) - mb_strlen($short)); if ($html) { $symbol = "<span class=\"currencyHolder currencyHolderLong currency{$this->getOwner()->currency}\"><span class=\"currencyPreSymbol\">{$pre}</span><span class=\"currencySymbol\">{$short}</span></span>"; } else { $symbol = $pre . $short; } $amount = $this->getOwner()->getAmount(); $currency = $this->getOwner()->getCurrency(); $formatter = $this->getOwner()->getFormatter(); $data = $currency ? $symbol . $formatter->formatCurrency($amount, $currency) : $formatter->format($amount); return DBField::create_field('HTMLText', $data); } /** * returns a currency like this: 8,001 usd / 12.12 nzd. * * @param bool $html * * @return string */ public function SymbolNumberAndCode($html = true) { $symbol = self::get_short_symbol($this->getOwner()->currency); if ($html) { $symbol = "<span class=\"currencySymbol\">{$symbol}</span>"; } $code = strtolower($this->getOwner()->currency); if ($html) { $code = "<span class=\"currencyHolder\">{$code}</span>"; } $amount = $this->getOwner()->getAmount(); $data = is_numeric($amount) ? $symbol . $this->getOwner()->currencyLib->toCurrency($amount, [ 'symbol' => '', 'precision' => 0, ]) . ' ' . $code : ''; return DBField::create_field('HTMLText', $data); } /** * returns the default format for a site for currency. * * @param bool $html * * @return string */ public function NiceDefaultFormat($html = true) { $function = EcommerceConfig::get(EcommerceMoney::class, 'default_format'); return $this->getOwner()->{$function}($html); } } |