1
|
|
|
<?php
|
2
|
|
|
declare(strict_types = 1);
|
3
|
|
|
|
4
|
|
|
namespace Converter\Core;
|
5
|
|
|
|
6
|
|
|
use Converter\Init\Data;
|
7
|
|
|
|
8
|
|
|
/**
|
9
|
|
|
* Converts a number (up to 1e+510) to its text representation e.g. 312 -> триста двенадцать (Russian only).
|
10
|
|
|
* @author Sergey Kanashin <[email protected]>
|
11
|
|
|
* @copyright 2003-2017
|
12
|
|
|
*/
|
13
|
|
|
final class Number2Text
|
14
|
|
|
{
|
15
|
|
|
private $data;
|
16
|
|
|
private $iNumber;
|
17
|
|
|
private $currency;
|
18
|
|
|
private $arrChunks;
|
19
|
|
|
|
20
|
1 |
|
public function currency(bool $show = true)
|
21
|
|
|
{
|
22
|
1 |
|
$this->currency = $show;
|
23
|
1 |
|
}
|
24
|
|
|
|
25
|
7 |
|
public function convert(string $input): string
|
26
|
|
|
{
|
27
|
7 |
|
$this->initData($input);
|
28
|
7 |
|
$fullResult = '';
|
29
|
7 |
|
if ($this->iNumber === '0') {
|
30
|
2 |
|
$fullResult = 'ноль ';
|
31
|
|
|
}
|
32
|
7 |
|
$numGroups = count($this->arrChunks);
|
33
|
|
|
|
34
|
7 |
|
return $this->magicConverter($numGroups, $fullResult);
|
35
|
|
|
}
|
36
|
|
|
|
37
|
7 |
|
private function initData(string $number)
|
38
|
|
|
{
|
39
|
7 |
|
$this->iNumber = preg_replace("/[^\d]/", "", $number);
|
40
|
7 |
|
$rvrsValue = strrev($this->iNumber);
|
41
|
7 |
|
$chunks = chunk_split($rvrsValue, 3);
|
42
|
7 |
|
$this->arrChunks = explode("\r\n", $chunks);
|
43
|
7 |
|
}
|
44
|
|
|
|
45
|
7 |
|
private function magicConverter(int $numgrps, string $fullres): string
|
46
|
|
|
{
|
47
|
7 |
|
$this->data = new Data();
|
48
|
|
|
|
49
|
7 |
|
for ($i = $numgrps; $i >= 1; $i--) {
|
50
|
7 |
|
$fullres .= $this->getWords($i);
|
51
|
|
|
}
|
52
|
|
|
|
53
|
7 |
|
return $fullres;
|
54
|
|
|
}
|
55
|
|
|
|
56
|
7 |
|
private function getWords(int $iterator): string
|
57
|
|
|
{
|
58
|
7 |
|
$currChunk = (int)strrev($this->arrChunks[$iterator - 1]);
|
59
|
7 |
|
$iterator < 3 ? $this->switchArray($iterator) : true;
|
60
|
7 |
|
$preResult = $this->makeWords($currChunk);
|
61
|
|
|
|
62
|
7 |
|
if ($currChunk !== 0 || $iterator === 1) {
|
63
|
7 |
|
$preResult .= $this->getExponent($iterator, $currChunk);
|
64
|
|
|
}
|
65
|
|
|
|
66
|
7 |
|
return $preResult;
|
67
|
|
|
}
|
68
|
|
|
|
69
|
7 |
|
private function switchArray(int $group)
|
70
|
|
|
{
|
71
|
7 |
|
if ($group === 2) {
|
72
|
7 |
|
$this->data->arrUnits[0] = 'одна ';
|
73
|
7 |
|
$this->data->arrUnits[1] = 'две ';
|
74
|
|
|
|
75
|
7 |
|
return;
|
76
|
|
|
}
|
77
|
7 |
|
$this->data->arrUnits[0] = 'один ';
|
78
|
7 |
|
$this->data->arrUnits[1] = 'два ';
|
79
|
7 |
|
}
|
80
|
|
|
|
81
|
7 |
|
private function makeWords(int $cChunk): string
|
82
|
|
|
{
|
83
|
7 |
|
$decs = $cChunk % 100;
|
84
|
7 |
|
$resWords = $this->getCentum($cChunk);
|
85
|
|
|
|
86
|
7 |
|
if ($decs === 0) {
|
87
|
7 |
|
return $resWords;
|
88
|
|
|
}
|
89
|
|
|
|
90
|
7 |
|
$resWords .= $this->getDecem($decs);
|
91
|
|
|
|
92
|
7 |
|
return $resWords;
|
93
|
|
|
}
|
94
|
|
|
|
95
|
7 |
|
private function getCentum(int $chunk): string
|
96
|
|
|
{
|
97
|
7 |
|
$cent = (int)($chunk / 100);
|
98
|
|
|
|
99
|
7 |
|
if ($cent >= 1) {
|
100
|
5 |
|
return $this->data->arrHundreds[$cent - 1];
|
101
|
|
|
}
|
102
|
|
|
|
103
|
7 |
|
return '';
|
104
|
|
|
}
|
105
|
|
|
|
106
|
7 |
|
private function getDecem(int $decs): string
|
107
|
|
|
{
|
108
|
7 |
|
$result = '';
|
109
|
7 |
|
if ($decs < 20) {
|
110
|
7 |
|
$result .= $this->data->arrUnits[$decs - 1];
|
111
|
|
|
|
112
|
7 |
|
return $result;
|
113
|
|
|
}
|
114
|
|
|
|
115
|
5 |
|
$result .= $this->data->arrTens[$decs / 10 - 1];
|
116
|
|
|
|
117
|
5 |
|
if ($decs % 10 !== 0) {
|
118
|
5 |
|
$result .= $this->data->arrUnits[$decs % 10 - 1];
|
119
|
|
|
}
|
120
|
5 |
|
return $result;
|
121
|
|
|
}
|
122
|
|
|
|
123
|
7 |
|
private function getExponent(int $chunkPos, int $chunkData): string
|
124
|
|
|
{
|
125
|
7 |
|
if (!$this->currency && $chunkPos === 1) {
|
126
|
6 |
|
return '';
|
127
|
|
|
}
|
128
|
4 |
|
$exponent = $this->data->arrExponents[$chunkPos];
|
129
|
4 |
|
$chunkPos > 3 ? $chunkPos = 3 : true;
|
130
|
4 |
|
$index = $this->getIndex($chunkData % 100);
|
131
|
4 |
|
$suffix = $this->data->arrSuffix[$index][$chunkPos];
|
132
|
|
|
|
133
|
4 |
|
return $exponent . $suffix;
|
134
|
|
|
}
|
135
|
|
|
|
136
|
4 |
|
private function getIndex(int $lastDigits): int
|
137
|
|
|
{
|
138
|
4 |
|
$last = $lastDigits % 10;
|
139
|
4 |
|
if ($lastDigits >= 11 && $lastDigits <= 14) {
|
140
|
3 |
|
return 2;
|
141
|
|
|
}
|
142
|
4 |
|
if ($last === 1) {
|
143
|
4 |
|
return 0;
|
144
|
|
|
}
|
145
|
4 |
|
if ($last >= 2 && $last <= 4) {
|
146
|
4 |
|
return 1;
|
147
|
|
|
}
|
148
|
|
|
|
149
|
3 |
|
return 2;
|
150
|
|
|
}
|
151
|
|
|
}
|
152
|
|
|
|