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
|
|
|
*
|
11
|
|
|
* @author Sergey Kanashin <[email protected]>
|
12
|
|
|
* @copyright 2003-2017
|
13
|
|
|
*/
|
14
|
|
|
final class Number2Text
|
15
|
|
|
{
|
16
|
|
|
|
17
|
|
|
/**
|
18
|
|
|
* Contains all the language specific data for Number2Text class
|
19
|
|
|
*
|
20
|
|
|
* @var object
|
21
|
|
|
*/
|
22
|
|
|
private $data;
|
23
|
|
|
|
24
|
|
|
/**
|
25
|
|
|
* The number to be converted to text
|
26
|
|
|
*
|
27
|
|
|
* @var string
|
28
|
|
|
*/
|
29
|
|
|
private $iNumber;
|
30
|
|
|
|
31
|
|
|
/**
|
32
|
|
|
* Flag that indicates whether to show currency name
|
33
|
|
|
*
|
34
|
|
|
* @var bool
|
35
|
|
|
*/
|
36
|
|
|
private $currency;
|
37
|
|
|
|
38
|
|
|
/**
|
39
|
|
|
* Array of triplets
|
40
|
|
|
*
|
41
|
|
|
* @var mixed array
|
42
|
|
|
*/
|
43
|
|
|
private $arrChunks;
|
44
|
|
|
|
45
|
|
|
/**
|
46
|
|
|
* Number2Text constructor. Implements loading of functional data.
|
47
|
|
|
*/
|
48
|
7 |
|
public function __construct()
|
49
|
|
|
{
|
50
|
7 |
|
$this->data = new Data();
|
51
|
7 |
|
}
|
52
|
|
|
|
53
|
|
|
/**
|
54
|
|
|
* Sets the visibility of currency name
|
55
|
|
|
*
|
56
|
|
|
* @param bool $show
|
57
|
|
|
*/
|
58
|
1 |
|
public function currency(bool $show = true)
|
59
|
|
|
{
|
60
|
1 |
|
$this->currency = $show;
|
61
|
1 |
|
}
|
62
|
|
|
|
63
|
6 |
|
public function convert(string $input): string
|
64
|
|
|
{
|
65
|
6 |
|
$this->initData($input);
|
66
|
6 |
|
$input === '0' ? $fres[0] = 'ноль ' : $fres = [];
|
|
|
|
|
67
|
|
|
|
68
|
6 |
|
return implode($this->fetchData($fres));
|
69
|
|
|
}
|
70
|
|
|
|
71
|
6 |
|
public function fetchData(array $fullResult): array
|
72
|
|
|
{
|
73
|
6 |
|
$numGroups = count($this->arrChunks);
|
74
|
|
|
|
75
|
6 |
|
for ($i = $numGroups; $i >= 1; $i--) {
|
76
|
6 |
|
$fullResult[] = $this->getWords($i);
|
77
|
|
|
}
|
78
|
|
|
|
79
|
6 |
|
return $fullResult;
|
80
|
|
|
}
|
81
|
|
|
|
82
|
6 |
|
private function initData(string $number)
|
83
|
|
|
{
|
84
|
6 |
|
$this->iNumber = preg_replace("/[^\d]/", "", $number);
|
85
|
6 |
|
$rvrsValue = strrev($this->iNumber);
|
86
|
6 |
|
$chunks = chunk_split($rvrsValue, 3);
|
87
|
6 |
|
$this->arrChunks = explode("\r\n", $chunks);
|
88
|
6 |
|
}
|
89
|
|
|
|
90
|
6 |
|
private function getWords(int $iterator): string
|
91
|
|
|
{
|
92
|
6 |
|
$currChunk = (int)strrev($this->arrChunks[$iterator - 1]);
|
93
|
6 |
|
$iterator < 3 ? $this->switchArray($iterator) : true;
|
94
|
6 |
|
$preResult = $this->makeWords($currChunk);
|
95
|
|
|
|
96
|
6 |
|
if ($currChunk !== 0 || $iterator === 1) {
|
97
|
6 |
|
$preResult .= $this->getExponent($iterator, $currChunk);
|
98
|
|
|
}
|
99
|
|
|
|
100
|
6 |
|
return $preResult;
|
101
|
|
|
}
|
102
|
|
|
|
103
|
|
|
/**
|
104
|
|
|
* Depending on group's gender switches array to adjust that
|
105
|
|
|
*
|
106
|
|
|
* @param int $group
|
107
|
|
|
*/
|
108
|
6 |
|
private function switchArray(int $group)
|
109
|
|
|
{
|
110
|
6 |
|
if ($group === 2) {
|
111
|
6 |
|
$this->data->arrUnits[0] = 'одна ';
|
112
|
6 |
|
$this->data->arrUnits[1] = 'две ';
|
113
|
|
|
|
114
|
6 |
|
return;
|
115
|
|
|
}
|
116
|
6 |
|
$this->data->arrUnits[0] = 'один ';
|
117
|
6 |
|
$this->data->arrUnits[1] = 'два ';
|
118
|
6 |
|
}
|
119
|
|
|
|
120
|
6 |
|
private function makeWords(int $cChunk): string
|
121
|
|
|
{
|
122
|
6 |
|
$decs = $cChunk % 100;
|
123
|
6 |
|
$resWords = $this->getCentum($cChunk);
|
124
|
|
|
|
125
|
6 |
|
if ($decs === 0) {
|
126
|
6 |
|
return $resWords;
|
127
|
|
|
}
|
128
|
|
|
|
129
|
6 |
|
$resWords .= $this->getDecem($decs);
|
130
|
|
|
|
131
|
6 |
|
return $resWords;
|
132
|
|
|
}
|
133
|
|
|
|
134
|
6 |
|
private function getCentum(int $chunk): string
|
135
|
|
|
{
|
136
|
6 |
|
$cent = (int)($chunk / 100);
|
137
|
|
|
|
138
|
6 |
|
if ($cent >= 1) {
|
139
|
4 |
|
return $this->data->arrHundreds[$cent - 1];
|
140
|
|
|
}
|
141
|
|
|
|
142
|
6 |
|
return '';
|
143
|
|
|
}
|
144
|
|
|
|
145
|
6 |
|
private function getDecem(int $decs): string
|
146
|
|
|
{
|
147
|
6 |
|
$result = '';
|
148
|
6 |
|
if ($decs < 20) {
|
149
|
6 |
|
$result .= $this->data->arrUnits[$decs - 1];
|
150
|
|
|
|
151
|
6 |
|
return $result;
|
152
|
|
|
}
|
153
|
|
|
|
154
|
4 |
|
$result .= $this->data->arrTens[$decs / 10 - 1];
|
155
|
|
|
|
156
|
4 |
|
if ($decs % 10 !== 0) {
|
157
|
4 |
|
$result .= $this->data->arrUnits[$decs % 10 - 1];
|
158
|
|
|
}
|
159
|
|
|
|
160
|
4 |
|
return $result;
|
161
|
|
|
}
|
162
|
|
|
|
163
|
6 |
|
private function getExponent(int $chunkPos, int $chunkData): string
|
164
|
|
|
{
|
165
|
6 |
|
if (!$this->currency && $chunkPos === 1) {
|
166
|
5 |
|
return '';
|
167
|
|
|
}
|
168
|
3 |
|
$exponent = $this->data->arrExponents[$chunkPos];
|
169
|
3 |
|
$chunkPos > 3 ? $chunkPos = 3 : true;
|
170
|
3 |
|
$index = $this->getIndex($chunkData % 100);
|
171
|
3 |
|
$suffix = $this->data->arrSuffix[$index][$chunkPos];
|
172
|
|
|
|
173
|
3 |
|
return $exponent . $suffix;
|
174
|
|
|
}
|
175
|
|
|
|
176
|
3 |
|
private function getIndex(int $lastDigits): int
|
177
|
|
|
{
|
178
|
3 |
|
$last = $lastDigits % 10;
|
179
|
|
|
|
180
|
3 |
|
if ($lastDigits >= 11 && $lastDigits <= 14) {
|
181
|
2 |
|
return 2;
|
182
|
|
|
}
|
183
|
|
|
|
184
|
3 |
|
return $this->checkSingleChunk($last);
|
185
|
|
|
}
|
186
|
|
|
|
187
|
3 |
|
public function checkSingleChunk(int $digit): int
|
188
|
|
|
{
|
189
|
3 |
|
if ($digit === 1) {
|
190
|
3 |
|
return 0;
|
191
|
|
|
}
|
192
|
3 |
|
if ($digit >= 2 && $digit <= 4) {
|
193
|
3 |
|
return 1;
|
194
|
|
|
}
|
195
|
|
|
|
196
|
2 |
|
return 2;
|
197
|
|
|
}
|
198
|
|
|
}
|
199
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.