Skip to content

Commit

Permalink
[13.x] Allow options with formatAmount (#1348)
Browse files Browse the repository at this point in the history
* Allow options with formatAmount

* wip
  • Loading branch information
driesvints authored Apr 22, 2022
1 parent 90deda5 commit bccbfbb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/Cashier.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,19 +135,25 @@ public static function formatCurrencyUsing(callable $callback)
* @param int $amount
* @param string|null $currency
* @param string|null $locale
* @param array $options
* @return string
*/
public static function formatAmount($amount, $currency = null, $locale = null)
public static function formatAmount($amount, $currency = null, $locale = null, array $options = [])
{
if (static::$formatCurrencyUsing) {
return call_user_func(static::$formatCurrencyUsing, $amount, $currency, $locale);
return call_user_func(static::$formatCurrencyUsing, $amount, $currency, $locale, $options);
}

$money = new Money($amount, new Currency(strtoupper($currency ?? config('cashier.currency'))));

$locale = $locale ?? config('cashier.currency_locale');

$numberFormatter = new NumberFormatter($locale, NumberFormatter::CURRENCY);

if (isset($options['min_fraction_digits'])) {
$numberFormatter->setAttribute(NumberFormatter::MIN_FRACTION_DIGITS, $options['min_fraction_digits']);
}

$moneyFormatter = new IntlMoneyFormatter($numberFormatter, new ISOCurrencies());

return $moneyFormatter->format($money);
Expand Down
19 changes: 19 additions & 0 deletions tests/Unit/CashierTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Laravel\Cashier\Tests\Unit;

use Laravel\Cashier\Cashier;
use Laravel\Cashier\Tests\TestCase;

class CashierTest extends TestCase
{
public function test_it_can_format_an_amount()
{
$this->assertSame('$10.00', Cashier::formatAmount(1000));
}

public function test_it_can_format_an_amount_without_digits()
{
$this->assertSame('$10', Cashier::formatAmount(1000, null, null, ['min_fraction_digits' => 0]));
}
}

0 comments on commit bccbfbb

Please sign in to comment.