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|int|float $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($amount, ?
string $currency = NULL, ?
string $locale = NULL): string {
37 // Empty value => empty string
38 if (is_null($amount) ||
$amount === '' ||
$amount === FALSE) {
41 // Verify the amount is a number or numeric string/object
42 elseif ($amount === TRUE ||
!is_numeric((string) $amount)) {
43 throw new \
CRM_Core_Exception('Invalid value for type money');
46 $currency = Civi
::settings()->get('defaultCurrency');
48 if (!isset($locale)) {
49 $locale = CRM_Core_I18n
::getLocale();
51 $money = Money
::of($amount, $currency, NULL, RoundingMode
::HALF_UP
);
52 $formatter = $this->getMoneyFormatter($currency, $locale);
53 return $money->formatWith($formatter);
57 * Get a formatted number.
59 * @param string|int|float|Money $amount
60 * Amount in a machine money format.
61 * @param string|null $locale
62 * @param array $attributes
63 * Additional values supported by NumberFormatter
64 * https://www.php.net/manual/en/class.numberformatter.php
65 * By default this will set it to round to 8 places and not
70 public function number($amount, ?
string $locale = NULL, array $attributes = [
71 NumberFormatter
::MIN_FRACTION_DIGITS
=> 0,
72 NumberFormatter
::MAX_FRACTION_DIGITS
=> 8,
74 $formatter = $this->getMoneyFormatter(NULL, $locale, NumberFormatter
::DECIMAL
, $attributes);
75 return $formatter->format($amount);
79 * Get a number formatted with rounding expectations derived from the currency.
81 * @param string|float|int $amount
82 * @param string $currency
87 * @noinspection PhpDocMissingThrowsInspection
88 * @noinspection PhpUnhandledExceptionInspection
90 public function moneyNumber($amount, string $currency, $locale): string {
91 $formatter = $this->getMoneyFormatter($currency, $locale, NumberFormatter
::DECIMAL
);
92 $money = Money
::of($amount, $currency, NULL, RoundingMode
::HALF_UP
);
93 return $money->formatWith($formatter);
97 * Get a money value with formatting but not rounding.
99 * @param string|float|int $amount
100 * @param string|null $currency
101 * @param string|null $locale
105 * @noinspection PhpDocMissingThrowsInspection
106 * @noinspection PhpUnhandledExceptionInspection
108 public function moneyLong($amount, ?
string $currency, ?
string $locale): string {
109 $formatter = $this->getMoneyFormatter($currency, $locale, NumberFormatter
::CURRENCY
, [
110 NumberFormatter
::MAX_FRACTION_DIGITS
=> 9,
112 $money = Money
::of($amount, $currency, new AutoContext());
113 return $money->formatWith($formatter);
117 * Get a number with minimum decimal places based on the currency but no rounding.
119 * @param string|float|int $amount
120 * @param string|null $currency
121 * @param string|null $locale
125 * @noinspection PhpDocMissingThrowsInspection
126 * @noinspection PhpUnhandledExceptionInspection
128 public function moneyNumberLong($amount, ?
string $currency, ?
string $locale): string {
129 $formatter = $this->getMoneyFormatter($currency, $locale, NumberFormatter
::DECIMAL
, [
130 NumberFormatter
::MAX_FRACTION_DIGITS
=> 9,
132 $money = Money
::of($amount, $currency, new AutoContext());
133 return $money->formatWith($formatter);
137 * Should we use the configured thousand & decimal separators.
139 * The goal is to phase this into being FALSE - but for now
140 * we are looking at how to manage an 'opt in'
142 protected function isUseSeparatorSettings(): bool {
143 return !CRM_Utils_Constant
::value('IGNORE_SEPARATOR_CONFIG');
147 * Get the money formatter for when we are using configured thousand separators.
149 * Our intent is to phase out these settings in favour of deriving them from the locale.
151 * @param string|null $currency
152 * @param string|null $locale
154 * See https://www.php.net/manual/en/class.numberformatter.php#intl.numberformatter-constants
155 * @param array $attributes
156 * See https://www.php.net/manual/en/class.numberformatter.php#intl.numberformatter-constants.unumberformatattribute
158 * @return \NumberFormatter
160 * @noinspection PhpDocMissingThrowsInspection
161 * @noinspection PhpUnhandledExceptionInspection
163 public function getMoneyFormatter(?
string $currency = NULL, ?
string $locale = NULL, int $style = NumberFormatter
::CURRENCY
, array $attributes = []): NumberFormatter
{
165 $currency = Civi
::settings()->get('defaultCurrency');
167 $cacheKey = __CLASS__
. $currency . '_' . $locale . '_' . $style . (!empty($attributes) ?
md5(json_encode($attributes)) : '');
168 if (!isset(\Civi
::$statics[$cacheKey])) {
169 $formatter = new NumberFormatter($locale, $style);
171 if (!isset($attributes[NumberFormatter
::MIN_FRACTION_DIGITS
])) {
172 $attributes[NumberFormatter
::MIN_FRACTION_DIGITS
] = Currency
::of($currency)
173 ->getDefaultFractionDigits();
175 if (!isset($attributes[NumberFormatter
::MAX_FRACTION_DIGITS
])) {
176 $attributes[NumberFormatter
::MAX_FRACTION_DIGITS
] = Currency
::of($currency)
177 ->getDefaultFractionDigits();
180 foreach ($attributes as $attribute => $value) {
181 $formatter->setAttribute($attribute, $value);
183 if ($locale === CRM_Core_I18n
::getLocale() && $this->isUseSeparatorSettings()) {
184 $formatter->setSymbol(NumberFormatter
::MONETARY_GROUPING_SEPARATOR_SYMBOL
, CRM_Core_Config
::singleton()->monetaryThousandSeparator
);
185 $formatter->setSymbol(NumberFormatter
::MONETARY_SEPARATOR_SYMBOL
, CRM_Core_Config
::singleton()->monetaryDecimalPoint
);
187 \Civi
::$statics[$cacheKey] = $formatter;
189 return \Civi
::$statics[$cacheKey];