Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[13.x] Allow options with formatAmount #1348

Merged
merged 2 commits into from
Apr 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]));
}
}