1
|
|
|
<?php |
2
|
|
|
namespace Carpenstar\ByBitAPI\WebSockets\Channels\Derivatives\PublicChannels\OrderBook\Entities; |
3
|
|
|
|
4
|
|
|
use Carpenstar\ByBitAPI\Core\Builders\ResponseBuilder; |
5
|
|
|
use Carpenstar\ByBitAPI\Core\Interfaces\ICollectionInterface; |
6
|
|
|
use Carpenstar\ByBitAPI\Core\Objects\Collection\EntityCollection; |
7
|
|
|
use Carpenstar\ByBitAPI\Core\Objects\AbstractResponse; |
8
|
|
|
use Carpenstar\ByBitAPI\WebSockets\Channels\Spot\PublicChannels\OrderBook\Entities\OrderBookPriceAbstract; |
|
|
|
|
9
|
|
|
|
10
|
|
|
class OrderBookAbstract extends AbstractResponse |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Topic name |
15
|
|
|
* @var string $topic |
16
|
|
|
*/ |
17
|
|
|
private string $topic; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Message type. snapshot,delta |
21
|
|
|
* @var string $type |
22
|
|
|
*/ |
23
|
|
|
private string $type; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private string $symbol; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var \DateTime $timestamp |
32
|
|
|
*/ |
33
|
|
|
private \DateTime $timestamp; |
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var ICollectionInterface $bid |
37
|
|
|
*/ |
38
|
|
|
private ICollectionInterface $bid; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var ICollectionInterface $ask |
42
|
|
|
*/ |
43
|
|
|
private ICollectionInterface $ask; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Update id, is always in sequence. Occasionally, you'll receive "u"=1, |
47
|
|
|
* which is a snapshot data due to the restart of the service. |
48
|
|
|
* So please overwrite the locally saved orderbook |
49
|
|
|
* @var null|int $updateId |
50
|
|
|
*/ |
51
|
|
|
private ?int $updateId; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var null|int $crossSequence |
55
|
|
|
*/ |
56
|
|
|
private ?int $crossSequence; |
57
|
|
|
|
58
|
|
|
public function __construct(array $data) |
59
|
|
|
{ |
60
|
|
|
$this |
61
|
|
|
->setTopic($data['topic']) |
62
|
|
|
->setType($data['type']) |
63
|
|
|
->setSymbol($data['data']['s']) |
64
|
|
|
->setBid($data['data']['b']) |
65
|
|
|
->setAsk($data['data']['a']) |
66
|
|
|
->setUpdateId($data['data']['u']) |
67
|
|
|
->setCrossSequence($data['data']['seq']); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $topic |
72
|
|
|
* @return self |
73
|
|
|
*/ |
74
|
|
|
private function setTopic(string $topic): self |
75
|
|
|
{ |
76
|
|
|
$this->topic = $topic; |
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
public function getTopic(): string |
84
|
|
|
{ |
85
|
|
|
return $this->topic; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param string $type |
90
|
|
|
* @return self |
91
|
|
|
*/ |
92
|
|
|
private function setType(string $type): self |
93
|
|
|
{ |
94
|
|
|
$this->type = $type; |
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
public function getType(): string |
102
|
|
|
{ |
103
|
|
|
return $this->type; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return ICollectionInterface |
108
|
|
|
*/ |
109
|
|
|
public function getAsk(): ICollectionInterface |
110
|
|
|
{ |
111
|
|
|
return $this->ask; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param string $symbol |
116
|
|
|
* @return self |
117
|
|
|
*/ |
118
|
|
|
private function setSymbol(string $symbol): self |
119
|
|
|
{ |
120
|
|
|
$this->symbol = $symbol; |
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
|
|
public function getSymbol(): string |
128
|
|
|
{ |
129
|
|
|
return $this->symbol; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param array $asks |
134
|
|
|
* @return OrderBookAbstract |
135
|
|
|
* @throws \Exception |
136
|
|
|
*/ |
137
|
|
|
private function setAsk(array $asks): self |
138
|
|
|
{ |
139
|
|
|
$askCollection = new EntityCollection(); |
140
|
|
|
|
141
|
|
|
if (!empty($asks)) { |
142
|
|
|
array_map(function ($askItem) use ($askCollection) { |
143
|
|
|
$askCollection->push(ResponseBuilder::make(OrderBookPriceAbstract::class, $askItem)); |
144
|
|
|
}, $asks); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$this->ask = $askCollection; |
148
|
|
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param array $bids |
153
|
|
|
* @return OrderBookAbstract |
154
|
|
|
* @throws \Exception |
155
|
|
|
*/ |
156
|
|
|
private function setBid(array $bids): self |
157
|
|
|
{ |
158
|
|
|
$bidCollection = new EntityCollection(); |
159
|
|
|
|
160
|
|
|
if (!empty($bids)) { |
161
|
|
|
array_map(function ($bidItem) use ($bidCollection) { |
162
|
|
|
$bidCollection->push(ResponseBuilder::make(OrderBookPriceAbstract::class, $bidItem)); |
163
|
|
|
}, $bids); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
$this->bid = $bidCollection; |
167
|
|
|
return $this; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @return ICollectionInterface |
172
|
|
|
*/ |
173
|
|
|
public function getBid(): ICollectionInterface |
174
|
|
|
{ |
175
|
|
|
return $this->bid; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param int|null $updateId |
180
|
|
|
* @return self |
181
|
|
|
*/ |
182
|
|
|
private function setUpdateId(?int $updateId): self |
183
|
|
|
{ |
184
|
|
|
$this->updateId = $updateId; |
185
|
|
|
return $this; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @return int |
190
|
|
|
*/ |
191
|
|
|
public function getUpdateId(): int |
192
|
|
|
{ |
193
|
|
|
return $this->updateId; |
|
|
|
|
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @param int|null $crossSequence |
198
|
|
|
* @return self |
199
|
|
|
*/ |
200
|
|
|
private function setCrossSequence(?int $crossSequence): self |
201
|
|
|
{ |
202
|
|
|
$this->crossSequence = $crossSequence; |
203
|
|
|
return $this; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @return int |
208
|
|
|
*/ |
209
|
|
|
public function getCrossSequence(): int |
210
|
|
|
{ |
211
|
|
|
return $this->crossSequence; |
|
|
|
|
212
|
|
|
} |
213
|
|
|
} |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: