1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/phpviet/number-to-words |
4
|
|
|
* @copyright (c) PHP Viet |
5
|
|
|
* @license [MIT](http://www.opensource.org/licenses/MIT) |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace PHPViet\NumberToWords\Concerns; |
9
|
|
|
|
10
|
|
|
use PHPViet\NumberToWords\DictionaryInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @author Vuong Minh <[email protected]> |
14
|
|
|
* @since 1.0.0 |
15
|
|
|
*/ |
16
|
|
|
trait TripletTransformer |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Trả về từ điển hổ trợ cho việc chuyển đổi. |
20
|
|
|
* |
21
|
|
|
* @return DictionaryInterface |
22
|
|
|
*/ |
23
|
|
|
abstract protected function getDictionary(): DictionaryInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Chuyển đổi cụm 3 số thành chữ số. |
27
|
|
|
* |
28
|
|
|
* @param int $triplet |
29
|
|
|
* @param bool $isFirst |
30
|
|
|
* @param int $exponent |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
|
|
protected function tripletToWords(int $triplet, bool $isFirst, int $exponent): string |
34
|
|
|
{ |
35
|
|
|
$words = []; |
36
|
|
|
[$hundred, $ten, $unit] = $this->splitTriplet($triplet); |
|
|
|
|
37
|
|
|
$dictionary = $this->getDictionary(); |
38
|
|
|
|
39
|
|
|
if (0 < $hundred || ! $isFirst) { |
40
|
|
|
$words[] = $dictionary->getTripletHundred($hundred); |
41
|
|
|
|
42
|
|
|
if (0 === $ten && 0 < $unit) { |
43
|
|
|
$words[] = $dictionary->tripletTenSeparator(); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if (0 < $ten) { |
48
|
|
|
$words[] = $dictionary->getTripletTen($ten); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if (0 < $unit) { |
52
|
|
|
$words[] = $this->getTripletUnit($unit, $ten); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$words[] = $dictionary->getExponent($exponent); |
56
|
|
|
|
57
|
|
|
return implode($dictionary->separator(), array_filter($words)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Chia 3 số thành mảng 3 phần tử tương ứng với hàng trăm, hàng chục, hàng đơn vị. |
62
|
|
|
* |
63
|
|
|
* @param int $triplet |
64
|
|
|
* @return array |
65
|
|
|
*/ |
66
|
|
|
private function splitTriplet(int $triplet): array |
67
|
|
|
{ |
68
|
|
|
$hundred = (int) ($triplet / 100) % 10; |
69
|
|
|
$ten = (int) ($triplet / 10) % 10; |
70
|
|
|
$unit = $triplet % 10; |
71
|
|
|
|
72
|
|
|
return [$hundred, $ten, $unit]; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Chuyển đổi số hàng đơn vị sang chữ số ở một số trường hợp đặc biệt. |
77
|
|
|
* |
78
|
|
|
* @param int $unit |
79
|
|
|
* @param int $ten |
80
|
|
|
* @return null|string |
81
|
|
|
*/ |
82
|
|
|
private function getTripletUnit(int $unit, int $ten): string |
83
|
|
|
{ |
84
|
|
|
$dictionary = $this->getDictionary(); |
85
|
|
|
|
86
|
|
|
if (2 <= $ten) { |
87
|
|
|
if (1 === $unit) { |
88
|
|
|
return $dictionary->specialTripletUnitOne(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if (4 === $unit) { |
92
|
|
|
return $dictionary->specialTripletUnitFour(); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if (1 <= $ten && 5 === $unit) { |
97
|
|
|
return $dictionary->specialTripletUnitFive(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $dictionary->getTripletUnit($unit); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.