1
|
|
|
<<<<<<< Updated upstream |
|
|
|
|
2
|
|
|
<?php |
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Converter\Core; |
6
|
|
|
|
7
|
|
|
use Converter\Init\Data; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Converts a number up to 1e+510 to its text representation e.g. 312 -> триста двенадцать (Russian only). |
11
|
|
|
* |
12
|
|
|
* @author Sergey Kanashin <[email protected]> |
13
|
|
|
* @copyright 2003-2017 |
14
|
|
|
*/ |
15
|
|
|
final class Number2Text |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var mixed array |
20
|
|
|
*/ |
21
|
|
|
private $data; |
22
|
|
|
|
23
|
|
|
private $iNumber; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Flag for whether to show currency text or not |
27
|
|
|
* |
28
|
|
|
* @var bool |
29
|
|
|
*/ |
30
|
|
|
private $currency; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Array of triplets |
34
|
|
|
* |
35
|
|
|
* @var mixed array |
36
|
|
|
*/ |
37
|
|
|
private $arrChunks; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Sets the visibility of currency name |
41
|
|
|
* |
42
|
|
|
* @param bool $show |
43
|
|
|
*/ |
44
|
|
|
public function currency(bool $show = true) |
45
|
|
|
{ |
46
|
|
|
$this->currency = $show; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function convert(string $input): string |
50
|
|
|
{ |
51
|
|
|
$this->initData($input); |
52
|
|
|
$input === '0' ? $fullResult = 'ноль ' : $fullResult = ''; |
53
|
|
|
$numGroups = count($this->arrChunks); |
54
|
|
|
|
55
|
|
|
$this->data = new Data(); |
56
|
|
|
|
57
|
|
|
for ($i = $numGroups; $i >= 1; $i--) { |
58
|
|
|
$fullResult .= $this->getWords($i); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $fullResult; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
private function initData(string $number) |
65
|
|
|
{ |
66
|
|
|
$this->iNumber = preg_replace("/[^\d]/", "", $number); |
67
|
|
|
$rvrsValue = strrev($this->iNumber); |
68
|
|
|
$chunks = chunk_split($rvrsValue, 3); |
69
|
|
|
$this->arrChunks = explode("\r\n", $chunks); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
private function getWords(int $iterator): string |
73
|
|
|
{ |
74
|
|
|
$currChunk = (int)strrev($this->arrChunks[$iterator - 1]); |
75
|
|
|
$iterator < 3 ? $this->switchArray($iterator) : true; |
76
|
|
|
$preResult = $this->makeWords($currChunk); |
77
|
|
|
|
78
|
|
|
if ($currChunk !== 0 || $iterator === 1) { |
79
|
|
|
$preResult .= $this->getExponent($iterator, $currChunk); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $preResult; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Depending on group's gender switches array to reflect that |
87
|
|
|
* |
88
|
|
|
* @param int $group |
89
|
|
|
*/ |
90
|
|
|
private function switchArray(int $group) |
91
|
|
|
{ |
92
|
|
|
if ($group === 2) { |
93
|
|
|
$this->data->arrUnits[0] = 'одна '; |
94
|
|
|
$this->data->arrUnits[1] = 'две '; |
95
|
|
|
|
96
|
|
|
return; |
97
|
|
|
} |
98
|
|
|
$this->data->arrUnits[0] = 'один '; |
99
|
|
|
$this->data->arrUnits[1] = 'два '; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
private function makeWords(int $cChunk): string |
103
|
|
|
{ |
104
|
|
|
$decs = $cChunk % 100; |
105
|
|
|
$resWords = $this->getCentum($cChunk); |
106
|
|
|
|
107
|
|
|
if ($decs === 0) { |
108
|
|
|
return $resWords; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$resWords .= $this->getDecem($decs); |
112
|
|
|
|
113
|
|
|
return $resWords; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
private function getCentum(int $chunk): string |
117
|
|
|
{ |
118
|
|
|
$cent = (int)($chunk / 100); |
119
|
|
|
|
120
|
|
|
if ($cent >= 1) { |
121
|
|
|
return $this->data->arrHundreds[$cent - 1]; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return ''; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
private function getDecem(int $decs): string |
128
|
|
|
{ |
129
|
|
|
$result = ''; |
130
|
|
|
if ($decs < 20) { |
131
|
|
|
$result .= $this->data->arrUnits[$decs - 1]; |
132
|
|
|
|
133
|
|
|
return $result; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$result .= $this->data->arrTens[$decs / 10 - 1]; |
137
|
|
|
|
138
|
|
|
if ($decs % 10 !== 0) { |
139
|
|
|
$result .= $this->data->arrUnits[$decs % 10 - 1]; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return $result; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
private function getExponent(int $chunkPos, int $chunkData): string |
146
|
|
|
{ |
147
|
|
|
if (!$this->currency && $chunkPos === 1) { |
148
|
|
|
return ''; |
149
|
|
|
} |
150
|
|
|
$exponent = $this->data->arrExponents[$chunkPos]; |
151
|
|
|
$chunkPos > 3 ? $chunkPos = 3 : true; |
152
|
|
|
$index = $this->getIndex($chunkData % 100); |
153
|
|
|
$suffix = $this->data->arrSuffix[$index][$chunkPos]; |
154
|
|
|
|
155
|
|
|
return $exponent . $suffix; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
private function getIndex(int $lastDigits): int |
159
|
|
|
{ |
160
|
|
|
$last = $lastDigits % 10; |
161
|
|
|
|
162
|
|
|
if ($lastDigits >= 11 && $lastDigits <= 14) { |
163
|
|
|
return 2; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return $this->checkSingleChunk($last); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function checkSingleChunk(int $digit): int |
170
|
|
|
{ |
171
|
|
|
if ($digit === 1) { |
172
|
|
|
return 0; |
173
|
|
|
} |
174
|
|
|
if ($digit >= 2 && $digit <= 4) { |
175
|
|
|
return 1; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return 2; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
======= |
|
|
|
|
182
|
|
|
<?php
|
183
|
|
|
declare(strict_types = 1);
|
184
|
|
|
|
185
|
|
|
namespace Converter\Core;
|
186
|
|
|
|
187
|
|
|
use Converter\Init\Data;
|
|
|
|
|
188
|
|
|
|
189
|
|
|
/**
|
190
|
|
|
* Converts a number up to 1e+510 to its text representation e.g. 312 -> триста двенадцать (Russian only).
|
191
|
|
|
*
|
192
|
|
|
* @author Sergey Kanashin <[email protected]>
|
193
|
|
|
* @copyright 2003-2017
|
194
|
|
|
*/
|
195
|
|
|
final class Number2Text
|
|
|
|
|
196
|
|
|
{
|
|
|
|
|
197
|
|
|
|
198
|
|
|
/**
|
199
|
|
|
* Contains all the language specific data for Number2Text class
|
200
|
|
|
*
|
201
|
|
|
* @var object
|
202
|
|
|
*/
|
203
|
|
|
private $data;
|
204
|
|
|
|
205
|
|
|
/**
|
206
|
|
|
* The number to be converted to text
|
207
|
|
|
*
|
208
|
|
|
* @var string
|
209
|
|
|
*/
|
210
|
|
|
private $iNumber;
|
211
|
|
|
|
212
|
|
|
/**
|
213
|
|
|
* Flag that indicates whether to show currency name
|
214
|
|
|
*
|
215
|
|
|
* @var bool
|
216
|
|
|
*/
|
217
|
|
|
private $currency;
|
218
|
|
|
|
219
|
|
|
/**
|
220
|
|
|
* Array of triplets
|
221
|
|
|
*
|
222
|
|
|
* @var mixed array
|
223
|
|
|
*/
|
224
|
|
|
private $arrChunks;
|
225
|
|
|
|
226
|
|
|
/**
|
227
|
|
|
* Number2Text constructor. Implements loading of functional data.
|
228
|
|
|
*/
|
229
|
|
|
public function __construct()
|
230
|
|
|
{
|
231
|
|
|
$this->data = new Data();
|
232
|
|
|
}
|
233
|
|
|
|
234
|
|
|
/**
|
235
|
|
|
* Sets the visibility of currency name
|
236
|
|
|
*
|
237
|
|
|
* @param bool $show
|
238
|
|
|
*/
|
239
|
|
|
public function currency(bool $show = true)
|
240
|
|
|
{
|
241
|
|
|
$this->currency = $show;
|
242
|
|
|
}
|
243
|
|
|
|
244
|
|
|
public function convert(string $input): string
|
245
|
|
|
{
|
246
|
|
|
$this->initData($input);
|
247
|
|
|
$input === '0' ? $fres[0] = 'ноль ' : $fres = [];
|
248
|
|
|
|
249
|
|
|
return implode($this->fetchData($fres));
|
250
|
|
|
}
|
251
|
|
|
|
252
|
|
|
public function fetchData(array $fullResult): array
|
253
|
|
|
{
|
254
|
|
|
$numGroups = count($this->arrChunks);
|
255
|
|
|
|
256
|
|
|
for ($i = $numGroups; $i >= 1; $i--) {
|
257
|
|
|
$fullResult[] = $this->getWords($i);
|
258
|
|
|
}
|
259
|
|
|
|
260
|
|
|
return $fullResult;
|
261
|
|
|
}
|
262
|
|
|
|
263
|
|
|
private function initData(string $number)
|
264
|
|
|
{
|
265
|
|
|
$this->iNumber = preg_replace("/[^\d]/", "", $number);
|
266
|
|
|
$rvrsValue = strrev($this->iNumber);
|
267
|
|
|
$chunks = chunk_split($rvrsValue, 3);
|
268
|
|
|
$this->arrChunks = explode("\r\n", $chunks);
|
269
|
|
|
}
|
270
|
|
|
|
271
|
|
|
private function getWords(int $iterator): string
|
272
|
|
|
{
|
273
|
|
|
$currChunk = (int)strrev($this->arrChunks[$iterator - 1]);
|
274
|
|
|
$iterator < 3 ? $this->switchArray($iterator) : true;
|
275
|
|
|
$preResult = $this->makeWords($currChunk);
|
276
|
|
|
|
277
|
|
|
if ($currChunk !== 0 || $iterator === 1) {
|
278
|
|
|
$preResult .= $this->getExponent($iterator, $currChunk);
|
279
|
|
|
}
|
280
|
|
|
|
281
|
|
|
return $preResult;
|
282
|
|
|
}
|
283
|
|
|
|
284
|
|
|
/**
|
285
|
|
|
* Depending on group's gender switches array to adjust that
|
286
|
|
|
*
|
287
|
|
|
* @param int $group
|
288
|
|
|
*/
|
289
|
|
|
private function switchArray(int $group)
|
290
|
|
|
{
|
291
|
|
|
if ($group === 2) {
|
292
|
|
|
$this->data->arrUnits[0] = 'одна ';
|
293
|
|
|
$this->data->arrUnits[1] = 'две ';
|
294
|
|
|
|
295
|
|
|
return;
|
296
|
|
|
}
|
297
|
|
|
$this->data->arrUnits[0] = 'один ';
|
298
|
|
|
$this->data->arrUnits[1] = 'два ';
|
299
|
|
|
}
|
300
|
|
|
|
301
|
|
|
private function makeWords(int $cChunk): string
|
302
|
|
|
{
|
303
|
|
|
$decs = $cChunk % 100;
|
304
|
|
|
$resWords = $this->getCentum($cChunk);
|
305
|
|
|
|
306
|
|
|
if ($decs === 0) {
|
307
|
|
|
return $resWords;
|
308
|
|
|
}
|
309
|
|
|
|
310
|
|
|
$resWords .= $this->getDecem($decs);
|
311
|
|
|
|
312
|
|
|
return $resWords;
|
313
|
|
|
}
|
314
|
|
|
|
315
|
|
|
private function getCentum(int $chunk): string
|
316
|
|
|
{
|
317
|
|
|
$cent = (int)($chunk / 100);
|
318
|
|
|
|
319
|
|
|
if ($cent >= 1) {
|
320
|
|
|
return $this->data->arrHundreds[$cent - 1];
|
321
|
|
|
}
|
322
|
|
|
|
323
|
|
|
return '';
|
324
|
|
|
}
|
325
|
|
|
|
326
|
|
|
private function getDecem(int $decs): string
|
327
|
|
|
{
|
328
|
|
|
$result = '';
|
329
|
|
|
if ($decs < 20) {
|
330
|
|
|
$result .= $this->data->arrUnits[$decs - 1];
|
331
|
|
|
|
332
|
|
|
return $result;
|
333
|
|
|
}
|
334
|
|
|
|
335
|
|
|
$result .= $this->data->arrTens[$decs / 10 - 1];
|
336
|
|
|
|
337
|
|
|
if ($decs % 10 !== 0) {
|
338
|
|
|
$result .= $this->data->arrUnits[$decs % 10 - 1];
|
339
|
|
|
}
|
340
|
|
|
|
341
|
|
|
return $result;
|
342
|
|
|
}
|
343
|
|
|
|
344
|
|
|
private function getExponent(int $chunkPos, int $chunkData): string
|
345
|
|
|
{
|
346
|
|
|
if (!$this->currency && $chunkPos === 1) {
|
347
|
|
|
return '';
|
348
|
|
|
}
|
349
|
|
|
$exponent = $this->data->arrExponents[$chunkPos];
|
350
|
|
|
$chunkPos > 3 ? $chunkPos = 3 : true;
|
351
|
|
|
$index = $this->getIndex($chunkData % 100);
|
352
|
|
|
$suffix = $this->data->arrSuffix[$index][$chunkPos];
|
353
|
|
|
|
354
|
|
|
return $exponent . $suffix;
|
355
|
|
|
}
|
356
|
|
|
|
357
|
|
|
private function getIndex(int $lastDigits): int
|
358
|
|
|
{
|
359
|
|
|
$last = $lastDigits % 10;
|
360
|
|
|
|
361
|
|
|
if ($lastDigits >= 11 && $lastDigits <= 14) {
|
362
|
|
|
return 2;
|
363
|
|
|
}
|
364
|
|
|
|
365
|
|
|
return $this->checkSingleChunk($last);
|
366
|
|
|
}
|
367
|
|
|
|
368
|
|
|
public function checkSingleChunk(int $digit): int
|
369
|
|
|
{
|
370
|
|
|
if ($digit === 1) {
|
371
|
|
|
return 0;
|
372
|
|
|
}
|
373
|
|
|
if ($digit >= 2 && $digit <= 4) {
|
374
|
|
|
return 1;
|
375
|
|
|
}
|
376
|
|
|
|
377
|
|
|
return 2;
|
378
|
|
|
}
|
379
|
|
|
}
|
380
|
|
|
>>>>>>> Stashed changes |
381
|
|
|
|