Skip to content

Commit c3b6cbe

Browse files
authored
Merge pull request #5 from backendrulz/master
Format to PSR-12, fix for PHP 8.1 and removes redundant code
2 parents 978ea17 + 5792ecc commit c3b6cbe

File tree

1 file changed

+89
-62
lines changed

1 file changed

+89
-62
lines changed

CryptAPI/CryptAPI.php

Lines changed: 89 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,59 @@
11
<?php
22

33
namespace CryptAPI;
4+
45
use Exception;
56

6-
class CryptAPI {
7-
private static $base_url = "https://api.cryptapi.io";
8-
private static $pro_url = "https://pro-api.cryptapi.io";
9-
private $valid_coins = [];
10-
private $own_address = null;
7+
class CryptAPI
8+
{
9+
private static $base_url = 'https://api.cryptapi.io';
10+
private static $pro_url = 'https://pro-api.cryptapi.io';
11+
private $valid_coins = [];
12+
private $own_address = null;
1113
private $payment_address = null;
12-
private $callback_url = null;
13-
private $coin = null;
14-
private $ca_params = [];
15-
private $parameters = [];
16-
private $api_key = null;
14+
private $callback_url = null;
15+
private $coin = null;
16+
private $ca_params = [];
17+
private $parameters = [];
18+
private $api_key = null;
1719

1820
public static $COIN_MULTIPLIERS = [
19-
'btc' => 10**8,
20-
'bch' => 10**8,
21-
'ltc' => 10**8,
22-
'eth' => 10**18,
23-
'iota' => 10**6,
24-
'xmr' => 10**12,
25-
'trx' => 10**6,
21+
'btc' => 10 ** 8,
22+
'bch' => 10 ** 8,
23+
'ltc' => 10 ** 8,
24+
'eth' => 10 ** 18,
25+
'iota' => 10 ** 6,
26+
'xmr' => 10 ** 12,
27+
'trx' => 10 ** 6,
2628
];
2729

28-
public function __construct($coin, $own_address, $callback_url, $parameters=[], $ca_params=[], $api_key=null) {
30+
public function __construct($coin, $own_address, $callback_url, $parameters = [], $ca_params = [], $api_key = null)
31+
{
2932
$this->valid_coins = CryptAPI::get_supported_coins();
3033

3134
if (!in_array($coin, $this->valid_coins)) {
3235
$vc = print_r($this->valid_coins, true);
3336
throw new Exception("Unsupported Coin: {$coin}, Valid options are: {$vc}");
3437
}
3538

36-
$this->own_address = $own_address;
39+
$this->own_address = $own_address;
3740
$this->callback_url = $callback_url;
38-
$this->coin = $coin;
39-
$this->ca_params = $ca_params;
40-
$this->parameters = $parameters;
41-
$this->api_key = $api_key;
41+
$this->coin = $coin;
42+
$this->ca_params = $ca_params;
43+
$this->parameters = $parameters;
44+
$this->api_key = $api_key;
4245
}
4346

44-
public static function get_supported_coins() {
47+
public static function get_supported_coins()
48+
{
4549
$info = CryptAPI::get_info(null, true);
4650

4751
if (empty($info)) {
4852
return null;
4953
}
5054

5155
unset($info['fee_tiers']);
52-
56+
5357
$coins = [];
5458

5559
foreach ($info as $chain => $data) {
@@ -68,27 +72,30 @@ public static function get_supported_coins() {
6872
return $coins;
6973
}
7074

71-
public function get_address() {
72-
if (empty($this->own_address) || empty($this->coin) || empty($this->callback_url)) return null;
75+
public function get_address()
76+
{
77+
if (empty($this->own_address) || empty($this->coin) || empty($this->callback_url)) {
78+
return null;
79+
}
7380

7481
$api_key = $this->api_key;
7582

7683
$callback_url = $this->callback_url;
7784
if (!empty($this->parameters)) {
7885
$req_parameters = http_build_query($this->parameters);
79-
$callback_url = "{$this->callback_url}?{$req_parameters}";
86+
$callback_url = "{$this->callback_url}?{$req_parameters}";
8087
}
8188

8289
if (empty($api_key)) {
8390
$ca_params = array_merge([
8491
'callback' => $callback_url,
85-
'address' => $this->own_address,
92+
'address' => $this->own_address,
8693
], $this->ca_params);
8794
} else {
8895
$ca_params = array_merge([
89-
'apikey' => $api_key,
96+
'apikey' => $api_key,
9097
'callback' => $callback_url,
91-
'address' => $this->own_address,
98+
'address' => $this->own_address,
9299
], $this->ca_params);
93100
}
94101

@@ -103,13 +110,16 @@ public function get_address() {
103110
return null;
104111
}
105112

106-
public function check_logs() {
107-
if (empty($this->coin) || empty($this->callback_url)) return null;
113+
public function check_logs()
114+
{
115+
if (empty($this->coin) || empty($this->callback_url)) {
116+
return null;
117+
}
108118

109119
$callback_url = $this->callback_url;
110120
if (!empty($this->parameters)) {
111121
$req_parameters = http_build_query($this->parameters);
112-
$callback_url = "{$this->callback_url}?{$req_parameters}";
122+
$callback_url = "{$this->callback_url}?{$req_parameters}";
113123
}
114124

115125
$params = [
@@ -125,15 +135,22 @@ public function check_logs() {
125135
return null;
126136
}
127137

128-
public function get_qrcode($value = false, $size = false) {
129-
if (empty($this->coin)) return null;
138+
public function get_qrcode($value = false, $size = false)
139+
{
140+
if (empty($this->coin)) {
141+
return null;
142+
}
130143

131144
$params = [
132145
'address' => $this->payment_address,
133146
];
134147

135-
if ($value) $params['value'] = $value;
136-
if ($size) $params['size'] = $size;
148+
if ($value) {
149+
$params['value'] = $value;
150+
}
151+
if ($size) {
152+
$params['size'] = $size;
153+
}
137154

138155
$response = CryptAPI::_request($this->coin, 'qrcode', $params);
139156

@@ -144,7 +161,8 @@ public function get_qrcode($value = false, $size = false) {
144161
return null;
145162
}
146163

147-
public static function get_info($coin = null, $assoc = false) {
164+
public static function get_info($coin = null, $assoc = false)
165+
{
148166
$params = [];
149167

150168
if (empty($coin)) {
@@ -160,10 +178,11 @@ public static function get_info($coin = null, $assoc = false) {
160178
return null;
161179
}
162180

163-
public static function get_estimate($coin, $addresses = 1, $priority = 'default') {
181+
public static function get_estimate($coin, $addresses = 1, $priority = 'default')
182+
{
164183
$response = CryptAPI::_request($coin, 'estimate', [
165184
'addresses' => $addresses,
166-
'priority' => $priority
185+
'priority' => $priority
167186
]);
168187

169188
if ($response->status == 'success') {
@@ -172,11 +191,12 @@ public static function get_estimate($coin, $addresses = 1, $priority = 'default'
172191

173192
return null;
174193
}
175-
176-
public static function get_convert($coin, $value, $from) {
194+
195+
public static function get_convert($coin, $value, $from)
196+
{
177197
$response = CryptAPI::_request($coin, 'convert', [
178198
'value' => $value,
179-
'from' => $from
199+
'from' => $from
180200
]);
181201

182202
if ($response->status == 'success') {
@@ -186,51 +206,58 @@ public static function get_convert($coin, $value, $from) {
186206
return null;
187207
}
188208

189-
public static function process_callback($_get) {
209+
public static function process_callback($_get)
210+
{
190211
$params = [
191-
'address_in' => $_get['address_in'],
192-
'address_out' => $_get['address_out'],
193-
'txid_in' => $_get['txid_in'],
194-
'txid_out' => isset($_get['txid_out']) ? $_get['txid_out'] : null,
195-
'confirmations' => $_get['confirmations'],
196-
'value' => $_get['value'],
197-
'value_coin' => $_get['value_coin'],
198-
'value_forwarded' => isset($_get['value_forwarded']) ? $_get['value_forwarded'] : null,
212+
'address_in' => $_get['address_in'],
213+
'address_out' => $_get['address_out'],
214+
'txid_in' => $_get['txid_in'],
215+
'txid_out' => isset($_get['txid_out']) ? $_get['txid_out'] : null,
216+
'confirmations' => $_get['confirmations'],
217+
'value' => $_get['value'],
218+
'value_coin' => $_get['value_coin'],
219+
'value_forwarded' => isset($_get['value_forwarded']) ? $_get['value_forwarded'] : null,
199220
'value_forwarded_coin' => isset($_get['value_forwarded_coin']) ? $_get['value_forwarded_coin'] : null,
200-
'coin' => $_get['coin'],
201-
'pending' => isset($_get['pending']) ? $_get['pending'] : false,
221+
'coin' => $_get['coin'],
222+
'pending' => isset($_get['pending']) ? $_get['pending'] : false,
202223
];
203224

204225
foreach ($_get as $k => $v) {
205-
if (isset($params[$k])) continue;
226+
if (isset($params[$k])) {
227+
continue;
228+
}
206229
$params[$k] = $_get[$k];
207230
}
208231

209232
return $params;
210233
}
211234

212-
private static function _request($coin, $endpoint, $params = [], $assoc = false) {
235+
private static function _request($coin, $endpoint, $params = [], $assoc = false)
236+
{
213237
$base_url = Cryptapi::$base_url;
214-
$coin = str_replace('_', '/', $coin);
238+
$coin = str_replace('_', '/', (string) $coin);
215239

216240
if (!empty($params['apikey']) && $endpoint !== 'info') {
217241
$base_url = CryptAPI::$pro_url;
218242
}
219243

220-
if (!empty($params)) $data = http_build_query($params);
244+
if (!empty($params)) {
245+
$data = http_build_query($params);
246+
}
221247

222248
if (!empty($coin)) {
223-
$coin = str_replace('_', '/', $coin);
224249
$url = "{$base_url}/{$coin}/{$endpoint}/";
225250
} else {
226251
$url = "{$base_url}/{$endpoint}/";
227252
}
228253

229-
if (!empty($data)) $url .= "?{$data}";
254+
if (!empty($data)) {
255+
$url .= "?{$data}";
256+
}
230257

231258
$ch = curl_init();
232259
curl_setopt($ch, CURLOPT_URL, $url);
233-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
260+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
234261
$response = curl_exec($ch);
235262
curl_close($ch);
236263

0 commit comments

Comments
 (0)