1
|
|
|
<?php |
2
|
|
|
namespace Carpenstar\ByBitAPI\Spot\MarketData\MergedOrderBook\Response; |
3
|
|
|
|
4
|
|
|
use Carpenstar\ByBitAPI\Core\Builders\ResponseBuilder; |
5
|
|
|
use Carpenstar\ByBitAPI\Core\Helpers\DateTimeHelper; |
6
|
|
|
use Carpenstar\ByBitAPI\Core\Objects\Collection\EntityCollection; |
7
|
|
|
use Carpenstar\ByBitAPI\Core\Objects\ResponseEntity; |
8
|
|
|
use Carpenstar\ByBitAPI\Spot\MarketData\MergedOrderBook\Interfaces\IMergedOrderBookResponse; |
9
|
|
|
use Carpenstar\ByBitAPI\Spot\MarketData\OrderBook\Response\OrderBookPriceItemResponse; |
10
|
|
|
|
11
|
|
|
class MergedOrderBookResponse extends ResponseEntity implements IMergedOrderBookResponse |
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
|
|
|
$this->setTime($data['time']); |
38
|
|
|
|
39
|
|
|
$bids = new EntityCollection(); |
40
|
|
|
if (!empty($data['bids'])) { |
41
|
|
|
array_map(function ($bid) use ($bids) { |
42
|
|
|
$bids->push(ResponseBuilder::make(OrderBookPriceItemResponse::class, $bid)); |
43
|
|
|
}, $data['bids']); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$asks = new EntityCollection(); |
47
|
|
|
if (!empty($data['asks'])) { |
48
|
|
|
array_map(function ($ask) use ($asks) { |
49
|
|
|
$asks->push(ResponseBuilder::make(OrderBookPriceItemResponse::class, $ask)); |
50
|
|
|
}, $data['asks']); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$this->setBids($bids); |
54
|
|
|
$this->setAsks($asks); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $time |
59
|
|
|
* @return MergedOrderBookResponse |
60
|
|
|
* @throws \Exception |
61
|
|
|
*/ |
62
|
|
|
private function setTime(string $time): self |
63
|
|
|
{ |
64
|
|
|
$this->time = DateTimeHelper::makeFromTimestamp($time); |
|
|
|
|
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return \DateTime |
70
|
|
|
*/ |
71
|
|
|
public function getTime(): \DateTime |
72
|
|
|
{ |
73
|
|
|
return $this->time; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param EntityCollection $asks |
78
|
|
|
* @return MergedOrderBookResponse |
79
|
|
|
*/ |
80
|
|
|
private function setAsks(EntityCollection $asks): self |
81
|
|
|
{ |
82
|
|
|
$this->asks = $asks; |
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return EntityCollection |
88
|
|
|
*/ |
89
|
|
|
public function getAsks(): EntityCollection |
90
|
|
|
{ |
91
|
|
|
return $this->asks; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param EntityCollection $bids |
96
|
|
|
* @return MergedOrderBookResponse |
97
|
|
|
*/ |
98
|
|
|
private function setBids(EntityCollection $bids): self |
99
|
|
|
{ |
100
|
|
|
$this->bids = $bids; |
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return EntityCollection |
106
|
|
|
*/ |
107
|
|
|
public function getBids(): EntityCollection |
108
|
|
|
{ |
109
|
|
|
return $this->bids; |
110
|
|
|
} |
111
|
|
|
} |