1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Carpenstar\ByBitAPI\Spot\MarketData\OrderBook\Response; |
4
|
|
|
|
5
|
|
|
use Carpenstar\ByBitAPI\Core\Builders\ResponseDtoBuilder; |
6
|
|
|
use Carpenstar\ByBitAPI\Core\Helpers\DateTimeHelper; |
7
|
|
|
use Carpenstar\ByBitAPI\Core\Objects\AbstractResponse; |
8
|
|
|
use Carpenstar\ByBitAPI\Core\Objects\Collection\EntityCollection; |
9
|
|
|
use Carpenstar\ByBitAPI\Spot\MarketData\OrderBook\Interfaces\IOrderBookResponseInterface; |
10
|
|
|
|
11
|
|
|
class OrderBookResponse extends AbstractResponse implements IOrderBookResponseInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Current time, unit in millisecond |
15
|
|
|
* @var \DateTime $time |
16
|
|
|
*/ |
17
|
|
|
protected \DateTime $time; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Bid price and quantity, with best bid prices ranked from top to bottom |
21
|
|
|
* @var EntityCollection $bids |
22
|
|
|
*/ |
23
|
|
|
protected EntityCollection $bids; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Ask price and quantity, with best ask prices ranked from top to bottom |
27
|
|
|
* @var EntityCollection $asks |
28
|
|
|
*/ |
29
|
|
|
protected EntityCollection $asks; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param array $data |
33
|
|
|
* @throws \Exception |
34
|
|
|
*/ |
35
|
|
|
public function __construct(array $data) |
36
|
|
|
{ |
37
|
|
|
$bids = new EntityCollection(); |
38
|
|
|
$asks = new EntityCollection(); |
39
|
|
|
|
40
|
|
|
$this->setTime($data['time']); |
41
|
|
|
|
42
|
|
|
if (!empty($data['bids'])) { |
43
|
|
|
array_map(function ($bid) use ($bids) { |
44
|
|
|
$bids->push(ResponseDtoBuilder::make(OrderBookPriceItemResponse::class, $bid)); |
45
|
|
|
}, $data['bids']); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if (!empty($data['asks'])) { |
49
|
|
|
array_map(function ($ask) use ($asks) { |
50
|
|
|
$asks->push(ResponseDtoBuilder::make(OrderBookPriceItemResponse::class, $ask)); |
51
|
|
|
}, $data['asks']); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$this->setBids($bids); |
55
|
|
|
$this->setAsks($asks); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $time |
60
|
|
|
* @return OrderBookResponse |
61
|
|
|
* @throws \Exception |
62
|
|
|
*/ |
63
|
|
|
private function setTime(string $time): self |
64
|
|
|
{ |
65
|
|
|
$this->time = DateTimeHelper::makeFromTimestamp($time); |
|
|
|
|
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return \DateTime |
71
|
|
|
*/ |
72
|
|
|
public function getTime(): \DateTime |
73
|
|
|
{ |
74
|
|
|
return $this->time; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param EntityCollection $asks |
79
|
|
|
* @return OrderBookResponse |
80
|
|
|
*/ |
81
|
|
|
private function setAsks(EntityCollection $asks): self |
82
|
|
|
{ |
83
|
|
|
$this->asks = $asks; |
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return EntityCollection |
89
|
|
|
*/ |
90
|
|
|
public function getAsks(): EntityCollection |
91
|
|
|
{ |
92
|
|
|
return $this->asks; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param EntityCollection $bids |
97
|
|
|
* @return OrderBookResponse |
98
|
|
|
*/ |
99
|
|
|
private function setBids(EntityCollection $bids): self |
100
|
|
|
{ |
101
|
|
|
$this->bids = $bids; |
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return EntityCollection |
107
|
|
|
*/ |
108
|
|
|
public function getBids(): EntityCollection |
109
|
|
|
{ |
110
|
|
|
return $this->bids; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|