|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Carpenstar\ByBitAPI\WebSockets\Derivatives\PublicChannels\OrderBook\Entities; |
|
4
|
|
|
|
|
5
|
|
|
use Carpenstar\ByBitAPI\Core\Builders\ResponseDtoBuilder; |
|
6
|
|
|
use Carpenstar\ByBitAPI\Core\Interfaces\ICollectionInterface; |
|
7
|
|
|
use Carpenstar\ByBitAPI\Core\Objects\Collection\EntityCollection; |
|
8
|
|
|
use Carpenstar\ByBitAPI\Core\Objects\AbstractResponse; |
|
9
|
|
|
|
|
10
|
|
|
class OrderBookResponse extends AbstractResponse |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Topic name |
|
14
|
|
|
* @var string $topic |
|
15
|
|
|
*/ |
|
16
|
|
|
private ?string $topic; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Message type. snapshot,delta |
|
20
|
|
|
* @var string $type |
|
21
|
|
|
*/ |
|
22
|
|
|
private ?string $type; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
private string $symbol; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var \DateTime $timestamp |
|
31
|
|
|
*/ |
|
32
|
|
|
private \DateTime $timestamp; |
|
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var ICollectionInterface $bid |
|
36
|
|
|
*/ |
|
37
|
|
|
private ICollectionInterface $bid; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var ICollectionInterface $ask |
|
41
|
|
|
*/ |
|
42
|
|
|
private ICollectionInterface $ask; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Update id, is always in sequence. Occasionally, you'll receive "u"=1, |
|
46
|
|
|
* which is a snapshot data due to the restart of the service. |
|
47
|
|
|
* So please overwrite the locally saved orderbook |
|
48
|
|
|
* @var null|int $updateId |
|
49
|
|
|
*/ |
|
50
|
|
|
private ?int $updateId; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var null|int $crossSequence |
|
54
|
|
|
*/ |
|
55
|
|
|
private ?int $crossSequence; |
|
56
|
|
|
|
|
57
|
|
|
public function __construct(array $data) |
|
58
|
|
|
{ |
|
59
|
|
|
$askCollection = new EntityCollection(); |
|
60
|
|
|
$bidCollection = new EntityCollection(); |
|
61
|
|
|
|
|
62
|
|
|
$this->topic = $data['topic'] ?? null; |
|
63
|
|
|
$this->type = $data['type'] ?? null; |
|
64
|
|
|
|
|
65
|
|
|
if (!empty($data['data'])) { |
|
66
|
|
|
if (!empty($data['data']['a'])) { |
|
67
|
|
|
array_map(function ($askItem) use ($askCollection) { |
|
68
|
|
|
$askCollection->push(ResponseDtoBuilder::make(OrderBookResponseItem::class, $askItem)); |
|
69
|
|
|
}, $data['data']['a']); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
if (!empty($data['data']['b'])) { |
|
73
|
|
|
array_map(function ($bidItem) use ($bidCollection) { |
|
74
|
|
|
$bidCollection->push(ResponseDtoBuilder::make(OrderBookResponseItem::class, $bidItem)); |
|
75
|
|
|
}, $data['data']['b']); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$this->symbol = $data['data']['s']; |
|
79
|
|
|
$this->updateId = $data['data']['u']; |
|
80
|
|
|
$this->crossSequence = $data['data']['seq']; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$this->bid = $bidCollection; |
|
84
|
|
|
$this->ask = $askCollection; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @return string |
|
89
|
|
|
*/ |
|
90
|
|
|
public function getTopic(): ?string |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->topic; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return string |
|
97
|
|
|
*/ |
|
98
|
|
|
public function getType(): ?string |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->type; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return array |
|
105
|
|
|
*/ |
|
106
|
|
|
public function getAsk(): array |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->ask->all(); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @return string |
|
113
|
|
|
*/ |
|
114
|
|
|
public function getSymbol(): string |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->symbol; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @return array |
|
121
|
|
|
*/ |
|
122
|
|
|
public function getBid(): array |
|
123
|
|
|
{ |
|
124
|
|
|
return $this->bid->all(); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @return int |
|
129
|
|
|
*/ |
|
130
|
|
|
public function getUpdateId(): int |
|
131
|
|
|
{ |
|
132
|
|
|
return $this->updateId; |
|
|
|
|
|
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @return int |
|
137
|
|
|
*/ |
|
138
|
|
|
public function getCrossSequence(): int |
|
139
|
|
|
{ |
|
140
|
|
|
return $this->crossSequence; |
|
|
|
|
|
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|