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 $sign;
|
19
|
|
|
private $arrChunks;
|
20
|
|
|
|
21
|
1 |
|
public function currency(bool $show = false)
|
22
|
|
|
{
|
23
|
1 |
|
$this->currency = $show;
|
24
|
1 |
|
}
|
25
|
|
|
|
26
|
8 |
|
public function convert(string $input): string
|
27
|
|
|
{
|
28
|
8 |
|
$this->initData($input);
|
29
|
8 |
|
$fullResult = '';
|
30
|
8 |
|
if ($this->iNumber === '0') {
|
31
|
3 |
|
$fullResult = 'ноль ';
|
32
|
3 |
|
$this->sign = '';
|
33
|
|
|
}
|
34
|
8 |
|
$numGroups = count($this->arrChunks);
|
35
|
|
|
|
36
|
8 |
|
return $this->magicConverter($numGroups, $fullResult);
|
37
|
|
|
}
|
38
|
|
|
|
39
|
8 |
|
private function initData(string $number)
|
40
|
|
|
{
|
41
|
8 |
|
$this->sign = '';
|
42
|
8 |
|
if (substr($number, 0, 1) === "-") {
|
43
|
2 |
|
$this->sign = 'минус ';
|
44
|
2 |
|
$number = substr($number, 1);
|
45
|
|
|
}
|
46
|
8 |
|
$this->iNumber = preg_replace("/[^\d]/", "", $number);
|
47
|
|
|
|
48
|
8 |
|
$rvrsValue = strrev($this->iNumber);
|
49
|
8 |
|
$chunks = chunk_split($rvrsValue, 3);
|
50
|
8 |
|
$this->arrChunks = explode("\r\n", $chunks);
|
51
|
8 |
|
}
|
52
|
|
|
|
53
|
8 |
|
private function magicConverter(int $numgrps, string $fullres): string
|
54
|
|
|
{
|
55
|
8 |
|
$this->data = new Data();
|
56
|
|
|
|
57
|
8 |
|
for ($i = $numgrps; $i >= 1; $i--) {
|
58
|
8 |
|
$currChunk = (int)strrev($this->arrChunks[$i - 1]);
|
59
|
8 |
|
$i < 3 ? $this->switchArray($i) : true;
|
60
|
8 |
|
$preResult = $this->makeWords($currChunk);
|
61
|
8 |
|
if ($currChunk !== 0 || $i === 1) {
|
62
|
8 |
|
$preResult .= $this->getExponent($i, $currChunk);
|
63
|
|
|
}
|
64
|
8 |
|
$fullres .= $preResult;
|
65
|
|
|
}
|
66
|
|
|
|
67
|
8 |
|
return $this->sign . $fullres;
|
68
|
|
|
}
|
69
|
|
|
|
70
|
8 |
|
private function switchArray(int $group)
|
71
|
|
|
{
|
72
|
8 |
|
if ($group === 2) {
|
73
|
8 |
|
$this->data->arrUnits[0] = 'одна ';
|
74
|
8 |
|
$this->data->arrUnits[1] = 'две ';
|
75
|
8 |
|
return;
|
76
|
|
|
}
|
77
|
8 |
|
$this->data->arrUnits[0] = 'один ';
|
78
|
8 |
|
$this->data->arrUnits[1] = 'два ';
|
79
|
8 |
|
}
|
80
|
|
|
|
81
|
8 |
|
private function makeWords(int $cChunk): string
|
82
|
|
|
{
|
83
|
8 |
|
$resWords = '';
|
84
|
8 |
|
$cent = (int)($cChunk / 100);
|
85
|
8 |
|
$decs = $cChunk % 100;
|
86
|
8 |
|
if ($cent >= 1) {
|
87
|
6 |
|
$resWords = $this->data->arrHundreds[$cent - 1];
|
88
|
|
|
}
|
89
|
8 |
|
if ($decs === 0) {
|
90
|
8 |
|
return $resWords;
|
91
|
|
|
}
|
92
|
8 |
|
if ($decs < 20) {
|
93
|
8 |
|
$resWords .= $this->data->arrUnits[$decs - 1];
|
94
|
8 |
|
return $resWords;
|
95
|
|
|
} else {
|
96
|
6 |
|
$resWords .= $this->data->arrTens[$decs / 10 - 1];
|
97
|
|
|
}
|
98
|
6 |
|
if ($decs % 10 !== 0) {
|
99
|
6 |
|
$resWords .= $this->data->arrUnits[$decs % 10 - 1];
|
100
|
|
|
}
|
101
|
|
|
|
102
|
6 |
|
return $resWords;
|
103
|
|
|
}
|
104
|
|
|
|
105
|
8 |
|
private function getExponent(int $chunkPos, int $chunkData): string
|
106
|
|
|
{
|
107
|
8 |
|
if (!$this->currency && $chunkPos === 1) {
|
108
|
7 |
|
return '';
|
109
|
|
|
}
|
110
|
5 |
|
$exponent = $this->data->arrExponents[$chunkPos];
|
111
|
5 |
|
$chunkPos > 3 ? $chunkPos = 3 : true;
|
112
|
5 |
|
$index = $this->getIndex($chunkData % 100);
|
113
|
5 |
|
$suffix = $this->data->arrSuffix[$index][$chunkPos];
|
114
|
|
|
|
115
|
5 |
|
return $exponent . $suffix;
|
116
|
|
|
}
|
117
|
|
|
|
118
|
5 |
|
private function getIndex(int $lastDigits): int
|
119
|
|
|
{
|
120
|
5 |
|
$last = $lastDigits % 10;
|
121
|
5 |
|
if ($lastDigits >= 11 && $lastDigits <= 14) {
|
122
|
3 |
|
return 2;
|
123
|
|
|
}
|
124
|
5 |
|
if ($last === 1) {
|
125
|
4 |
|
return 0;
|
126
|
|
|
}
|
127
|
5 |
|
if ($last >= 2 && $last <= 4) {
|
128
|
4 |
|
return 1;
|
129
|
|
|
}
|
130
|
|
|
|
131
|
4 |
|
return 2;
|
132
|
|
|
}
|
133
|
|
|
}
|
134
|
|
|
|