CurrencyFacade.php
101 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Framework\Support\Facades; |
| 4 | |
| 5 | use Money\Converter; |
| 6 | use Money\Currencies\ISOCurrencies; |
| 7 | use Money\Currency; |
| 8 | use Money\Exchange\FixedExchange; |
| 9 | use Money\Formatter\DecimalMoneyFormatter; |
| 10 | use Money\Formatter\IntlMoneyFormatter; |
| 11 | use Money\Money; |
| 12 | use Money\Parser\DecimalMoneyParser; |
| 13 | use NumberFormatter; |
| 14 | |
| 15 | class CurrencyFacade |
| 16 | { |
| 17 | /** |
| 18 | * Immutably converts the given amount into the system currency. |
| 19 | * |
| 20 | * @since 2.20.0 |
| 21 | * |
| 22 | * @param Money $amount |
| 23 | * @param int|float $exchangeRate |
| 24 | */ |
| 25 | public function convertToBaseCurrency(Money $amount, $exchangeRate = 1): Money |
| 26 | { |
| 27 | $baseCurrency = $this->getBaseCurrency(); |
| 28 | |
| 29 | if ($amount->getCurrency()->equals($baseCurrency)) { |
| 30 | return $amount; |
| 31 | } |
| 32 | |
| 33 | $converter = new Converter( |
| 34 | new ISOCurrencies(), new FixedExchange([ |
| 35 | $amount->getCurrency()->getCode() => [ |
| 36 | give_get_option('currency', 'USD') => $exchangeRate, |
| 37 | ], |
| 38 | ]) |
| 39 | ); |
| 40 | |
| 41 | return $converter->convert($amount, $baseCurrency); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Creates a new Money instance from a decimal amount |
| 46 | * |
| 47 | * @since 2.20.0 |
| 48 | * |
| 49 | * @param string|float|int $amount |
| 50 | */ |
| 51 | public function parseFromDecimal($amount, string $currency): Money |
| 52 | { |
| 53 | return (new DecimalMoneyParser(new ISOCurrencies()))->parse((string)$amount, new Currency($currency)); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Returns the amount in a decimal format, not including any currency symbols: |
| 58 | * - $1,500.25 -> 1500.25 |
| 59 | * |
| 60 | * @since 2.20.0 |
| 61 | */ |
| 62 | public function formatToDecimal(Money $amount): string |
| 63 | { |
| 64 | return (new DecimalMoneyFormatter(new ISOCurrencies()))->format($amount); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Formats the amount to a currency format, including currency symbols, in the given locale. |
| 69 | * |
| 70 | * @since 2.20.0 |
| 71 | * |
| 72 | * @param Money $amount |
| 73 | * @param string|null $locale |
| 74 | * |
| 75 | * @return string |
| 76 | */ |
| 77 | public function formatToLocale(Money $amount, $locale = null): string |
| 78 | { |
| 79 | if ($locale === null) { |
| 80 | $locale = get_locale(); |
| 81 | } |
| 82 | |
| 83 | $numberFormatter = new NumberFormatter($locale, NumberFormatter::CURRENCY); |
| 84 | $moneyFormatter = new IntlMoneyFormatter($numberFormatter, new ISOCurrencies()); |
| 85 | |
| 86 | return $moneyFormatter->format($amount); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Retrieves the system's base currency |
| 91 | * |
| 92 | * @since 2.20.0 |
| 93 | * |
| 94 | * @return Currency |
| 95 | */ |
| 96 | public function getBaseCurrency(): Currency |
| 97 | { |
| 98 | return new Currency(give_get_option('currency', 'USD')); |
| 99 | } |
| 100 | } |
| 101 |