|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Ay4t\Helper\Validation; |
| 4 | + |
| 5 | +/** |
| 6 | + * Shipping Container Check Digit (ISO 6346) helper |
| 7 | + * |
| 8 | + * - Mendukung input: "CSQU3054383", "CSQU 305438 3", atau prefix+serial terpisah. |
| 9 | + * - Algoritma: map huruf ke nilai ISO, bobot 2^pos untuk 10 karakter pertama, |
| 10 | + * jumlah % 11; jika hasil 10 maka check digit = 0; selain itu = hasil. |
| 11 | + */ |
| 12 | +class ShippingContainerChecker |
| 13 | +{ |
| 14 | + /** @var string */ |
| 15 | + protected string $ownerCode = ''; |
| 16 | + /** @var string */ |
| 17 | + protected string $category = 'U'; |
| 18 | + /** @var string */ |
| 19 | + protected string $serial = ''; |
| 20 | + /** @var int|null */ |
| 21 | + protected ?int $providedCheck = null; |
| 22 | + |
| 23 | + /** |
| 24 | + * Set container number in a flexible format. |
| 25 | + * Accepts: "CSQU3054383", "CSQU 305438 3", or parts |
| 26 | + */ |
| 27 | + public function set(string $container, ?string $category = null): self |
| 28 | + { |
| 29 | + // Normalize: remove spaces and non-alphanumeric |
| 30 | + $norm = strtoupper(preg_replace('/[^A-Z0-9]/i', '', $container)); |
| 31 | + |
| 32 | + // Expect at least 10 chars (4 letters + 6 digits) |
| 33 | + if (strlen($norm) < 10) { |
| 34 | + throw new \InvalidArgumentException('Container number too short'); |
| 35 | + } |
| 36 | + |
| 37 | + $prefix = substr($norm, 0, 4); |
| 38 | + $serial = substr($norm, 4, 6); |
| 39 | + $check = strlen($norm) >= 11 ? substr($norm, 10, 1) : null; |
| 40 | + |
| 41 | + if (!preg_match('/^[A-Z]{4}$/', $prefix)) { |
| 42 | + throw new \InvalidArgumentException('Invalid owner/category code'); |
| 43 | + } |
| 44 | + if (!preg_match('/^[0-9]{6}$/', $serial)) { |
| 45 | + throw new \InvalidArgumentException('Invalid serial'); |
| 46 | + } |
| 47 | + if ($check !== null && !preg_match('/^[0-9]$/', $check)) { |
| 48 | + throw new \InvalidArgumentException('Invalid check digit'); |
| 49 | + } |
| 50 | + |
| 51 | + $this->ownerCode = substr($prefix, 0, 3); |
| 52 | + $this->category = $category ? strtoupper($category) : substr($prefix, 3, 1); |
| 53 | + $this->serial = $serial; |
| 54 | + $this->providedCheck = $check !== null ? (int)$check : null; |
| 55 | + |
| 56 | + return $this; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Hitung check digit dari owner+category+serial (10 karakter pertama) |
| 61 | + */ |
| 62 | + public function calculateCheckDigit(?string $owner = null, ?string $category = null, ?string $serial = null): int |
| 63 | + { |
| 64 | + $owner = $owner ?? $this->ownerCode; |
| 65 | + $category = $category ?? $this->category; |
| 66 | + $serial = $serial ?? $this->serial; |
| 67 | + |
| 68 | + if (!preg_match('/^[A-Z]{3}$/', $owner) || !preg_match('/^[A-Z]{1}$/', $category) || !preg_match('/^[0-9]{6}$/', $serial)) { |
| 69 | + throw new \InvalidArgumentException('Invalid components for calculation'); |
| 70 | + } |
| 71 | + |
| 72 | + $first10 = $owner . $category . $serial; // 10 chars |
| 73 | + $sum = 0; |
| 74 | + for ($i = 0; $i < 10; $i++) { |
| 75 | + $char = $first10[$i]; |
| 76 | + $value = ctype_alpha($char) ? self::letterValue($char) : (int)$char; |
| 77 | + $weight = 1 << $i; // 2^i |
| 78 | + $sum += $value * $weight; |
| 79 | + } |
| 80 | + $remainder = $sum % 11; |
| 81 | + $check = $remainder % 10; // if 10 => 0 |
| 82 | + return $check; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Validasi nomor container (jika disediakan check digit) |
| 87 | + */ |
| 88 | + public function isValid(): bool |
| 89 | + { |
| 90 | + if ($this->providedCheck === null) { |
| 91 | + return false; |
| 92 | + } |
| 93 | + return $this->providedCheck === $this->calculateCheckDigit(); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Dapatkan check digit yang diharapkan dari data saat ini |
| 98 | + */ |
| 99 | + public function expectedCheckDigit(): int |
| 100 | + { |
| 101 | + return $this->calculateCheckDigit(); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Kembalikan format terstandardisasi: OWNER CATEGORY SERIAL CHECK |
| 106 | + * contoh: CSQU3054383 |
| 107 | + */ |
| 108 | + public function getResult(): string |
| 109 | + { |
| 110 | + $check = $this->calculateCheckDigit(); |
| 111 | + return sprintf('%s%s%s%d', $this->ownerCode, $this->category, $this->serial, $check); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Nilai huruf sesuai ISO 6346 |
| 116 | + * A=10, B=12, C=13, D=14, E=15, F=16, G=17, H=18, I=19, J=20, |
| 117 | + * K=21, L=23, M=24, N=25, O=26, P=27, Q=28, R=29, S=30, T=31, |
| 118 | + * U=32, V=34, W=35, X=36, Y=37, Z=38 |
| 119 | + */ |
| 120 | + public static function letterValue(string $letter): int |
| 121 | + { |
| 122 | + static $map = [ |
| 123 | + 'A' => 10, 'B' => 12, 'C' => 13, 'D' => 14, 'E' => 15, 'F' => 16, 'G' => 17, 'H' => 18, 'I' => 19, |
| 124 | + 'J' => 20, 'K' => 21, 'L' => 23, 'M' => 24, 'N' => 25, 'O' => 26, 'P' => 27, 'Q' => 28, 'R' => 29, |
| 125 | + 'S' => 30, 'T' => 31, 'U' => 32, 'V' => 34, 'W' => 35, 'X' => 36, 'Y' => 37, 'Z' => 38, |
| 126 | + ]; |
| 127 | + $letter = strtoupper($letter); |
| 128 | + if (!isset($map[$letter])) { |
| 129 | + throw new \InvalidArgumentException('Invalid letter: ' . $letter); |
| 130 | + } |
| 131 | + return $map[$letter]; |
| 132 | + } |
| 133 | +} |
0 commit comments