feat(helpers): membuat fungsi baru yaitu formatDiscount dan menyesuaikan paramter fungsi StringHelper
This commit is contained in:
parent
2b2ed20cbf
commit
f701fdc70a
@ -1,18 +1,27 @@
|
||||
<?php
|
||||
|
||||
use App\Enums\VoucherType;
|
||||
use Illuminate\Support\Number;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
if (! function_exists('currency')) {
|
||||
function currency(string $value, ?string $currency = null, ?string $locale = 'id_ID'): string
|
||||
function currency(?string $value = null, ?string $currency = null, ?string $locale = 'id_ID'): string
|
||||
{
|
||||
if ($value === null) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return $currency.Number::format($value, locale: $locale);
|
||||
}
|
||||
}
|
||||
|
||||
if (! function_exists('replaceCurrency')) {
|
||||
function replaceCurrency(string $value)
|
||||
function replaceCurrency(?string $value = null)
|
||||
{
|
||||
if ($value === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (int) Str::replace('.', '', $value);
|
||||
}
|
||||
}
|
||||
@ -23,3 +32,12 @@ function percentage(int|float $value, int $precision = 2, int $maxPrecision = 2,
|
||||
return Number::percentage($value, $precision, $maxPrecision, $locale);
|
||||
}
|
||||
}
|
||||
|
||||
if (! function_exists('formatDiscount')) {
|
||||
function formatDiscount($amount, VoucherType $type, string $currency = 'Rp'): string
|
||||
{
|
||||
return $type === VoucherType::PERCENTAGE
|
||||
? percentage($amount)
|
||||
: currency($amount, $currency);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user