|
1
|
|
|
<?php |
|
2
|
|
|
namespace Carpenstar\ByBitAPI\WebSockets\Channels\Spot\PublicChannels\OrderBook\Entities; |
|
3
|
|
|
|
|
4
|
|
|
use Carpenstar\ByBitAPI\Core\Builders\ResponseBuilder; |
|
5
|
|
|
use Carpenstar\ByBitAPI\Core\Helpers\DateTimeHelper; |
|
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 OrderBookAbstract extends AbstractResponse |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Topic name |
|
14
|
|
|
* @var string $topic |
|
15
|
|
|
*/ |
|
16
|
|
|
private string $topic; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Data type. snapshot |
|
20
|
|
|
* @var string $type |
|
21
|
|
|
*/ |
|
22
|
|
|
private string $type; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Trading pair |
|
26
|
|
|
* @var string $symbol |
|
27
|
|
|
*/ |
|
28
|
|
|
private string $symbol; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The timestamp that message is sent out |
|
32
|
|
|
* @var \DateTime $requestTimestamp |
|
33
|
|
|
*/ |
|
34
|
|
|
private \DateTime $requestTimestamp; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* The timestamp that system generates the data. |
|
38
|
|
|
* @var \DateTime $responseTimestamp |
|
39
|
|
|
*/ |
|
40
|
|
|
private \DateTime $responseTimestamp; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var ICollectionInterface $ask |
|
44
|
|
|
*/ |
|
45
|
|
|
private ICollectionInterface $ask; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var ICollectionInterface $bid |
|
49
|
|
|
*/ |
|
50
|
|
|
private ICollectionInterface $bid; |
|
51
|
|
|
|
|
52
|
|
|
public function __construct(array $data) |
|
53
|
|
|
{ |
|
54
|
|
|
$this |
|
55
|
|
|
->setSymbol($data['data']['s'] ?? null) |
|
|
|
|
|
|
56
|
|
|
->setTopic($data['topic'] ?? null) |
|
|
|
|
|
|
57
|
|
|
->setType($data['type'] ?? null) |
|
|
|
|
|
|
58
|
|
|
->setBid($data['data']['b'] ?? null) |
|
|
|
|
|
|
59
|
|
|
->setAsk($data['data']['a'] ?? null) |
|
60
|
|
|
->setRequestTimestamp($data['ts'] ?? null) |
|
|
|
|
|
|
61
|
|
|
->setResponseTimestamp($data['data']['t'] ?? null); |
|
|
|
|
|
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param string $topic |
|
66
|
|
|
* @return self |
|
67
|
|
|
*/ |
|
68
|
|
|
private function setTopic(string $topic): self |
|
69
|
|
|
{ |
|
70
|
|
|
$this->topic = $topic; |
|
71
|
|
|
return $this; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @return string |
|
76
|
|
|
*/ |
|
77
|
|
|
public function getTopic(): string |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->topic; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param string $type |
|
84
|
|
|
* @return self |
|
85
|
|
|
*/ |
|
86
|
|
|
private function setType(string $type): self |
|
87
|
|
|
{ |
|
88
|
|
|
$this->type = $type; |
|
89
|
|
|
return $this; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @return string |
|
94
|
|
|
*/ |
|
95
|
|
|
public function getType(): string |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->type; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param string $symbol |
|
102
|
|
|
* @return self |
|
103
|
|
|
*/ |
|
104
|
|
|
private function setSymbol(string $symbol): self |
|
105
|
|
|
{ |
|
106
|
|
|
$this->symbol = $symbol; |
|
107
|
|
|
return $this; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @return string |
|
112
|
|
|
*/ |
|
113
|
|
|
public function getSymbol(): string |
|
114
|
|
|
{ |
|
115
|
|
|
return $this->symbol; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param string $timestamp |
|
120
|
|
|
* @return self |
|
121
|
|
|
*/ |
|
122
|
|
|
private function setRequestTimestamp(string $timestamp): self |
|
123
|
|
|
{ |
|
124
|
|
|
$this->requestTimestamp = DateTimeHelper::makeFromTimestamp($timestamp); |
|
|
|
|
|
|
125
|
|
|
return $this; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @return \DateTime |
|
130
|
|
|
*/ |
|
131
|
|
|
public function getRequestTimestamp(): \DateTime |
|
132
|
|
|
{ |
|
133
|
|
|
return $this->requestTimestamp; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @param string $timestamp |
|
138
|
|
|
* @return self |
|
139
|
|
|
*/ |
|
140
|
|
|
private function setResponseTimestamp(string $timestamp): self |
|
141
|
|
|
{ |
|
142
|
|
|
$this->responseTimestamp = DateTimeHelper::makeFromTimestamp($timestamp); |
|
|
|
|
|
|
143
|
|
|
return $this; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @return \DateTime |
|
148
|
|
|
*/ |
|
149
|
|
|
public function getResponseTimestamp(): \DateTime |
|
150
|
|
|
{ |
|
151
|
|
|
return $this->responseTimestamp; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @param $asks |
|
156
|
|
|
* @return self |
|
157
|
|
|
* @throws \Exception |
|
158
|
|
|
*/ |
|
159
|
|
|
private function setAsk($asks): self |
|
160
|
|
|
{ |
|
161
|
|
|
$askCollection = new EntityCollection(); |
|
162
|
|
|
|
|
163
|
|
|
if (!empty($asks)) { |
|
164
|
|
|
array_map(function ($askItem) use ($askCollection) { |
|
165
|
|
|
$askCollection->push(ResponseBuilder::make(OrderBookPriceDTO::class, $askItem)); |
|
|
|
|
|
|
166
|
|
|
}, $asks); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
$this->ask = $askCollection; |
|
170
|
|
|
return $this; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* @return ICollectionInterface |
|
175
|
|
|
*/ |
|
176
|
|
|
public function getAsk(): ICollectionInterface |
|
177
|
|
|
{ |
|
178
|
|
|
return $this->ask; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* @param array $bids |
|
183
|
|
|
* @return OrderBookAbstract |
|
184
|
|
|
* @throws \Exception |
|
185
|
|
|
*/ |
|
186
|
|
|
private function setBid(array $bids): self |
|
187
|
|
|
{ |
|
188
|
|
|
$bidCollection = new EntityCollection(); |
|
189
|
|
|
|
|
190
|
|
|
if (!empty($bids)) { |
|
191
|
|
|
array_map(function ($bidItem) use ($bidCollection) { |
|
192
|
|
|
$bidCollection->push(ResponseBuilder::make(OrderBookPriceDTO::class, $bidItem)); |
|
193
|
|
|
}, $bids); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
$this->bid = $bidCollection; |
|
197
|
|
|
return $this; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* @return ICollectionInterface |
|
202
|
|
|
*/ |
|
203
|
|
|
public function getBid(): ICollectionInterface |
|
204
|
|
|
{ |
|
205
|
|
|
return $this->bid; |
|
206
|
|
|
} |
|
207
|
|
|
} |