1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Sarnado\Converter; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Sarnado\Converter\API\APIClient; |
8
|
|
|
use Sarnado\Converter\Builders\CryptoCurrenciesCollectionBuilder; |
9
|
|
|
use Sarnado\Converter\Builders\CryptoExchangesCollectionBuilder; |
10
|
|
|
use Sarnado\Converter\Builders\CryptoRateObjectBuilder; |
11
|
|
|
use Sarnado\Converter\Builders\CryptoRatesCollectionBuilder; |
12
|
|
|
use Sarnado\Converter\Builders\FiatCurrenciesCollectionBuilder; |
13
|
|
|
use Sarnado\Converter\Builders\FiatRateObjectBuilder; |
14
|
|
|
use Sarnado\Converter\Builders\FiatRatesCollectionBuilder; |
15
|
|
|
use Sarnado\Converter\Collections\CryptoCurrenciesCollection; |
16
|
|
|
use Sarnado\Converter\Collections\CryptoExchangesCollection; |
17
|
|
|
use Sarnado\Converter\Collections\FiatCurrenciesCollection; |
18
|
|
|
use Sarnado\Converter\Objects\APIUsageObject; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class SarnadoConverter |
22
|
|
|
* @package Sarnado\Converter |
23
|
|
|
*/ |
24
|
|
|
class SarnadoConverter |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var APIClient |
28
|
|
|
*/ |
29
|
|
|
private $apiClient; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* SarnadoConverter constructor. |
33
|
|
|
* @param string $apiKey |
34
|
|
|
*/ |
35
|
45 |
|
public function __construct(string $apiKey) |
36
|
|
|
{ |
37
|
45 |
|
$this->apiClient = new APIClient($apiKey); |
38
|
45 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return APIUsageObject |
42
|
|
|
* @throws Exceptions\BadAPIResponseException |
43
|
|
|
* @throws Exceptions\BadRequestException |
44
|
|
|
* @throws Exceptions\ConverterServerErrorException |
45
|
|
|
* @throws Exceptions\DefaultAPIException |
46
|
|
|
* @throws Exceptions\ReachedRateLimitException |
47
|
|
|
* @throws Exceptions\UnauthorizedException |
48
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
49
|
|
|
*/ |
50
|
6 |
|
public function getApiUsage(): APIUsageObject |
51
|
|
|
{ |
52
|
6 |
|
$result = $this->apiClient->getAPIUsage()->getResult(); |
53
|
3 |
|
return new APIUsageObject($result['current'], $result['available'], $result['refresh_after']); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return CryptoExchangesCollection |
58
|
|
|
* @throws Exceptions\BadAPIResponseException |
59
|
|
|
* @throws Exceptions\BadRequestException |
60
|
|
|
* @throws Exceptions\ConverterServerErrorException |
61
|
|
|
* @throws Exceptions\DefaultAPIException |
62
|
|
|
* @throws Exceptions\ReachedRateLimitException |
63
|
|
|
* @throws Exceptions\UnauthorizedException |
64
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
65
|
|
|
*/ |
66
|
6 |
|
public function getCryptoExchanges(): CryptoExchangesCollection |
67
|
|
|
{ |
68
|
6 |
|
$result = $this->apiClient->getCryptoExchanges()->getResult(); |
69
|
3 |
|
return CryptoExchangesCollectionBuilder::build($result); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param null $crypto |
|
|
|
|
74
|
|
|
* @param null $fiat |
|
|
|
|
75
|
|
|
* @param null $exchange |
|
|
|
|
76
|
|
|
* @return Collections\CryptoRatesCollection|Objects\CryptoRateObject |
77
|
|
|
* @throws Exceptions\BadAPIResponseException |
78
|
|
|
* @throws Exceptions\BadRequestException |
79
|
|
|
* @throws Exceptions\ConverterServerErrorException |
80
|
|
|
* @throws Exceptions\DefaultAPIException |
81
|
|
|
* @throws Exceptions\ReachedRateLimitException |
82
|
|
|
* @throws Exceptions\UnauthorizedException |
83
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
84
|
|
|
*/ |
85
|
12 |
|
public function getCryptoRates($crypto = null, $fiat = null, $exchange = null) |
86
|
|
|
{ |
87
|
12 |
|
$result = $this->apiClient->getCryptoRates($crypto, $fiat, $exchange)->getResult(); |
88
|
9 |
|
if (!is_null($crypto) && !is_null($fiat)) |
|
|
|
|
89
|
|
|
{ |
90
|
6 |
|
return CryptoRateObjectBuilder::build($result); |
91
|
|
|
} |
92
|
3 |
|
return CryptoRatesCollectionBuilder::build($result); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param null $from |
|
|
|
|
97
|
|
|
* @param null $to |
|
|
|
|
98
|
|
|
* @return Collections\FiatRatesCollection|Objects\FiatRateObject |
99
|
|
|
* @throws Exceptions\BadAPIResponseException |
100
|
|
|
* @throws Exceptions\BadRequestException |
101
|
|
|
* @throws Exceptions\ConverterServerErrorException |
102
|
|
|
* @throws Exceptions\DefaultAPIException |
103
|
|
|
* @throws Exceptions\ReachedRateLimitException |
104
|
|
|
* @throws Exceptions\UnauthorizedException |
105
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
106
|
|
|
*/ |
107
|
9 |
|
public function getFiatRates($from = null, $to = null) |
108
|
|
|
{ |
109
|
9 |
|
$result = $this->apiClient->getFiatRates($from, $to)->getResult(); |
110
|
6 |
|
if (!is_null($from) && !is_null($to)) |
|
|
|
|
111
|
|
|
{ |
112
|
3 |
|
return FiatRateObjectBuilder::build($result); |
113
|
|
|
} |
114
|
3 |
|
return FiatRatesCollectionBuilder::build($result); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return CryptoCurrenciesCollection |
119
|
|
|
* @throws Exceptions\BadAPIResponseException |
120
|
|
|
* @throws Exceptions\BadRequestException |
121
|
|
|
* @throws Exceptions\ConverterServerErrorException |
122
|
|
|
* @throws Exceptions\DefaultAPIException |
123
|
|
|
* @throws Exceptions\ReachedRateLimitException |
124
|
|
|
* @throws Exceptions\UnauthorizedException |
125
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
126
|
|
|
*/ |
127
|
6 |
|
public function getCryptoCurrencies(): CryptoCurrenciesCollection |
128
|
|
|
{ |
129
|
6 |
|
$result = $this->apiClient->getCryptoCurrencies()->getResult(); |
130
|
3 |
|
return CryptoCurrenciesCollectionBuilder::build($result); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return FiatCurrenciesCollection |
136
|
|
|
* @throws Exceptions\BadAPIResponseException |
137
|
|
|
* @throws Exceptions\BadRequestException |
138
|
|
|
* @throws Exceptions\ConverterServerErrorException |
139
|
|
|
* @throws Exceptions\DefaultAPIException |
140
|
|
|
* @throws Exceptions\ReachedRateLimitException |
141
|
|
|
* @throws Exceptions\UnauthorizedException |
142
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
143
|
|
|
*/ |
144
|
6 |
|
public function getFiatCurrencies(): FiatCurrenciesCollection |
145
|
|
|
{ |
146
|
6 |
|
$result = $this->apiClient->getFiatCurrencies()->getResult(); |
147
|
3 |
|
return FiatCurrenciesCollectionBuilder::build($result); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|