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; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @author Vuong Minh <[email protected]> |
12
|
|
|
* @since 1.0.0 |
13
|
|
|
*/ |
14
|
|
|
class Transformer |
15
|
|
|
{ |
16
|
|
|
use Concerns\Collapse; |
17
|
|
|
use Concerns\NumberResolver; |
18
|
|
|
use Concerns\TripletsConverter; |
19
|
|
|
use Concerns\TripletTransformer; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var DictionaryInterface |
23
|
|
|
*/ |
24
|
|
|
protected $dictionary; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Tạo đối tượng mới với từ điển chỉ định. |
28
|
|
|
* |
29
|
|
|
* @param DictionaryInterface $dictionary |
30
|
|
|
*/ |
31
|
|
|
public function __construct(?DictionaryInterface $dictionary = null) |
32
|
|
|
{ |
33
|
|
|
if (null === $dictionary) { |
34
|
|
|
$dictionary = new Dictionary(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$this->dictionary = $dictionary; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Chuyển đổi số sang chữ số. |
42
|
|
|
* |
43
|
|
|
* @param int|float|string $number |
44
|
|
|
* @return string |
45
|
|
|
* @throws \InvalidArgumentException |
46
|
|
|
*/ |
47
|
|
|
public function toWords($number): string |
48
|
|
|
{ |
49
|
|
|
[$minus, $number, $decimal] = $this->resolveNumber($number); |
|
|
|
|
50
|
|
|
$words[] = $minus ? $this->dictionary->minus() : ''; |
|
|
|
|
51
|
|
|
|
52
|
|
|
if (0 === $number) { |
53
|
|
|
$words[] = $this->dictionary->zero(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$triplets = $this->numberToTriplets($number); |
57
|
|
|
|
58
|
|
|
foreach ($triplets as $pos => $triplet) { |
59
|
|
|
if (0 < $triplet) { |
60
|
|
|
$words[] = $this->tripletToWords($triplet, 0 === $pos, count($triplets) - $pos - 1); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if (0 < $decimal) { |
65
|
|
|
$words[] = $this->dictionary->fraction(); |
66
|
|
|
$words[] = $this->toWords($decimal); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $this->collapseWords($words); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Chuyển đổi số sang chữ số kết hợp với đơn vị tiền tệ. |
74
|
|
|
* |
75
|
|
|
* @param $number |
76
|
|
|
* @param array|string[]|string $unit |
77
|
|
|
* @return string |
78
|
|
|
* @throws \InvalidArgumentException |
79
|
|
|
*/ |
80
|
|
|
public function toCurrency($number, $unit = 'đồng'): string |
81
|
|
|
{ |
82
|
|
|
$unit = (array) $unit; |
83
|
|
|
$originNumber = $number; |
84
|
|
|
[$minus, $number, $decimal] = $this->resolveNumber($number); |
|
|
|
|
85
|
|
|
|
86
|
|
|
if (0 === $decimal || ! isset($unit[1])) { |
87
|
|
|
$words[] = $this->toWords($originNumber); |
|
|
|
|
88
|
|
|
$words[] = $unit[0]; |
89
|
|
|
} else { |
90
|
|
|
[$unit, $decimalUnit] = $unit; |
|
|
|
|
91
|
|
|
$words[] = $minus ? $this->dictionary->minus() : ''; |
|
|
|
|
92
|
|
|
$words[] = $this->toWords($number); |
93
|
|
|
$words[] = $unit; |
94
|
|
|
$words[] = $this->toWords($decimal); |
95
|
|
|
$words[] = $decimalUnit; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $this->collapseWords($words); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
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.