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
|
|
|
/** |
11
|
|
|
* @author Vuong Minh <[email protected]> |
12
|
|
|
* @since 1.0.0 |
13
|
|
|
*/ |
14
|
|
|
trait TripletTransformer |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Chuyển đổi cụm 3 số thành chữ số. |
18
|
|
|
* |
19
|
|
|
* @param int $triplet |
20
|
|
|
* @param bool $isFirst |
21
|
|
|
* @param int $exponent |
22
|
|
|
* @return string |
23
|
|
|
*/ |
24
|
|
|
protected function tripletToWords(int $triplet, bool $isFirst, int $exponent): string |
25
|
|
|
{ |
26
|
|
|
[$hundred, $ten, $unit] = $this->splitTriplet($triplet); |
|
|
|
|
27
|
|
|
|
28
|
|
|
if (0 < $hundred || ! $isFirst) { |
29
|
|
|
$words[] = $this->dictionary->getTripletHundred($hundred); |
|
|
|
|
30
|
|
|
|
31
|
|
|
if (0 === $ten && 0 < $unit) { |
32
|
|
|
$words[] = $this->dictionary->tripletTenSeparator(); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
if (0 < $ten) { |
37
|
|
|
$words[] = $this->dictionary->getTripletTen($ten); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if (0 < $unit) { |
41
|
|
|
$words[] = $this->getTripletUnit($unit, $ten); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$words[] = $this->dictionary->getExponent($exponent); |
45
|
|
|
|
46
|
|
|
return $this->collapseWords($words); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* 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ị. |
51
|
|
|
* |
52
|
|
|
* @param int $triplet |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
|
|
private function splitTriplet(int $triplet): array |
56
|
|
|
{ |
57
|
|
|
$hundred = (int) ($triplet / 100) % 10; |
58
|
|
|
$ten = (int) ($triplet / 10) % 10; |
59
|
|
|
$unit = $triplet % 10; |
60
|
|
|
|
61
|
|
|
return [$hundred, $ten, $unit]; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Chuyển đổi số hàng đơn vị sang chữ số ở một số trường hợp đặc biệt. |
66
|
|
|
* |
67
|
|
|
* @param int $unit |
68
|
|
|
* @param int $ten |
69
|
|
|
* @return null|string |
70
|
|
|
*/ |
71
|
|
|
private function getTripletUnit(int $unit, int $ten): string |
72
|
|
|
{ |
73
|
|
|
if (1 <= $ten && 5 === $unit) { |
74
|
|
|
return $this->dictionary->specialTripletUnitFive(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (2 <= $ten) { |
78
|
|
|
if (1 === $unit) { |
79
|
|
|
return $this->dictionary->specialTripletUnitOne(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if (4 === $unit) { |
83
|
|
|
return $this->dictionary->specialTripletUnitFour(); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $this->dictionary->getTripletUnit($unit); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
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.