From 13dfa28d6e46816750bc8d6fe5d769d1d0310824 Mon Sep 17 00:00:00 2001 From: Yoga Pangestu Date: Mon, 8 Dec 2025 14:05:40 +0700 Subject: [PATCH] feat(helper): memberi komnetar untuk setiap fungsi dan menjadikan clean code --- app/Helpers/ColorHelpers.php | 41 +++++++++++++------ app/Helpers/DateTimeHelpers.php | 72 ++++++++++++++++++++++++--------- app/Helpers/StringHelpers.php | 43 +++++++++++++++++--- 3 files changed, 118 insertions(+), 38 deletions(-) diff --git a/app/Helpers/ColorHelpers.php b/app/Helpers/ColorHelpers.php index 4c67855..1c1a7e3 100644 --- a/app/Helpers/ColorHelpers.php +++ b/app/Helpers/ColorHelpers.php @@ -1,19 +1,34 @@ 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']; diff --git a/app/Helpers/StringHelpers.php b/app/Helpers/StringHelpers.php index e4fc26c..ce86dcd 100644 --- a/app/Helpers/StringHelpers.php +++ b/app/Helpers/StringHelpers.php @@ -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) {