|
1
|
|
|
<?php |
|
2
|
|
|
namespace Carpenstar\ByBitAPI\Core\Response; |
|
3
|
|
|
|
|
4
|
|
|
use Carpenstar\ByBitAPI\Core\Enums\EnumOutputMode; |
|
5
|
|
|
use Carpenstar\ByBitAPI\Core\Exceptions\ApiException; |
|
6
|
|
|
use Carpenstar\ByBitAPI\Core\Builders\ResponseDtoBuilder; |
|
7
|
|
|
use Carpenstar\ByBitAPI\Core\Interfaces\ICurlResponseDtoInterface; |
|
8
|
|
|
use Carpenstar\ByBitAPI\Core\Interfaces\IResponseHandlerInterface; |
|
9
|
|
|
use Carpenstar\ByBitAPI\Core\Objects\Collection\ArrayCollection; |
|
10
|
|
|
use Carpenstar\ByBitAPI\Core\Objects\Collection\EntityCollection; |
|
11
|
|
|
use Carpenstar\ByBitAPI\Core\Objects\Collection\StringCollection; |
|
12
|
|
|
|
|
13
|
|
|
class CurlResponseHandler implements IResponseHandlerInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var string $entityClassName |
|
17
|
|
|
*/ |
|
18
|
|
|
private string $entity; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @param string $className |
|
22
|
|
|
* @return void |
|
23
|
|
|
*/ |
|
24
|
|
|
public function bindDto(string $className): self |
|
25
|
|
|
{ |
|
26
|
|
|
$this->entity = $className; |
|
27
|
|
|
return $this; |
|
|
|
|
|
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @return IResponseHandlerInterface |
|
32
|
|
|
* @throws \Exception |
|
33
|
|
|
*/ |
|
34
|
|
|
public function handle(string $apiData, int $mode = EnumOutputMode::MODE_ARRAY): ICurlResponseDtoInterface |
|
35
|
|
|
{ |
|
36
|
|
|
$data = json_decode($apiData, true); |
|
37
|
|
|
|
|
38
|
|
|
// If return NULL, then service blocked that ip, try again after 10 minutes |
|
39
|
|
|
if (is_null($data)) { |
|
40
|
|
|
$data = [ |
|
41
|
|
|
'time' => time(), |
|
42
|
|
|
'retCode' => 400, |
|
43
|
|
|
'retMsg' => 'Forbidden, try again later', |
|
44
|
|
|
'result' => ['list' =>[]], |
|
45
|
|
|
'retExtInfo' => [] |
|
46
|
|
|
]; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
if (!empty($data["retCode"]) && (int)$data["retCode"] > 0) { |
|
50
|
|
|
throw new ApiException($data["retMsg"], $data["retCode"]); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$time = $data['time'] ?? 0; |
|
54
|
|
|
$code = $data['retCode'] ?? 0; |
|
55
|
|
|
$msg = $data['retMsg'] ?? ''; |
|
56
|
|
|
$extMsg = $data['retExtInfo'] ?? []; |
|
57
|
|
|
$cursor = $data['result']['nextPageCursor'] ?? ''; |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
if (isset($data['result'][$this->entity::$rootDataKey])) { |
|
|
|
|
|
|
61
|
|
|
$data = $data['result'][$this->entity::$rootDataKey]; |
|
62
|
|
|
} elseif (!empty($data['result'])) { |
|
63
|
|
|
$data = [$data['result']]; |
|
64
|
|
|
} elseif (empty($data['result'])) { |
|
65
|
|
|
$data = []; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
switch ($mode) { |
|
69
|
|
|
case EnumOutputMode::MODE_JSON: |
|
70
|
|
|
$collection = new StringCollection(); |
|
71
|
|
|
$collection->push($apiData); |
|
72
|
|
|
break; |
|
73
|
|
|
case EnumOutputMode::MODE_ARRAY: |
|
74
|
|
|
$collection = new ArrayCollection(); |
|
75
|
|
|
array_walk($data, function ($item) use ($collection) { |
|
76
|
|
|
$collection->push($item); |
|
77
|
|
|
}); |
|
78
|
|
|
break; |
|
79
|
|
|
case EnumOutputMode::MODE_ENTITY: |
|
80
|
|
|
default: |
|
81
|
|
|
$collection = new EntityCollection(); |
|
82
|
|
|
array_walk($data, function ($item) use ($collection) { |
|
83
|
|
|
if (!empty($item)) { |
|
84
|
|
|
$collection->push(ResponseDtoBuilder::make($this->entity, $item)); |
|
85
|
|
|
} |
|
86
|
|
|
}); |
|
87
|
|
|
break; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return (new CurlResponseDto()) |
|
|
|
|
|
|
91
|
|
|
->setTime($time) |
|
92
|
|
|
->setReturnCode($code) |
|
93
|
|
|
->setReturnMessage($msg) |
|
94
|
|
|
->setReturnExtendedInfo($extMsg) |
|
95
|
|
|
->setNextPageCursor($cursor) |
|
96
|
|
|
->setBody($collection); |
|
97
|
|
|
} |
|
98
|
|
|
} |