-
Notifications
You must be signed in to change notification settings - Fork 0
/
NumberToWords.php
268 lines (218 loc) · 5.91 KB
/
NumberToWords.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<?php
declare(strict_types=1);
namespace Tamedevelopers\Support;
use Tamedevelopers\Support\Str;
use Tamedevelopers\Support\Traits\NumberToWordsTraits;
class NumberToWords {
use NumberToWordsTraits;
/**
* Allow cents text to be added
*
* @var bool|null
*/
static private $allowCents;
/**
* Currency data
*
* @var mixed
*/
static private $currencyData;
/**
* Formatted text data
*
* @var mixed
*/
static private $text;
/**
* Allow Cents
* @param bool|null $cents
* - [optional] Default is false
*
* @return $this
*/
static public function cents($cents = null)
{
$clone = self::clone();
self::$allowCents = $cents;
return $clone;
}
/**
* Country <iso-3></iso-3> code
*
* @param string|null $code
* - [optional] Currency code
*
* @return $this
*/
static public function iso($code = null)
{
$clone = self::clone();
self::$currencyData = self::getCurrencyText($code);
return $clone;
}
/**
* Convert a number to its text representation.
* - Can be able to convert numbers upto <quintillion>
*
* @param string|int|float $number
*
* @param bool|null $cents
* - [optional] Default is false
*
* @return $this
*/
static public function number($number, $cents = null)
{
$clone = self::clone();
if(is_null(self::$allowCents) && is_bool($cents)){
self::$allowCents = $cents;
}
// trim to convert to string
$number = Str::trim($number);
// if cents is allowed
if(self::$allowCents){
// get name of currency
$currencyText = self::$currencyData['name'] ?? null;
// allow if not empty
$currencyText = !empty($currencyText) ? " {$currencyText}" : null;
} else{
$currencyText = null;
}
// convert number to text
$numberText = self::convertNumberToText($number);
// remove line break from text
$numberText = Str::trim(
Str::replace(["\n", "\r"], '', $numberText)
);
// add to text
self::$text = ucfirst($numberText) . $currencyText . self::toCents($number);
return $clone;
}
/**
* Translate text into readable formats
*
* @return string|null
*/
static public function translate()
{
return self::$text;
}
/**
* Convert to cents
*
* @param string $number
* @return string
*/
static private function toCents($number)
{
// if number contain (.) dot
// we treat as cents
if(Str::contains('.', $number)){
$cents = explode('.', $number);
$decimalNumber = isset($cents[1]) ? $cents[1] : null;
if(!empty($decimalNumber) && self::$allowCents){
$centsCurrency = Str::trim(
self::convertNumberToText($decimalNumber)
);
// cents text
$centsText = self::$currencyData['cents'] ?? null;
// allow if not empty
$centsText = !empty($centsText) ? " {$centsText}" : '';
// reset allowCents
self::$allowCents = null;
return ", {$centsCurrency}{$centsText}";
}
}
// reset allowCents
self::$allowCents = null;
}
/**
* Convert the integer part of a number to its text representation.
*
* @param string $number
* @return string
*/
static private function convertNumberToText($number)
{
if (intval($number) == 0) {
return 'zero';
}
return self::convertIntegerToText($number);
}
/**
* Convert an integer to its text representation.
*
* @param string $number
* @return string
*/
static private function convertIntegerToText($number)
{
$result = '';
$i = 0;
while (intval($number) > 0) {
$chunk = intval($number) % 1000;
$number = intval($number) / 1000;
if (intval($chunk) > 0) {
$result = self::convertChunkToText($chunk) . ' ' . self::$units[$i] . ' ' . $result;
}
$i++;
}
return $result;
}
/**
* Convert a chunk of numbers (up to 999) to its text representation.
*
* @param string $number
* @return string
*/
static private function convertChunkToText($number)
{
$result = '';
if (intval($number) >= 100) {
$result .= self::$words[(int)intval($number) / 100] . ' hundred';
$number = intval($number) % 100;
if (intval($number) > 0) {
$result .= ' and ';
}
}
if (intval($number) > 0) {
if (intval($number) < 20) {
$result .= self::$words[intval($number)];
} else {
$result .= self::$tens[(int)intval($number) / 10];
if (intval($number) % 10 > 0) {
$result .= '-' . self::$words[intval($number) % 10];
}
}
}
return $result;
}
/**
* Get the text representation of a currency code.
*
* @param string|null $code
* - [NGA, USD, EUR]
*
* @return array|null
*/
static public function getCurrencyText($code = null)
{
// convert code to upper
$code = Str::upper($code);
// get data
$data = self::currencyNames()[$code] ?? null;
if(is_null($data)){
return;
}
return Str::convertArrayCase($data, 'lower', 'lower');
}
/**
* Clone self
*
* @return $this
*/
static private function clone()
{
return clone new self();
}
}