feat(helper): memberi komnetar untuk setiap fungsi dan menjadikan clean code
This commit is contained in:
parent
96e8b0a3d5
commit
13dfa28d6e
@ -1,19 +1,34 @@
|
||||
<?php
|
||||
|
||||
function randomColors(int $count, float $alpha = 0.2): array
|
||||
{
|
||||
$colors = [];
|
||||
for ($i = 0; $i < $count; $i++) {
|
||||
$r = rand(0, 255);
|
||||
$g = rand(0, 255);
|
||||
$b = rand(0, 255);
|
||||
$colors[] = "rgba($r, $g, $b, $alpha)";
|
||||
if (! function_exists('randomColors')) {
|
||||
/**
|
||||
* Generate an array of random RGBA colors
|
||||
*
|
||||
* @param int $count Number of colors to generate
|
||||
* @param float $alpha Alpha/transparency value (default: 0.2)
|
||||
*/
|
||||
function randomColors(int $count, float $alpha = 0.2): array
|
||||
{
|
||||
$colors = [];
|
||||
for ($i = 0; $i < $count; $i++) {
|
||||
$r = rand(0, 255);
|
||||
$g = rand(0, 255);
|
||||
$b = rand(0, 255);
|
||||
$colors[] = "rgba($r, $g, $b, $alpha)";
|
||||
}
|
||||
|
||||
return $colors;
|
||||
}
|
||||
|
||||
return $colors;
|
||||
}
|
||||
|
||||
function randomBorderColors(int $count): array
|
||||
{
|
||||
return randomColors($count, 1.0);
|
||||
if (! function_exists('randomBorderColors')) {
|
||||
/**
|
||||
* Generate an array of random RGB colors for borders (alpha = 1.0)
|
||||
*
|
||||
* @param int $count Number of colors to generate
|
||||
*/
|
||||
function randomBorderColors(int $count): array
|
||||
{
|
||||
return randomColors($count, 1.0);
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,40 +3,62 @@
|
||||
use Carbon\Carbon;
|
||||
|
||||
if (! function_exists('formatDate')) {
|
||||
function formatDate(?string $date = null, ?string $format = 'l, d M Y')
|
||||
/**
|
||||
* Format date/time with a configurable format using Carbon.
|
||||
* Core formatting function used by formatTime() and formatDateTime().
|
||||
*
|
||||
* @param string $format Carbon format (default: 'l, d M Y')
|
||||
* @param bool $translated Use translatedFormat for locale-aware output (default: true)
|
||||
*/
|
||||
function formatDate(?string $date = null, string $format = 'l, d M Y', bool $translated = true): string
|
||||
{
|
||||
return ! $date ? '-' : Carbon::parse($date)->translatedFormat($format);
|
||||
if (! $date) {
|
||||
return '-';
|
||||
}
|
||||
|
||||
$carbon = Carbon::parse($date);
|
||||
|
||||
return $translated ? $carbon->translatedFormat($format) : $carbon->format($format);
|
||||
}
|
||||
}
|
||||
|
||||
if (! function_exists('formatTime')) {
|
||||
function formatTime(?string $time = null, ?string $format = 'H:i')
|
||||
/**
|
||||
* Format time only (convenience wrapper for formatDate with time format).
|
||||
*
|
||||
* @param string $format Carbon format (default: 'H:i')
|
||||
*/
|
||||
function formatTime(?string $time = null, string $format = 'H:i'): string
|
||||
{
|
||||
return ! $time ? '-' : Carbon::parse($time)->format($format);
|
||||
return formatDate($time, $format, false);
|
||||
}
|
||||
}
|
||||
|
||||
if (! function_exists('formatDateTime')) {
|
||||
function formatDateTime(?string $dateTime = null)
|
||||
/**
|
||||
* Format date and time (convenience wrapper for formatDate with datetime format).
|
||||
*
|
||||
* @param string $format Carbon format (default: 'l, d M Y H:i:s')
|
||||
*/
|
||||
function formatDateTime(?string $dateTime = null, string $format = 'l, d M Y H:i:s'): string
|
||||
{
|
||||
|
||||
return ! $dateTime ? '-' : Carbon::parse($dateTime)->translatedFormat('l, d M Y H:i:s');
|
||||
}
|
||||
}
|
||||
|
||||
if (! function_exists('formatDateDash')) {
|
||||
function formatDateDash(string $date)
|
||||
{
|
||||
return Carbon::parse($date)->translatedFormat('Y-m-d');
|
||||
return formatDate($dateTime, $format);
|
||||
}
|
||||
}
|
||||
|
||||
if (! function_exists('getAge')) {
|
||||
function getAge(string $birthdate)
|
||||
/**
|
||||
* Calculate age from birthdate using Carbon.
|
||||
* Returns formatted string in Indonesian (e.g. "25 tahun 3 bulan").
|
||||
*/
|
||||
function getAge(?string $birthdate = null): string
|
||||
{
|
||||
if (! $birthdate) {
|
||||
return '-';
|
||||
}
|
||||
|
||||
$birth = Carbon::parse($birthdate);
|
||||
$now = Carbon::now();
|
||||
|
||||
$diff = $birth->diff($now);
|
||||
|
||||
return "{$diff->y} tahun {$diff->m} bulan";
|
||||
@ -44,14 +66,26 @@ function getAge(string $birthdate)
|
||||
}
|
||||
|
||||
if (! function_exists('timeAgo')) {
|
||||
function timeAgo(?string $date = null)
|
||||
/**
|
||||
* Get relative time string using Carbon's diffForHumans().
|
||||
* Returns localized relative time (e.g. "2 hours ago", "3 days ago").
|
||||
*/
|
||||
function timeAgo(?string $date = null): string
|
||||
{
|
||||
return ! $date ? '-' : Carbon::parse($date)->diffForHumans();
|
||||
if (! $date) {
|
||||
return '-';
|
||||
}
|
||||
|
||||
return Carbon::parse($date)->diffForHumans();
|
||||
}
|
||||
}
|
||||
|
||||
if (! function_exists('normalizeOpeningHours')) {
|
||||
function normalizeOpeningHours(array $openingHours)
|
||||
/**
|
||||
* Normalize opening hours for all days of the week.
|
||||
* Ensures all 7 days are present in the result array.
|
||||
*/
|
||||
function normalizeOpeningHours(array $openingHours): array
|
||||
{
|
||||
$days = ['senin', 'selasa', 'rabu', 'kamis', 'jumat', 'sabtu', 'minggu'];
|
||||
|
||||
|
||||
@ -5,7 +5,13 @@
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
if (! function_exists('currency')) {
|
||||
function currency(?string $value = null, ?string $currency = null, ?string $locale = 'id_ID'): string
|
||||
/**
|
||||
* 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 '';
|
||||
@ -16,7 +22,10 @@ function currency(?string $value = null, ?string $currency = null, ?string $loca
|
||||
}
|
||||
|
||||
if (! function_exists('replaceCurrency')) {
|
||||
function replaceCurrency(?string $value = null)
|
||||
/**
|
||||
* Strip currency format from string and return as integer
|
||||
*/
|
||||
function replaceCurrency(?string $value = null): ?int
|
||||
{
|
||||
if ($value === null) {
|
||||
return null;
|
||||
@ -33,14 +42,24 @@ function replaceCurrency(?string $value = null)
|
||||
}
|
||||
|
||||
if (! function_exists('percentage')) {
|
||||
function percentage(int|float $value, int $precision = 2, int $maxPrecision = 2, ?string $locale = 'id_ID'): string
|
||||
/**
|
||||
* 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')) {
|
||||
function formatDiscount($amount, VoucherType $type, string $currency = 'Rp'): string
|
||||
/**
|
||||
* 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)
|
||||
@ -49,6 +68,9 @@ function formatDiscount($amount, VoucherType $type, string $currency = 'Rp'): st
|
||||
}
|
||||
|
||||
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);
|
||||
@ -59,6 +81,11 @@ function generateReferralCode(string $username, int $unique): string
|
||||
}
|
||||
|
||||
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) {
|
||||
@ -74,12 +101,16 @@ function sanitizeIntegers(array $data, array $integerKeys = []): array
|
||||
}
|
||||
|
||||
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) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user