|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PhpOffice\PhpSpreadsheet\Calculation\TextData; |
| 4 | + |
| 5 | +use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
| 6 | +use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError; |
| 7 | + |
| 8 | +class Thai |
| 9 | +{ |
| 10 | + use ArrayEnabled; |
| 11 | + |
| 12 | + private const THAI_DIGITS = [ |
| 13 | + 0 => 'ศูนย์', |
| 14 | + 1 => 'หนึ่ง', |
| 15 | + 2 => 'สอง', |
| 16 | + 3 => 'สาม', |
| 17 | + 4 => 'สี่', |
| 18 | + 5 => 'ห้า', |
| 19 | + 6 => 'หก', |
| 20 | + 7 => 'เจ็ด', |
| 21 | + 8 => 'แปด', |
| 22 | + 9 => 'เก้า', |
| 23 | + ]; |
| 24 | + |
| 25 | + private const THAI_UNITS = [ |
| 26 | + 1 => 'สิบ', |
| 27 | + 2 => 'ร้อย', |
| 28 | + 3 => 'พัน', |
| 29 | + 4 => 'หมื่น', |
| 30 | + 5 => 'แสน', |
| 31 | + 6 => 'ล้าน', |
| 32 | + ]; |
| 33 | + |
| 34 | + private const THAI_COMPOUND_ONE = 'เอ็ด'; |
| 35 | + private const THAI_COMPOUND_TWO = 'ยี่'; |
| 36 | + private const THAI_INTEGER = 'ถ้วน'; |
| 37 | + private const THAI_MINUS = 'ลบ'; |
| 38 | + private const THAI_BAHT = 'บาท'; |
| 39 | + private const THAI_SATANG = 'สตางค์'; |
| 40 | + |
| 41 | + /** |
| 42 | + * BAHTTEXT. |
| 43 | + * |
| 44 | + * @param mixed $number The number or array of numbers to convert |
| 45 | + * |
| 46 | + * @return array<mixed>|string If an array of values is passed as the argument, then the returned result will also be an array with the same dimensions |
| 47 | + */ |
| 48 | + public static function getBahtText(mixed $number): array|string |
| 49 | + { |
| 50 | + if (is_array($number)) { |
| 51 | + return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $number); |
| 52 | + } |
| 53 | + |
| 54 | + if (is_string($number) && preg_match('/^-?\d+$/', $number)) { |
| 55 | + $isNegative = str_starts_with($number, '-'); |
| 56 | + $baht = ltrim($number, '-0') ?: '0'; |
| 57 | + $satang = '00'; |
| 58 | + } elseif (is_bool($number) || is_numeric($number)) { |
| 59 | + $number += 0; |
| 60 | + $isNegative = $number < 0; |
| 61 | + [$baht, $satang] = explode('.', number_format(abs($number), 2, '.', '')); |
| 62 | + } else { |
| 63 | + return ExcelError::VALUE(); |
| 64 | + } |
| 65 | + |
| 66 | + $hasWhole = $baht !== '0'; |
| 67 | + $hasFraction = $satang !== '00'; |
| 68 | + |
| 69 | + if (!$hasWhole && !$hasFraction) { |
| 70 | + return self::THAI_DIGITS[0] . self::THAI_BAHT . self::THAI_INTEGER; |
| 71 | + } |
| 72 | + |
| 73 | + $text = $isNegative |
| 74 | + ? self::THAI_MINUS |
| 75 | + : ''; |
| 76 | + |
| 77 | + if ($hasWhole) { |
| 78 | + $text .= self::convertLarge($baht) . self::THAI_BAHT; |
| 79 | + } |
| 80 | + |
| 81 | + $text .= $hasFraction |
| 82 | + ? self::convertBlock($satang) . self::THAI_SATANG |
| 83 | + : self::THAI_INTEGER; |
| 84 | + |
| 85 | + return $text; |
| 86 | + } |
| 87 | + |
| 88 | + private static function convertLarge(string $digits): string |
| 89 | + { |
| 90 | + $length = strlen($digits) % 6 ?: 6; |
| 91 | + |
| 92 | + $chunks = [ |
| 93 | + substr($digits, 0, $length), |
| 94 | + ...str_split(substr($digits, $length), 6), |
| 95 | + ]; |
| 96 | + |
| 97 | + $chunks = array_filter($chunks, fn (string $chunk): bool => $chunk !== ''); |
| 98 | + |
| 99 | + return implode( |
| 100 | + self::THAI_UNITS[6], |
| 101 | + array_map(self::convertBlock(...), $chunks) |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + private static function convertBlock(string $block): string |
| 106 | + { |
| 107 | + $out = ''; |
| 108 | + $length = strlen($block); |
| 109 | + $i = 0; |
| 110 | + |
| 111 | + // Hundreds and higher powers |
| 112 | + for ($power = $length - 1; $power >= 2; --$power) { |
| 113 | + $digit = $block[$i++]; |
| 114 | + if ($digit !== '0') { |
| 115 | + $out .= self::THAI_DIGITS[$digit] . self::THAI_UNITS[$power]; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + // Tens |
| 120 | + $ten = $length > 1 ? $block[$i++] : '0'; |
| 121 | + if ($ten !== '0') { |
| 122 | + $out .= match ($ten) { |
| 123 | + '1' => '', |
| 124 | + '2' => self::THAI_COMPOUND_TWO, |
| 125 | + default => self::THAI_DIGITS[$ten], |
| 126 | + } . self::THAI_UNITS[1]; |
| 127 | + } |
| 128 | + |
| 129 | + // Ones |
| 130 | + $one = $block[$i] ?? '0'; |
| 131 | + if ($one !== '0') { |
| 132 | + $out .= $ten !== '0' && $one === '1' |
| 133 | + ? self::THAI_COMPOUND_ONE |
| 134 | + : self::THAI_DIGITS[$one]; |
| 135 | + } |
| 136 | + |
| 137 | + return $out; |
| 138 | + } |
| 139 | +} |
0 commit comments