| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Calculation\TextData; |
| 4 |
|
| 5 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
| 6 |
use TablePress\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($number) |
| 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 = array_merge([(string) substr($digits, 0, $length)], str_split((string) substr($digits, $length), 6)); |
| 93 |
|
| 94 |
$chunks = array_filter($chunks, fn (string $chunk): bool => $chunk !== ''); |
| 95 |
|
| 96 |
return implode( |
| 97 |
self::THAI_UNITS[6], |
| 98 |
array_map(\Closure::fromCallable([self::class, 'convertBlock']), $chunks) |
| 99 |
); |
| 100 |
} |
| 101 |
|
| 102 |
private static function convertBlock(string $block): string |
| 103 |
{ |
| 104 |
$out = ''; |
| 105 |
$length = strlen($block); |
| 106 |
$i = 0; |
| 107 |
|
| 108 |
// Hundreds and higher powers |
| 109 |
for ($power = $length - 1; $power >= 2; --$power) { |
| 110 |
$digit = $block[$i++]; |
| 111 |
if ($digit !== '0') { |
| 112 |
$out .= self::THAI_DIGITS[$digit] . self::THAI_UNITS[$power]; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
// Tens |
| 117 |
$ten = $length > 1 ? $block[$i++] : '0'; |
| 118 |
if ($ten !== '0') { |
| 119 |
switch ($ten) { |
| 120 |
case '1': |
| 121 |
case '2': |
| 122 |
default: |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
// Ones |
| 127 |
$one = $block[$i] ?? '0'; |
| 128 |
if ($one !== '0') { |
| 129 |
$out .= $ten !== '0' && $one === '1' |
| 130 |
? self::THAI_COMPOUND_ONE |
| 131 |
: self::THAI_DIGITS[$one]; |
| 132 |
} |
| 133 |
|
| 134 |
return $out; |
| 135 |
} |
| 136 |
} |
| 137 |
|