Files
siakad/app/Support/Legacy/LegacyFormat.php
T

99 lines
3.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Support\Legacy;
use Illuminate\Support\Carbon;
use Illuminate\Support\Str;
/** Format tampilan data DB lama: nama bergelar, label periode, hari (chari), jam. */
final class LegacyFormat
{
/** f_semester */
private const array SEMESTERS = [1 => 'Ganjil', 2 => 'Genap'];
/** chari: 1 Senin sampai 7 Minggu; 0 berarti tanpa hari. */
public const array DAYS = [1 => 'Senin', 2 => 'Selasa', 3 => 'Rabu', 4 => 'Kamis', 5 => 'Jumat', 6 => 'Sabtu', 7 => 'Minggu'];
/** f_perkuliahan: 2 = semester pendek. */
public const int PERKULIAHAN_PENDEK = 2;
/** Nama pegawai bergelar, mis. "Dr. Budi Santoso, S.T., M.T.". */
public static function pegawaiName(?string $front, ?string $name, ?string $back): string
{
$full = Str::squish($front.' '.$name);
$suffix = trim((string) $back, " \t,");
return $suffix === '' ? $full : $full.', '.$suffix;
}
/** Mis. "2019/2020 Genap" atau "2019/2020 Genap (pendek)". */
public static function periodLabel(?string $thakad, int|string|null $semester, int|string|null $perkuliahan = null): string
{
$label = trim(trim((string) $thakad).' '.(self::SEMESTERS[(int) $semester] ?? ''));
return (int) $perkuliahan === self::PERKULIAHAN_PENDEK ? $label.' (pendek)' : $label;
}
/** Mis. "2020/2021 - Ganjil" (format tahun akademik pada lembar cetak). */
public static function academicYear(?string $thakad, int|string|null $semester): string
{
return trim(trim((string) $thakad).' - '.(self::SEMESTERS[(int) $semester] ?? ''), ' -');
}
/**
* Rentang tanggal perkiraan sebuah periode: ganjil Agustus–Januari, genap Februari–Juli
* (t_periodekuliah.mulai/selesai hampir selalu kosong). Null bila thakad bukan "YYYY/YYYY".
*
* @return array{0: Carbon, 1: Carbon}|null
*/
public static function periodRange(?string $thakad, int|string|null $semester): ?array
{
if (! preg_match('/^(\d{4})\/(\d{4})/', trim((string) $thakad), $m)) {
return null;
}
[$first, $second] = [(int) $m[1], (int) $m[2]];
return (int) $semester === 1
? [Carbon::create($first, 8, 1)->startOfDay(), Carbon::create($second, 1, 31)->endOfDay()]
: [Carbon::create($second, 2, 1)->startOfDay(), Carbon::create($second, 7, 31)->endOfDay()];
}
/** Tahun kalender sebuah bulan dalam periode "YYYY/YYYY": Agustus–Desember tahun pertama, Januari–Juli tahun kedua. */
public static function yearOfMonth(?string $thakad, int $month): ?int
{
if (! preg_match('/^(\d{4})\/(\d{4})/', trim((string) $thakad), $m)) {
return null;
}
return $month >= 8 ? (int) $m[1] : (int) $m[2];
}
/** Sama dengan f_semkex: (tahun periode - angkatan) * 2 + (semester periode - semester masuk) + 1. */
public static function semesterNumber(int|string|null $tahunMasuk, int|string|null $semesterMasuk, ?string $thakad, int|string|null $semester): ?int
{
if ($tahunMasuk === null || $semesterMasuk === null || $thakad === null || $semester === null) {
return null;
}
return ((int) substr(trim($thakad), 0, 4) - (int) $tahunMasuk) * 2 + ((int) $semester - (int) $semesterMasuk) + 1;
}
public static function dayName(int|string|null $day): ?string
{
return self::DAYS[(int) $day] ?? null;
}
/** Mis. "Rp 4.500.000". */
public static function rupiah(int|float $amount): string
{
return 'Rp '.number_format($amount, 0, ',', '.');
}
/** "07:30:00" menjadi "07.30". */
public static function time(?string $time): string
{
return str_replace(':', '.', substr((string) $time, 0, 5));
}
}