Currency::getPrecision()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
ccs 0
cts 0
cp 0
crap 6
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace B2Binpay;
5
6
use B2Binpay\Exception\UnknownValueException;
7
8
/**
9
 * Currency directory
10
 *
11
 * @package B2Binpay
12
 */
13
class Currency
14
{
15
    const MAX_PRECISION = 18;
16
17
    /**
18
     * @var array[]
19
     */
20
    private static $list = [
21
        156 => [
22
            'iso' => 156,
23
            'alpha' => 'CNY',
24
            'name' => 'Chinese yuan',
25
            'precision' => 2
26
        ],
27
        344 => [
28
            'iso' => 344,
29
            'alpha' => 'HKD',
30
            'name' => 'Hong Kong dollar',
31
            'precision' => 2
32
        ],
33
        392 => [
34
            'iso' => 392,
35
            'alpha' => 'JPY',
36
            'name' => 'Japanese yen',
37
            'precision' => 2
38
        ],
39
        398 => [
40
            'iso' => 398,
41
            'alpha' => 'KZT',
42
            'name' => 'Tenge',
43
            'precision' => 2
44
        ],
45
        643 => [
46
            'iso' => 643,
47
            'alpha' => 'RUB',
48
            'name' => 'Russian ruble',
49
            'precision' => 2
50
        ],
51
        826 => [
52
            'iso' => 826,
53
            'alpha' => 'GBP',
54
            'name' => 'Pound Sterling',
55
            'precision' => 2
56
        ],
57
        840 => [
58
            'iso' => 840,
59
            'alpha' => 'USD',
60
            'name' => 'US Dollar',
61
            'precision' => 2
62
        ],
63
        933 => [
64
            'iso' => 933,
65
            'alpha' => 'BYN',
66
            'name' => 'Belarusian Ruble',
67
            'precision' => 2
68
        ],
69
        978 => [
70
            'iso' => 978,
71
            'alpha' => 'EUR',
72
            'name' => 'Euro',
73
            'precision' => 2
74
        ],
75
        980 => [
76
            'iso' => 980,
77
            'alpha' => 'UAH',
78
            'name' => 'Ukrainian hryvnia',
79
            'precision' => 2
80
        ],
81
        1000 => [
82
            'iso' => 1000,
83
            'alpha' => 'BTC',
84
            'name' => 'Bitcoin',
85
            'precision' => 8
86
        ],
87
        1002 => [
88
            'iso' => 1002,
89
            'alpha' => 'ETH',
90
            'name' => 'Ethereum',
91
            'precision' => 18
92
        ],
93
        1003 => [
94
            'iso' => 1003,
95
            'alpha' => 'LTC',
96
            'name' => 'Litecoin',
97
            'precision' => 8
98
        ],
99
        1005 => [
100
            'iso' => 1005,
101
            'alpha' => 'DASH',
102
            'name' => 'DASH',
103
            'precision' => 8
104 3
        ],
105
        1006 => [
106 3
            'iso' => 1006,
107 1
            'alpha' => 'BCH',
108
            'name' => 'Bitcoin Cash',
109 2
            'precision' => 8
110
        ],
111
        1007 => [
112
            'iso' => 1007,
113
            'alpha' => 'XMR',
114
            'name' => 'Monero',
115
            'precision' => 12
116
        ],
117 15
        1010 => [
118
            'iso' => 1010,
119 15
            'alpha' => 'XRP',
120
            'name' => 'Ripple',
121 15
            'precision' => 6
122 15
        ],
123 15
        1012 => [
124 15
            'iso' => 1012,
125 14
            'alpha' => 'XEM',
126
            'name' => 'NEM',
127 15
            'precision' => 6
128 15
        ],
129 15
        1018 => [
130
            'iso' => 1018,
131
            'alpha' => 'ADA',
132 15
            'name' => 'Cardano',
133 1
            'precision' => 6
134
        ],
135
        1019 => [
136 14
            'iso' => 1019,
137
            'alpha' => 'DOGE',
138
            'name' => 'Dogecoin',
139
            'precision' => 8
140
        ],
141
        1020 => [
142
            'iso' => 1020,
143
            'alpha' => 'ZEC',
144 14
            'name' => 'Zcash',
145
            'precision' => 8
146 14
        ],
147 1
        1021 => [
148
            'iso' => 1021,
149
            'alpha' => 'XLM',
150 13
            'name' => 'Stellar',
151
            'precision' => 7
152
        ],
153
        1022 => [
154
            'iso' => 1022,
155
            'alpha' => 'EOS',
156 13
            'name' => 'EOS',
157
            'precision' => 4
158 13
        ],
159
        1026 => [
160
            'iso' => 1026,
161
            'alpha' => 'TRX',
162
            'name' => 'TRON',
163
            'precision' => 6
164
        ],
165
        2005 => [
166 2
            'iso' => 2005,
167
            'alpha' => 'USDT',
168 2
            'name' => 'Tether USD',
169 1
            'precision' => 8,
170
            'node' => [
171
                'usdt-omni',
172 1
                'usdt-eth'
173
            ]
174
        ],
175
        2006 => [
176
            'iso' => 2006,
177
            'alpha' => 'EURT',
178
            'name' => 'Tether EUR',
179
            'precision' => 8,
180
            'node' => [
181
                'eurt-omni',
182
                'eurt-eth'
183
            ]
184
        ],
185
        2014 => [
186
            'iso' => 2014,
187
            'alpha' => 'NEO',
188
            'name' => 'NEO',
189
            'precision' => 3
190
        ],
191
        2021 => [
192
            'iso' => 2021,
193
            'alpha' => 'PAX',
194
            'name' => 'PAX',
195
            'precision' => 18,
196
            'node' => [
197
                'pax-eth'
198
            ]
199
        ],
200
        2022 => [
201
            'iso' => 2022,
202
            'alpha' => 'TUSD',
203
            'name' => 'TrueUSD',
204
            'precision' => 18,
205
            'node' => [
206
                'tusd-eth'
207
            ]
208
        ],
209
        2023 => [
210
            'iso' => 2023,
211
            'alpha' => 'GUSD',
212
            'name' => 'Gemini dollar',
213
            'precision' => 2,
214
            'node' => [
215
                'gusd-eth'
216
            ]
217
        ],
218
        2024 => [
219
            'iso' => 2024,
220
            'alpha' => 'USDC',
221
            'name' => 'USD//Coin',
222
            'precision' => 6,
223
            'node' => [
224
                'usdc-eth'
225
            ]
226
        ],
227
        2025 => [
228
            'iso' => 2025,
229
            'alpha' => 'BNB',
230
            'name' => 'Binance Coin',
231
            'precision' => 8
232
        ],
233
        2068 => [
234
            'iso' => 2068,
235
            'alpha' => 'DAI',
236
            'name' => 'Dai Stablecoin',
237
            'precision' => 18,
238
            'node' => [
239
                'dai-eth'
240
            ]
241
        ],
242
        2077 => [
243
            'iso' => 2077,
244
            'alpha' => 'BUSD',
245
            'name' => 'Binance USD',
246
            'precision' => 18,
247
            'node' => [
248
                'busd-eth'
249
            ]
250
        ]
251
    ];
252
253
    /**
254
     * @param int $iso
255
     * @return string
256
     * @throws UnknownValueException
257
     */
258
    public function getAlpha(int $iso): string
259
    {
260
        if (!array_key_exists($iso, Currency::$list)) {
261
            throw new UnknownValueException($iso);
262
        }
263
        return Currency::$list[$iso]['alpha'];
264
    }
265
266
    /**
267
     * @param string $alpha
268
     * @return int
269
     * @throws UnknownValueException
270
     */
271
    public function getIso(string $alpha): int
272
    {
273
        $alpha = strtoupper($alpha);
274
275
        $iso = array_reduce(
276
            self::$list,
277
            function ($carry, $item) use ($alpha) {
278
               $nodeList = isset($item['node']) ? $item['node'] : [];
279
               $nodeList = array_map('strtoupper', $nodeList);
280
281
                if ($item['alpha'] === $alpha || in_array($alpha, $nodeList)) {
282
                    $carry = $item['iso'];
283
                }
284
                return (int)$carry;
285
            }
286
        );
287
288
        if (empty($iso)) {
289
            throw new UnknownValueException($alpha);
290
        }
291
292
        return $iso;
293
    }
294
295
    /**
296
     * @param int $iso
297
     * @return int
298
     * @throws UnknownValueException
299
     */
300
    public function getPrecision(int $iso): int
301
    {
302
        if (!array_key_exists($iso, self::$list)) {
303
            throw new UnknownValueException($iso);
304
        }
305
306
        return self::$list[$iso]['precision'];
307
    }
308
309
    /**
310
     * @return int
311
     */
312
    public function getMaxPrecision(): int
313
    {
314
        return self::MAX_PRECISION;
315
    }
316
317
    /**
318
     * @param int $iso
319
     * @return string
320
     * @throws UnknownValueException
321
     */
322
    public function getName(int $iso): string
323
    {
324
        if (!array_key_exists($iso, self::$list)) {
325
            throw new UnknownValueException($iso);
326
        }
327
328
        return self::$list[$iso]['name'];
329
    }
330
}
331