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