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
|
8 |
|
public function convert(string $input, bool $show = false): string
|
22
|
|
|
{
|
23
|
8 |
|
$this->currency = $show;
|
24
|
8 |
|
$this->initData($input);
|
25
|
8 |
|
$fullResult = '';
|
26
|
8 |
|
if ($this->iNumber === '0') {
|
27
|
3 |
|
$fullResult = 'ноль ';
|
28
|
3 |
|
$this->sign = '';
|
29
|
|
|
}
|
30
|
8 |
|
$numGroups = count($this->arrChunks);
|
31
|
|
|
|
32
|
8 |
|
return $this->magicConverter($numGroups, $fullResult);
|
33
|
|
|
}
|
34
|
|
|
|
35
|
8 |
|
private function magicConverter(int $numgrps, string $fullres): string
|
36
|
|
|
{
|
37
|
8 |
|
$this->data = new Data();
|
38
|
|
|
|
39
|
8 |
|
for ($i = $numgrps; $i >= 1; $i--) {
|
40
|
8 |
|
$currChunk = (int)strrev($this->arrChunks[$i - 1]);
|
41
|
8 |
|
$this->fixArray($i);
|
42
|
8 |
|
$preResult = $this->makeWords($currChunk);
|
43
|
8 |
|
if ($currChunk !== 0 || $i === 1) {
|
44
|
8 |
|
$preResult .= $this->getRegister($i, $currChunk);
|
45
|
|
|
}
|
46
|
8 |
|
$fullres .= $preResult;
|
47
|
|
|
}
|
48
|
|
|
|
49
|
8 |
|
return $this->sign . $fullres;
|
50
|
|
|
}
|
51
|
|
|
|
52
|
|
|
/**
|
53
|
|
|
* Checks and normalizes input number, defines its sign and returns absolute value.
|
54
|
|
|
* @param string $number - signed input number
|
55
|
|
|
* @property string $sign - sign of a number
|
56
|
|
|
* @return string - unsigned number
|
57
|
|
|
*/
|
58
|
8 |
|
private function initData(string $number): void
|
59
|
|
|
{
|
60
|
8 |
|
$this->sign = '';
|
61
|
8 |
|
if (substr($number, 0, 1) === "-") {
|
62
|
2 |
|
$this->sign = 'минус ';
|
63
|
2 |
|
$number = substr($number, 1);
|
64
|
|
|
}
|
65
|
8 |
|
$this->iNumber = preg_replace("/[^\d]/", "", $number);
|
66
|
|
|
|
67
|
8 |
|
$rvrsValue = strrev($this->iNumber);
|
68
|
8 |
|
$chunks = chunk_split($rvrsValue, 3);
|
69
|
8 |
|
$this->arrChunks = explode("\r\n", rtrim($chunks));
|
70
|
8 |
|
}
|
71
|
|
|
|
72
|
|
|
/**
|
73
|
|
|
* Change data array so that femine names of units are correct (Russian specific language construct)
|
74
|
|
|
* @param int $group - chunk's group in number from the end
|
75
|
|
|
* @internal param \Converter\Init\Data $data - Data object with data arrays.
|
76
|
|
|
*/
|
77
|
8 |
|
private function fixArray(int $group)
|
78
|
|
|
{
|
79
|
8 |
|
if ($group === 2) {
|
80
|
5 |
|
$this->data->arrUnits[0] = 'одна ';
|
81
|
5 |
|
$this->data->arrUnits[1] = 'две ';
|
82
|
|
|
} else {
|
83
|
8 |
|
$this->data->arrUnits[0] = 'один ';
|
84
|
8 |
|
$this->data->arrUnits[1] = 'два ';
|
85
|
|
|
}
|
86
|
8 |
|
}
|
87
|
|
|
|
88
|
8 |
|
private function makeWords(int $cChunk): string
|
89
|
|
|
{
|
90
|
8 |
|
$resWords = '';
|
91
|
8 |
|
$cent = (int)($cChunk / 100);
|
92
|
8 |
|
$decs = $cChunk % 100;
|
93
|
8 |
|
if ($cent >= 1) {
|
94
|
6 |
|
$resWords .= $this->data->arrHundreds[$cent - 1];
|
95
|
|
|
}
|
96
|
8 |
|
if ($decs >= 1 && $decs <= 19) {
|
97
|
8 |
|
$resWords .= $this->data->arrUnits[$decs - 1];
|
98
|
8 |
|
return $resWords;
|
99
|
7 |
|
} elseif ($decs !== 0) {
|
100
|
6 |
|
$resWords .= $this->data->arrTens[$decs / 10 - 1];
|
101
|
|
|
}
|
102
|
7 |
|
if ($decs % 10 !== 0) {
|
103
|
6 |
|
$resWords .= $this->data->arrUnits[$decs % 10 - 1];
|
104
|
|
|
}
|
105
|
|
|
|
106
|
7 |
|
return $resWords;
|
107
|
|
|
}
|
108
|
|
|
|
109
|
8 |
|
private function getRegister(int $chunkPos, int $chunkData): string
|
110
|
|
|
{
|
111
|
8 |
|
if (!$this->currency && $chunkPos === 1) {
|
112
|
7 |
|
return '';
|
113
|
|
|
}
|
114
|
|
|
|
115
|
5 |
|
$lastTwoDigits = $chunkData % 100;
|
116
|
5 |
|
$exponent = $this->data->arrExponents[$chunkPos];
|
117
|
5 |
|
if ($chunkPos > 3) {
|
118
|
3 |
|
$chunkPos = 3;
|
119
|
|
|
}
|
120
|
5 |
|
$index = $this->getSuffix($lastTwoDigits);
|
121
|
5 |
|
$suffix = $this->data->arrSuffix[$index][$chunkPos];
|
122
|
5 |
|
return $exponent . $suffix;
|
123
|
|
|
}
|
124
|
|
|
|
125
|
5 |
|
private function getSuffix(int $lastDigits): int
|
126
|
|
|
{
|
127
|
5 |
|
$last = $lastDigits % 10;
|
128
|
|
|
|
129
|
5 |
|
if ($lastDigits > 10 && $lastDigits < 15) {
|
130
|
3 |
|
return 2;
|
131
|
|
|
}
|
132
|
5 |
|
if ($last === 1) {
|
133
|
4 |
|
return 0;
|
134
|
|
|
}
|
135
|
5 |
|
if ($last >= 2 && $last <= 4) {
|
136
|
4 |
|
return 1;
|
137
|
|
|
}
|
138
|
|
|
|
139
|
4 |
|
return 2;
|
140
|
|
|
}
|
141
|
|
|
}
|
142
|
|
|
|