5 use Brick\Money\Currency
;
7 use Brick\Math\RoundingMode
;
11 use CRM_Utils_Constant
;
13 use Brick\Money\Context\AutoContext
;
19 * This class provides standardised formatting
26 * @param string $amount
27 * @param string|null $currency
28 * Currency, defaults to site currency if not provided.
29 * @param string|null $locale
33 * @noinspection PhpDocMissingThrowsInspection
34 * @noinspection PhpUnhandledExceptionInspection
36 public function money(string $amount, ?
string $currency = NULL, ?
string $locale = NULL): string {
38 $currency = Civi
::settings()->get('defaultCurrency');
40 if (!isset($locale)) {
41 $locale = CRM_Core_I18n
::getLocale();
43 $money = Money
::of($amount, $currency, NULL, RoundingMode
::HALF_UP
);
44 $formatter = $this->getMoneyFormatter($currency, $locale);
45 return $money->formatWith($formatter);
49 * Get a formatted number.
51 * @param string|int|float|Money $amount
52 * Amount in a machine money format.
53 * @param string|null $locale
54 * @param array $attributes
55 * Additional values supported by NumberFormatter
56 * https://www.php.net/manual/en/class.numberformatter.php
57 * By default this will set it to round to 8 places and not
62 public function number($amount, ?
string $locale = NULL, array $attributes = [
63 NumberFormatter
::MIN_FRACTION_DIGITS
=> 0,
64 NumberFormatter
::MAX_FRACTION_DIGITS
=> 8,
66 $formatter = $this->getMoneyFormatter(NULL, $locale, NumberFormatter
::DECIMAL
, $attributes);
67 return $formatter->format($amount);
71 * Get a number formatted with rounding expectations derived from the currency.
73 * @param string|float|int $amount
74 * @param string $currency
79 * @noinspection PhpDocMissingThrowsInspection
80 * @noinspection PhpUnhandledExceptionInspection
82 public function moneyNumber($amount, string $currency, $locale): string {
83 $formatter = $this->getMoneyFormatter($currency, $locale, NumberFormatter
::DECIMAL
);
84 $money = Money
::of($amount, $currency, NULL, RoundingMode
::HALF_UP
);
85 return $money->formatWith($formatter);
89 * Get a money value with formatting but not rounding.
91 * @param string|float|int $amount
92 * @param string|null $currency
93 * @param string|null $locale
97 * @noinspection PhpDocMissingThrowsInspection
98 * @noinspection PhpUnhandledExceptionInspection
100 public function moneyLong($amount, ?
string $currency, ?
string $locale): string {
101 $formatter = $this->getMoneyFormatter($currency, $locale, NumberFormatter
::CURRENCY
, [
102 NumberFormatter
::MAX_FRACTION_DIGITS
=> 9,
104 $money = Money
::of($amount, $currency, new AutoContext());
105 return $money->formatWith($formatter);
109 * Get a number with minimum decimal places based on the currency but no rounding.
111 * @param string|float|int $amount
112 * @param string|null $currency
113 * @param string|null $locale
117 * @noinspection PhpDocMissingThrowsInspection
118 * @noinspection PhpUnhandledExceptionInspection
120 public function moneyNumberLong($amount, ?
string $currency, ?
string $locale): string {
121 $formatter = $this->getMoneyFormatter($currency, $locale, NumberFormatter
::DECIMAL
, [
122 NumberFormatter
::MAX_FRACTION_DIGITS
=> 9,
124 $money = Money
::of($amount, $currency, new AutoContext());
125 return $money->formatWith($formatter);
129 * Should we use the configured thousand & decimal separators.
131 * The goal is to phase this into being FALSE - but for now
132 * we are looking at how to manage an 'opt in'
134 protected function isUseSeparatorSettings(): bool {
135 return !CRM_Utils_Constant
::value('IGNORE_SEPARATOR_CONFIG');
139 * Get the money formatter for when we are using configured thousand separators.
141 * Our intent is to phase out these settings in favour of deriving them from the locale.
143 * @param string|null $currency
144 * @param string|null $locale
146 * See https://www.php.net/manual/en/class.numberformatter.php#intl.numberformatter-constants
147 * @param array $attributes
148 * See https://www.php.net/manual/en/class.numberformatter.php#intl.numberformatter-constants.unumberformatattribute
150 * @return \NumberFormatter
152 * @noinspection PhpDocMissingThrowsInspection
153 * @noinspection PhpUnhandledExceptionInspection
155 public function getMoneyFormatter(?
string $currency = NULL, ?
string $locale = NULL, int $style = NumberFormatter
::CURRENCY
, array $attributes = []): NumberFormatter
{
157 $currency = Civi
::settings()->get('defaultCurrency');
159 $cacheKey = __CLASS__
. $currency . '_' . $locale . '_' . $style . (!empty($attributes) ?
md5(json_encode($attributes)) : '');
160 if (!isset(\Civi
::$statics[$cacheKey])) {
161 $formatter = new NumberFormatter($locale, $style);
163 if (!isset($attributes[NumberFormatter
::MIN_FRACTION_DIGITS
])) {
164 $attributes[NumberFormatter
::MIN_FRACTION_DIGITS
] = Currency
::of($currency)
165 ->getDefaultFractionDigits();
167 if (!isset($attributes[NumberFormatter
::MAX_FRACTION_DIGITS
])) {
168 $attributes[NumberFormatter
::MAX_FRACTION_DIGITS
] = Currency
::of($currency)
169 ->getDefaultFractionDigits();
172 foreach ($attributes as $attribute => $value) {
173 $formatter->setAttribute($attribute, $value);
175 if ($locale === CRM_Core_I18n
::getLocale() && $this->isUseSeparatorSettings()) {
176 $formatter->setSymbol(NumberFormatter
::MONETARY_GROUPING_SEPARATOR_SYMBOL
, CRM_Core_Config
::singleton()->monetaryThousandSeparator
);
177 $formatter->setSymbol(NumberFormatter
::MONETARY_SEPARATOR_SYMBOL
, CRM_Core_Config
::singleton()->monetaryDecimalPoint
);
179 \Civi
::$statics[$cacheKey] = $formatter;
181 return \Civi
::$statics[$cacheKey];