1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sarnado\Converter\Objects; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use Sarnado\Converter\Checkers\TypeValidator; |
7
|
|
|
use Sarnado\Converter\Helpers\ResultNumberFormatter; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class CryptoRateObject |
11
|
|
|
*/ |
12
|
|
|
class CryptoRateObject |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
private $crypto; |
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
private $fiat; |
22
|
|
|
/** |
23
|
|
|
* @var int|float |
24
|
|
|
*/ |
25
|
|
|
private $rate; |
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $time; |
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private $exchange; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return string |
37
|
|
|
*/ |
38
|
21 |
|
public function getCrypto(): string |
39
|
|
|
{ |
40
|
21 |
|
return $this->crypto; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string $crypto |
45
|
|
|
*/ |
46
|
126 |
|
public function setCrypto(string $crypto) |
47
|
|
|
{ |
48
|
126 |
|
$this->crypto = $crypto; |
49
|
126 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
21 |
|
public function getFiat(): string |
55
|
|
|
{ |
56
|
21 |
|
return $this->fiat; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $fiat |
61
|
|
|
*/ |
62
|
126 |
|
public function setFiat(string $fiat) |
63
|
|
|
{ |
64
|
126 |
|
$this->fiat = $fiat; |
65
|
126 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return float|int |
69
|
|
|
*/ |
70
|
6 |
|
public function getRate() |
71
|
|
|
{ |
72
|
6 |
|
return $this->rate; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param float|int $rate |
77
|
|
|
* @throws InvalidArgumentException |
78
|
|
|
*/ |
79
|
126 |
|
public function setRate($rate) |
80
|
|
|
{ |
81
|
126 |
|
TypeValidator::isNum($rate); |
82
|
126 |
|
$this->rate = $rate; |
83
|
126 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
3 |
|
public function getTime(): string |
89
|
|
|
{ |
90
|
3 |
|
return $this->time; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string $time |
95
|
|
|
*/ |
96
|
126 |
|
public function setTime(string $time) |
97
|
|
|
{ |
98
|
126 |
|
$this->time = $time; |
99
|
126 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
9 |
|
public function getExchange(): string |
105
|
|
|
{ |
106
|
9 |
|
return $this->exchange; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $exchange |
111
|
|
|
*/ |
112
|
126 |
|
public function setExchange(string $exchange) |
113
|
|
|
{ |
114
|
126 |
|
$this->exchange = $exchange; |
115
|
126 |
|
} |
116
|
|
|
|
117
|
|
|
|
118
|
126 |
|
public function __construct(string $crypto, string $fiat, $rate, string $time, string $exchange) |
119
|
|
|
{ |
120
|
126 |
|
$this->setCrypto($crypto); |
121
|
126 |
|
$this->setFiat($fiat); |
122
|
126 |
|
$this->setRate($rate); |
123
|
126 |
|
$this->setTime($time); |
124
|
126 |
|
$this->setExchange($exchange); |
125
|
126 |
|
} |
126
|
|
|
|
127
|
42 |
|
public function convertToCrypto($amount): ConverterResultObject |
128
|
|
|
{ |
129
|
42 |
|
TypeValidator::isNum($amount); |
130
|
|
|
|
131
|
18 |
|
$this->checkRateExists(); |
132
|
|
|
|
133
|
18 |
|
$rate = ResultNumberFormatter::format($this->rate); |
134
|
|
|
|
135
|
18 |
|
$amount = ResultNumberFormatter::format($amount); |
136
|
|
|
|
137
|
18 |
|
$res = bcmul($amount, $rate, ResultNumberFormatter::DEFAULT_SCALE); |
138
|
|
|
|
139
|
18 |
|
return new ConverterResultObject(ResultNumberFormatter::formatCrypto($res), $this->crypto); |
140
|
|
|
} |
141
|
|
|
|
142
|
42 |
|
public function convertToFiat($amount): ConverterResultObject |
143
|
|
|
{ |
144
|
42 |
|
TypeValidator::isNum($amount); |
145
|
|
|
|
146
|
18 |
|
$this->checkRateExists(); |
147
|
|
|
|
148
|
18 |
|
$rate = ResultNumberFormatter::format($this->rate); |
149
|
|
|
|
150
|
18 |
|
$amount = ResultNumberFormatter::format($amount); |
151
|
|
|
|
152
|
18 |
|
$res = bcdiv($amount, $rate, ResultNumberFormatter::DEFAULT_SCALE); |
153
|
|
|
|
154
|
18 |
|
return new ConverterResultObject(ResultNumberFormatter::formatFiat($res), $this->fiat); |
155
|
|
|
} |
156
|
|
|
|
157
|
36 |
|
private function checkRateExists(): bool |
158
|
|
|
{ |
159
|
36 |
|
return true; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|