TronAwareTrait::toBigNumber()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace mattvb91\TronTrx\Traits;
6
7
use mattvb91\TronTrx\Support\Base58Check;
8
9
/**
10
 * @codeCoverageIgnore
11
 */
12
trait TronAwareTrait
13
{
14
    /**
15
     * Convert from Hex
16
     *
17
     * @param $string
18
     * @return string
19
     */
20
    public function fromHex($string)
21
    {
22
        if (strlen($string) == 42 && mb_substr($string, 0, 2) === '41') {
23
            return $this->hexString2Address($string);
24
        }
25
26
        return $this->hexString2Utf8($string);
27
    }
28
29
    /**
30
     * Convert to Hex
31
     *
32
     * @param $str
33
     * @return string
34
     */
35
    public function toHex($str)
36
    {
37
        if (mb_strlen($str) == 34 && mb_substr($str, 0, 1) === 'T') {
38
            return $this->address2HexString($str);
39
        };
40
41
        return $this->stringUtf8toHex($str);
42
    }
43
44
    /**
45
     * Check the address before converting to Hex
46
     *
47
     * @param $sHexAddress
48
     * @return string
49
     */
50
    public function address2HexString($sHexAddress)
51
    {
52
        if (strlen($sHexAddress) == 42 && mb_strpos($sHexAddress, '41') == 0) {
53
            return $sHexAddress;
54
        }
55
        return Base58Check::decode($sHexAddress, 0, 3);
56
    }
57
58
    /**
59
     * Check Hex address before converting to Base58
60
     *
61
     * @param $sHexString
62
     * @return string
63
     */
64
    public function hexString2Address($sHexString)
65
    {
66
        if (strlen($sHexString) < 2 || (strlen($sHexString) & 1) != 0) {
67
            return '';
68
        }
69
70
        return Base58Check::encode($sHexString, 0, false);
71
    }
72
73
    /**
74
     * Convert string to hex
75
     *
76
     * @param $sUtf8
77
     * @return string
78
     */
79
    public function stringUtf8toHex($sUtf8)
80
    {
81
        return bin2hex($sUtf8);
82
    }
83
84
    /**
85
     * Convert hex to string
86
     *
87
     * @param $sHexString
88
     * @return string
89
     */
90
    public function hexString2Utf8($sHexString)
91
    {
92
        return hex2bin($sHexString);
93
    }
94
95
    /**
96
     * Convert to great value
97
     *
98
     * @param $str
99
     * @return BigInteger
100
     */
101
    public function toBigNumber($str)
102
    {
103
        return new BigInteger($str);
0 ignored issues
show
Bug introduced by
The type mattvb91\TronTrx\Traits\BigInteger was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
104
    }
105
106
    /**
107
     * Convert trx to float
108
     *
109
     * @param $amount
110
     * @return float
111
     */
112
    public function fromTron($amount): float
113
    {
114
        return (float)bcdiv((string)$amount, (string)1e6, 8);
115
    }
116
117
    /**
118
     * Convert float to trx format
119
     *
120
     * @param $double
121
     * @return int
122
     */
123
    public function toTron($double): int
124
    {
125
        return (int)bcmul((string)$double, (string)1e6, 0);
126
    }
127
}