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
|
7 |
|
if ($currChunk !== 0 || $iterator === 1) {
|
62
|
7 |
|
$preResult .= $this->getExponent($iterator, $currChunk);
|
63
|
|
|
}
|
64
|
7 |
|
return $preResult;
|
65
|
|
|
}
|
66
|
|
|
|
67
|
7 |
|
private function switchArray(int $group)
|
68
|
|
|
{
|
69
|
7 |
|
if ($group === 2) {
|
70
|
7 |
|
$this->data->arrUnits[0] = 'одна ';
|
71
|
7 |
|
$this->data->arrUnits[1] = 'две ';
|
72
|
|
|
|
73
|
7 |
|
return;
|
74
|
|
|
}
|
75
|
7 |
|
$this->data->arrUnits[0] = 'один ';
|
76
|
7 |
|
$this->data->arrUnits[1] = 'два ';
|
77
|
7 |
|
}
|
78
|
|
|
|
79
|
7 |
|
private function makeWords(int $cChunk): string
|
80
|
|
|
{
|
81
|
7 |
|
$resWords = '';
|
82
|
7 |
|
$cent = (int)($cChunk / 100);
|
83
|
7 |
|
$decs = $cChunk % 100;
|
84
|
7 |
|
if ($cent >= 1) {
|
85
|
5 |
|
$resWords = $this->data->arrHundreds[$cent - 1];
|
86
|
|
|
}
|
87
|
7 |
|
if ($decs === 0) {
|
88
|
7 |
|
return $resWords;
|
89
|
|
|
}
|
90
|
7 |
|
if ($decs < 20) {
|
91
|
7 |
|
$resWords .= $this->data->arrUnits[$decs - 1];
|
92
|
|
|
|
93
|
7 |
|
return $resWords;
|
94
|
|
|
}
|
95
|
5 |
|
$resWords .= $this->data->arrTens[$decs / 10 - 1];
|
96
|
5 |
|
if ($decs % 10 !== 0) {
|
97
|
5 |
|
$resWords .= $this->data->arrUnits[$decs % 10 - 1];
|
98
|
|
|
}
|
99
|
|
|
|
100
|
5 |
|
return $resWords;
|
101
|
|
|
}
|
102
|
|
|
|
103
|
7 |
|
private function getExponent(int $chunkPos, int $chunkData): string
|
104
|
|
|
{
|
105
|
7 |
|
if (!$this->currency && $chunkPos === 1) {
|
106
|
6 |
|
return '';
|
107
|
|
|
}
|
108
|
4 |
|
$exponent = $this->data->arrExponents[$chunkPos];
|
109
|
4 |
|
$chunkPos > 3 ? $chunkPos = 3 : true;
|
110
|
4 |
|
$index = $this->getIndex($chunkData % 100);
|
111
|
4 |
|
$suffix = $this->data->arrSuffix[$index][$chunkPos];
|
112
|
|
|
|
113
|
4 |
|
return $exponent . $suffix;
|
114
|
|
|
}
|
115
|
|
|
|
116
|
4 |
|
private function getIndex(int $lastDigits): int
|
117
|
|
|
{
|
118
|
4 |
|
$last = $lastDigits % 10;
|
119
|
4 |
|
if ($lastDigits >= 11 && $lastDigits <= 14) {
|
120
|
3 |
|
return 2;
|
121
|
|
|
}
|
122
|
4 |
|
if ($last === 1) {
|
123
|
4 |
|
return 0;
|
124
|
|
|
}
|
125
|
4 |
|
if ($last >= 2 && $last <= 4) {
|
126
|
4 |
|
return 1;
|
127
|
|
|
}
|
128
|
|
|
|
129
|
3 |
|
return 2;
|
130
|
|
|
}
|
131
|
|
|
}
|
132
|
|
|
|