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->prepNumber($input);
|
25
|
8 |
|
$this->makeChunks();
|
26
|
8 |
|
$numGroups = count($this->arrChunks);
|
27
|
8 |
|
$fullResult = '';
|
28
|
|
|
|
29
|
8 |
|
if ($this->iNumber === '0') {
|
30
|
3 |
|
$fullResult = 'ноль ';
|
31
|
3 |
|
$this->sign = '';
|
32
|
|
|
}
|
33
|
8 |
|
return $this->magicConverter($numGroups, $fullResult);
|
34
|
|
|
}
|
35
|
|
|
|
36
|
8 |
|
private function magicConverter(int $numgrps, string $fullres): string
|
37
|
|
|
{
|
38
|
8 |
|
$this->data = new Data();
|
39
|
|
|
|
40
|
8 |
|
for ($i = $numgrps; $i >= 1; $i--) {
|
41
|
8 |
|
$currChunk = (int)strrev($this->arrChunks[$i - 1]);
|
42
|
8 |
|
$this->fixArray($i);
|
43
|
8 |
|
$preResult = $this->makeWords($currChunk);
|
44
|
8 |
|
if ($currChunk !== 0 || $i === 1) {
|
45
|
8 |
|
$preResult .= $this->getRegister($i, $currChunk);
|
46
|
|
|
}
|
47
|
8 |
|
$fullres .= $preResult;
|
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 prepNumber(string $number): string
|
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 |
|
return $this->iNumber = preg_replace("/[^\d]/", "", $number);
|
66
|
|
|
}
|
67
|
|
|
|
68
|
|
|
/**
|
69
|
|
|
* Creates an array with reversed 3-digit chunks of given number.
|
70
|
|
|
* Example: '1125468' => array['864', '521', '1']
|
71
|
|
|
* @return array
|
72
|
|
|
*/
|
73
|
8 |
|
private function makeChunks()
|
74
|
|
|
{
|
75
|
8 |
|
$rvrsValue = strrev($this->iNumber);
|
76
|
8 |
|
$chunks = chunk_split($rvrsValue, 3);
|
77
|
8 |
|
$this->arrChunks = explode("\r\n", rtrim($chunks));
|
78
|
8 |
|
}
|
79
|
|
|
|
80
|
|
|
/**
|
81
|
|
|
* Change data array so that femine names of units are correct (Russian specific language construct)
|
82
|
|
|
* @param int $fem - flag that indicates chunk index
|
83
|
|
|
* @internal param \Converter\Init\Data $data - Data object with data arrays.
|
84
|
|
|
*/
|
85
|
8 |
|
private function fixArray(int $fem)
|
86
|
|
|
{
|
87
|
8 |
|
if ($fem === 2) {
|
88
|
5 |
|
$this->data->arrUnits[0] = 'одна ';
|
89
|
5 |
|
$this->data->arrUnits[1] = 'две ';
|
90
|
|
|
} else {
|
91
|
8 |
|
$this->data->arrUnits[0] = 'один ';
|
92
|
8 |
|
$this->data->arrUnits[1] = 'два ';
|
93
|
|
|
}
|
94
|
8 |
|
}
|
95
|
|
|
|
96
|
8 |
|
private function makeWords(int $cChunk): string
|
97
|
|
|
{
|
98
|
8 |
|
$resWords = '';
|
99
|
8 |
|
$cent = (int)($cChunk / 100);
|
100
|
8 |
|
$decs = $cChunk % 100;
|
101
|
8 |
|
if ($cent >= 1) {
|
102
|
6 |
|
$resWords .= $this->data->arrHundreds[$cent - 1];
|
103
|
|
|
}
|
104
|
8 |
|
if ($decs >= 1 && $decs <= 19) {
|
105
|
8 |
|
$resWords .= $this->data->arrUnits[$decs - 1];
|
106
|
8 |
|
$decs = 0;
|
107
|
|
|
}
|
108
|
8 |
|
if ($decs !== 0) {
|
109
|
6 |
|
$resWords .= $this->data->arrTens[$decs / 10 - 1];
|
110
|
|
|
}
|
111
|
8 |
|
if ($decs % 10 !== 0) {
|
112
|
6 |
|
$resWords .= $this->data->arrUnits[$decs % 10 - 1];
|
113
|
|
|
}
|
114
|
8 |
|
return $resWords;
|
115
|
|
|
}
|
116
|
|
|
|
117
|
8 |
|
private function getRegister(int $chunkPos, int $chunkData): string
|
118
|
|
|
{
|
119
|
8 |
|
$subResult = '';
|
120
|
8 |
|
$lastDigits = $chunkData % 100;
|
121
|
8 |
|
$exponent = $this->data->arrExponents[$chunkPos];
|
122
|
|
|
|
123
|
8 |
|
if (!$this->currency && $chunkPos === 1) {
|
124
|
7 |
|
return $subResult;
|
125
|
|
|
}
|
126
|
5 |
|
return $exponent . $this->getSuffix($lastDigits, $chunkPos);
|
127
|
|
|
}
|
128
|
|
|
|
129
|
5 |
|
private function getSuffix(int $lastDigits, int $group): string
|
130
|
|
|
{
|
131
|
5 |
|
if ($group > 3) {
|
132
|
3 |
|
$group = 3;
|
133
|
|
|
}
|
134
|
5 |
|
$last = $lastDigits % 10;
|
135
|
5 |
|
$result = $this->data->arrSuffix[2][$group];
|
136
|
5 |
|
if ($lastDigits >= 11 && $lastDigits <= 14) {
|
137
|
3 |
|
true;
|
138
|
5 |
|
} elseif ($last === 1) {
|
139
|
4 |
|
$result = $this->data->arrSuffix[0][$group];
|
140
|
5 |
|
} elseif ($last >= 2 && $last <= 4) {
|
141
|
4 |
|
$result = $this->data->arrSuffix[1][$group];
|
142
|
|
|
}
|
143
|
5 |
|
return $result;
|
144
|
|
|
}
|
145
|
|
|
}
|
146
|
|
|
|