parfum/app/Helpers/StringHelpers.php

191 lines
5.6 KiB
PHP

<?php
use App\Enums\VoucherType;
use Illuminate\Support\Number;
use Illuminate\Support\Str;
if (! function_exists('currency')) {
/**
* Format value as currency
*
* @param string|null $currency Currency prefix (default: null)
* @param string $locale Locale for formatting (default: 'id_ID')
*/
function currency(string|int|float|null $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')) {
/**
* Strip currency format from string and return as integer
*/
function replaceCurrency(?string $value = null): ?int
{
if ($value === null) {
return null;
}
$cleaned = Str::of($value)
->replace('Rp', '')
->replace('.', '')
->replace(' ', '')
->__toString();
return (int) $cleaned;
}
}
if (! function_exists('percentage')) {
/**
* Format value as percentage
*
* @param int $precision Precision (default: 2)
* @param int $maxPrecision Max precision (default: 2)
* @param string $locale Locale for formatting (default: 'id_ID')
*/
function percentage(int|float $value, int $precision = 2, int $maxPrecision = 2, string $locale = 'id_ID'): string
{
return Number::percentage($value, $precision, $maxPrecision, $locale);
}
}
if (! function_exists('formatDiscount')) {
/**
* Format discount based on voucher type (percentage or fixed amount)
*/
function formatDiscount(int|float $amount, VoucherType $type, string $currency = 'Rp'): string
{
return $type === VoucherType::PERCENTAGE
? percentage($amount)
: currency($amount, $currency);
}
}
if (! function_exists('generateReferralCode')) {
/**
* Generate referral code from username and unique number
*/
function generateReferralCode(string $username, int $unique): string
{
$prefix = Str::substr($username, 0, 6);
$year = date('y');
return Str::upper($prefix).$year.$unique;
}
}
if (! function_exists('sanitizeIntegers')) {
/**
* Sanitize integer values in an array (convert empty string/null to 0)
*
* @param array $integerKeys Keys to sanitize (empty = all keys)
*/
function sanitizeIntegers(array $data, array $integerKeys = []): array
{
foreach ($data as $key => $value) {
if (empty($integerKeys) || in_array($key, $integerKeys)) {
if ($value === '' || $value === null) {
$data[$key] = 0;
}
}
}
return $data;
}
}
if (! function_exists('formatPhoneNumber')) {
/**
* Format Indonesian phone number with customizable country prefix
*
* @param string $prefix Country prefix (default: '+62')
* @param bool $useDash Use dashes for formatting (default: false)
*/
function formatPhoneNumber(string $number, string $prefix = '+62', bool $useDash = false): string
{
$clean = preg_replace('/\D+/', '', $number);
$clean = preg_replace('/^(0|62)/', '', $clean);
$formatted = $prefix.$clean;
if ($useDash) {
$formatted = preg_replace('/(\d{3})(\d{3,4})(\d{3,4})(\d+)?/', $prefix.'-$1-$2-$3$4', $formatted);
}
return $formatted;
}
}
if (! function_exists('terbilang')) {
function terbilang($angka)
{
$angka = (int) abs($angka);
$huruf = ['', 'satu', 'dua', 'tiga', 'empat', 'lima', 'enam', 'tujuh', 'delapan', 'sembilan', 'sepuluh', 'sebelas'];
if ($angka < 12) {
return $huruf[$angka];
} elseif ($angka < 20) {
return $huruf[$angka - 10].' belas';
} elseif ($angka < 100) {
$result = $huruf[$angka / 10].' puluh';
$sisa = $angka % 10;
if ($sisa > 0) {
$result .= ' '.$huruf[$sisa];
}
return $result;
} elseif ($angka < 200) {
return 'seratus '.terbilang($angka - 100);
} elseif ($angka < 1000) {
$result = $huruf[$angka / 100].' ratus';
$sisa = $angka % 100;
if ($sisa > 0) {
$result .= ' '.terbilang($sisa);
}
return $result;
} elseif ($angka < 2000) {
return 'seribu '.terbilang($angka - 1000);
} elseif ($angka < 1000000) {
$result = terbilang($angka / 1000).' ribu';
$sisa = $angka % 1000;
if ($sisa > 0) {
$result .= ' '.terbilang($sisa);
}
return $result;
} elseif ($angka < 1000000000) {
$result = terbilang($angka / 1000000).' juta';
$sisa = $angka % 1000000;
if ($sisa > 0) {
$result .= ' '.terbilang($sisa);
}
return $result;
} elseif ($angka < 1000000000000) {
$result = terbilang($angka / 1000000000).' milyar';
$sisa = $angka % 1000000000;
if ($sisa > 0) {
$result .= ' '.terbilang($sisa);
}
return $result;
} elseif ($angka < 1000000000000000) {
$result = terbilang($angka / 1000000000000).' trilyun';
$sisa = $angka % 1000000000000;
if ($sisa > 0) {
$result .= ' '.terbilang($sisa);
}
return $result;
} else {
return 'Angka terlalu besar';
}
}
}