Skip to content

Commit 2747a5f

Browse files
[11.x] feat: introduce option to change default Number currency (#53022)
* feat: add perWeek option to Limit * feat: add perWeek option to Limit * feat: add perMonth option to Limit * feat: add perMonth option to Limit * revert * feat: introduce option to set default currency and override it * feat: introduce option to set default currency and override it
1 parent d9bb513 commit 2747a5f

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

src/Illuminate/Support/Number.php

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ class Number
1717
*/
1818
protected static $locale = 'en';
1919

20+
/**
21+
* The current default currency.
22+
*
23+
* @var string
24+
*/
25+
protected static $currency = 'USD';
26+
2027
/**
2128
* Format the given number according to the current locale.
2229
*
@@ -115,13 +122,13 @@ public static function percentage(int|float $number, int $precision = 0, ?int $m
115122
* @param string|null $locale
116123
* @return string|false
117124
*/
118-
public static function currency(int|float $number, string $in = 'USD', ?string $locale = null)
125+
public static function currency(int|float $number, string $in = '', ?string $locale = null)
119126
{
120127
static::ensureIntlExtensionIsInstalled();
121128

122129
$formatter = new NumberFormatter($locale ?? static::$locale, NumberFormatter::CURRENCY);
123130

124-
return $formatter->formatCurrency($number, $in);
131+
return $formatter->formatCurrency($number, ! empty($in) ? $in : static::$currency);
125132
}
126133

127134
/**
@@ -284,6 +291,22 @@ public static function withLocale(string $locale, callable $callback)
284291
return tap($callback(), fn () => static::useLocale($previousLocale));
285292
}
286293

294+
/**
295+
* Execute the given callback using the given currency.
296+
*
297+
* @param string $currency
298+
* @param callable $callback
299+
* @return mixed
300+
*/
301+
public static function withCurrency(string $currency, callable $callback)
302+
{
303+
$previousLCurrency = static::$currency;
304+
305+
static::useCurrency($currency);
306+
307+
return tap($callback(), fn () => static::useCurrency($previousLCurrency));
308+
}
309+
287310
/**
288311
* Set the default locale.
289312
*
@@ -295,6 +318,17 @@ public static function useLocale(string $locale)
295318
static::$locale = $locale;
296319
}
297320

321+
/**
322+
* Set the default currency.
323+
*
324+
* @param string $currency
325+
* @return void
326+
*/
327+
public static function useCurrency(string $currency)
328+
{
329+
static::$currency = $currency;
330+
}
331+
298332
/**
299333
* Ensure the "intl" PHP extension is installed.
300334
*

0 commit comments

Comments
 (0)