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 bindEntity(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 (!empty($data["retCode"]) && (int)$data["retCode"] > 0) { |
39
|
|
|
throw new ApiException($data["retMsg"], $data["retCode"]); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$time = $data['time']; |
43
|
|
|
$code = $data['retCode']; |
44
|
|
|
$msg = $data['retMsg']; |
45
|
|
|
$extMsg = $data['retExtInfo']; |
46
|
|
|
|
47
|
|
|
if (isset($data['result'][$this->entity::$rootDataKey])) { |
|
|
|
|
48
|
|
|
$data = $data['result'][$this->entity::$rootDataKey]; |
49
|
|
|
} elseif (!empty($data['result'])) { |
50
|
|
|
$data = [$data['result']]; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
switch ($mode) { |
54
|
|
|
case EnumOutputMode::MODE_JSON: |
55
|
|
|
$collection = new StringCollection(); |
56
|
|
|
$collection->push($apiData); |
57
|
|
|
break; |
58
|
|
|
case EnumOutputMode::MODE_ARRAY: |
59
|
|
|
$collection = new ArrayCollection(); |
60
|
|
|
array_walk($data, function ($item) use ($collection) { |
61
|
|
|
$collection->push($item); |
62
|
|
|
}); |
63
|
|
|
break; |
64
|
|
|
case EnumOutputMode::MODE_ENTITY: |
65
|
|
|
default: |
66
|
|
|
$collection = new EntityCollection(); |
67
|
|
|
array_walk($data, function ($item) use ($collection) { |
68
|
|
|
if (!empty($item)) { |
69
|
|
|
$collection->push(ResponseDtoBuilder::make($this->entity, $item)); |
70
|
|
|
} |
71
|
|
|
}); |
72
|
|
|
break; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return (new CurlResponseDto()) |
|
|
|
|
76
|
|
|
->setTime($time) |
77
|
|
|
->setReturnCode($code) |
78
|
|
|
->setReturnMessage($msg) |
79
|
|
|
->setReturnExtendedInfo($extMsg) |
80
|
|
|
->setBody($collection); |
81
|
|
|
} |
82
|
|
|
} |