CurrencyFacade.php
117 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Framework\Support\Facades; |
| 4 | |
| 5 | use Give\Log\Log; |
| 6 | use Money\Converter; |
| 7 | use Money\Currencies\ISOCurrencies; |
| 8 | use Money\Currency; |
| 9 | use Money\Exchange\FixedExchange; |
| 10 | use Money\Formatter\DecimalMoneyFormatter; |
| 11 | use Money\Formatter\IntlMoneyFormatter; |
| 12 | use Money\Money; |
| 13 | use Money\Parser\DecimalMoneyParser; |
| 14 | use NumberFormatter; |
| 15 | |
| 16 | class CurrencyFacade |
| 17 | { |
| 18 | /** |
| 19 | * Immutably converts the given amount into the system currency. |
| 20 | * |
| 21 | * @since 2.20.0 |
| 22 | * |
| 23 | * @param Money $amount |
| 24 | * @param int|float $exchangeRate |
| 25 | */ |
| 26 | public function convertToBaseCurrency(Money $amount, $exchangeRate = 1): Money |
| 27 | { |
| 28 | $baseCurrency = $this->getBaseCurrency(); |
| 29 | |
| 30 | if ($amount->getCurrency()->equals($baseCurrency)) { |
| 31 | return $amount; |
| 32 | } |
| 33 | |
| 34 | $converter = new Converter( |
| 35 | new ISOCurrencies(), new FixedExchange([ |
| 36 | $amount->getCurrency()->getCode() => [ |
| 37 | give_get_option('currency', 'USD') => $exchangeRate, |
| 38 | ], |
| 39 | ]) |
| 40 | ); |
| 41 | |
| 42 | return $converter->convert($amount, $baseCurrency); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Creates a new Money instance from a decimal amount |
| 47 | * |
| 48 | * @since 2.20.0 |
| 49 | * |
| 50 | * @param string|float|int $amount |
| 51 | */ |
| 52 | public function parseFromDecimal($amount, string $currency): Money |
| 53 | { |
| 54 | return (new DecimalMoneyParser(new ISOCurrencies()))->parse((string)$amount, new Currency($currency)); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Returns the amount in a decimal format, not including any currency symbols: |
| 59 | * - $1,500.25 -> 1500.25 |
| 60 | * |
| 61 | * @since 2.20.0 |
| 62 | */ |
| 63 | public function formatToDecimal(Money $amount): string |
| 64 | { |
| 65 | return (new DecimalMoneyFormatter(new ISOCurrencies()))->format($amount); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Formats the amount to a currency format, including currency symbols, in the given locale. |
| 70 | * |
| 71 | * @since 2.24.2 fallback on give formatting system if intl extension is not available |
| 72 | * @since 2.20.0 |
| 73 | * |
| 74 | * @param Money $amount |
| 75 | * @param string|null $locale |
| 76 | * |
| 77 | * @return string |
| 78 | */ |
| 79 | public function formatToLocale(Money $amount, $locale = null): string |
| 80 | { |
| 81 | $useAutoFormatting = give_get_option('auto_format_currency'); |
| 82 | if (!class_exists(NumberFormatter::class) || !$useAutoFormatting) { |
| 83 | if ($useAutoFormatting) { |
| 84 | Log::warning( |
| 85 | 'Auto-formatting is enabled at Donations > Settings > General > Currency but the INTL extension for PHP is not available. Please install the INTL extension to enable auto-formatting, or disable the Auto-formatting setting to prevent this error message. Most web hosts can help with installing and activating INTL. GiveWP is falling back to formatting based on the legacy settings.' |
| 86 | ); |
| 87 | } |
| 88 | |
| 89 | return give_currency_filter( |
| 90 | $this->formatToDecimal($amount), |
| 91 | ['currency' => $amount->getCurrency()->getCode()] |
| 92 | ); |
| 93 | } |
| 94 | |
| 95 | if ($locale === null) { |
| 96 | $locale = get_locale(); |
| 97 | } |
| 98 | |
| 99 | $numberFormatter = new NumberFormatter($locale, NumberFormatter::CURRENCY); |
| 100 | $moneyFormatter = new IntlMoneyFormatter($numberFormatter, new ISOCurrencies()); |
| 101 | |
| 102 | return $moneyFormatter->format($amount); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Retrieves the system's base currency |
| 107 | * |
| 108 | * @since 2.20.0 |
| 109 | * |
| 110 | * @return Currency |
| 111 | */ |
| 112 | public function getBaseCurrency(): Currency |
| 113 | { |
| 114 | return new Currency(give_get_option('currency', 'USD')); |
| 115 | } |
| 116 | } |
| 117 |