1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BitWasp\Buffertools; |
6
|
|
|
|
7
|
|
|
use BitWasp\Buffertools\Types\ByteString; |
8
|
|
|
use BitWasp\Buffertools\Types\Int128; |
9
|
|
|
use BitWasp\Buffertools\Types\Int16; |
10
|
|
|
use BitWasp\Buffertools\Types\Int256; |
11
|
|
|
use BitWasp\Buffertools\Types\Int32; |
12
|
|
|
use BitWasp\Buffertools\Types\Int64; |
13
|
|
|
use BitWasp\Buffertools\Types\Int8; |
14
|
|
|
use BitWasp\Buffertools\Types\Uint8; |
15
|
|
|
use BitWasp\Buffertools\Types\Uint16; |
16
|
|
|
use BitWasp\Buffertools\Types\Uint32; |
17
|
|
|
use BitWasp\Buffertools\Types\Uint64; |
18
|
|
|
use BitWasp\Buffertools\Types\Uint128; |
19
|
|
|
use BitWasp\Buffertools\Types\Uint256; |
20
|
|
|
use BitWasp\Buffertools\Types\VarInt; |
21
|
|
|
use BitWasp\Buffertools\Types\VarString; |
22
|
|
|
use BitWasp\Buffertools\Types\Vector; |
23
|
|
|
|
24
|
|
|
class TypeFactory implements TypeFactoryInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Add a Uint8 serializer to the template |
28
|
|
|
* |
29
|
|
|
* @return Uint8 |
30
|
|
|
*/ |
31
|
6 |
|
public function uint8(): Uint8 |
32
|
|
|
{ |
33
|
6 |
|
return new Uint8(ByteOrder::BE); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Add a little-endian Uint8 serializer to the template |
38
|
|
|
* |
39
|
|
|
* @return Uint8 |
40
|
|
|
*/ |
41
|
6 |
|
public function uint8le(): Uint8 |
42
|
|
|
{ |
43
|
6 |
|
return new Uint8(ByteOrder::LE); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Add a Uint16 serializer to the template |
48
|
|
|
* |
49
|
|
|
* @return Uint16 |
50
|
|
|
*/ |
51
|
6 |
|
public function uint16(): Uint16 |
52
|
|
|
{ |
53
|
6 |
|
return new Uint16(ByteOrder::BE); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Add a little-endian Uint16 serializer to the template |
58
|
|
|
* |
59
|
|
|
* @return Uint16 |
60
|
|
|
*/ |
61
|
6 |
|
public function uint16le(): Uint16 |
62
|
|
|
{ |
63
|
6 |
|
return new Uint16(ByteOrder::LE); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Add a Uint32 serializer to the template |
68
|
|
|
* |
69
|
|
|
* @return Uint32 |
70
|
|
|
*/ |
71
|
6 |
|
public function uint32(): Uint32 |
72
|
|
|
{ |
73
|
6 |
|
return new Uint32(ByteOrder::BE); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Add a little-endian Uint32 serializer to the template |
78
|
|
|
* |
79
|
|
|
* @return Uint32 |
80
|
|
|
*/ |
81
|
6 |
|
public function uint32le(): Uint32 |
82
|
|
|
{ |
83
|
6 |
|
return new Uint32(ByteOrder::LE); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Add a Uint64 serializer to the template |
88
|
|
|
* |
89
|
|
|
* @return Uint64 |
90
|
|
|
*/ |
91
|
6 |
|
public function uint64(): Uint64 |
92
|
|
|
{ |
93
|
6 |
|
return new Uint64(ByteOrder::BE); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Add a little-endian Uint64 serializer to the template |
98
|
|
|
* |
99
|
|
|
* @return Uint64 |
100
|
|
|
*/ |
101
|
6 |
|
public function uint64le(): Uint64 |
102
|
|
|
{ |
103
|
6 |
|
return new Uint64(ByteOrder::LE); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Add a Uint128 serializer to the template |
108
|
|
|
* |
109
|
|
|
* @return Uint128 |
110
|
|
|
*/ |
111
|
6 |
|
public function uint128(): Uint128 |
112
|
|
|
{ |
113
|
6 |
|
return new Uint128(ByteOrder::BE); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Add a little-endian Uint128 serializer to the template |
118
|
|
|
* |
119
|
|
|
* @return Uint128 |
120
|
|
|
*/ |
121
|
6 |
|
public function uint128le(): Uint128 |
122
|
|
|
{ |
123
|
6 |
|
return new Uint128(ByteOrder::LE); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Add a Uint256 serializer to the template |
128
|
|
|
* |
129
|
|
|
* @return Uint256 |
130
|
|
|
*/ |
131
|
6 |
|
public function uint256(): Uint256 |
132
|
|
|
{ |
133
|
6 |
|
return new Uint256(ByteOrder::BE); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Add a little-endian Uint256 serializer to the template |
138
|
|
|
* |
139
|
|
|
* @return Uint256 |
140
|
|
|
*/ |
141
|
6 |
|
public function uint256le(): Uint256 |
142
|
|
|
{ |
143
|
6 |
|
return new Uint256(ByteOrder::LE); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Add a int8 serializer to the template |
148
|
|
|
* |
149
|
|
|
* @return Int8 |
150
|
|
|
*/ |
151
|
6 |
|
public function int8(): Int8 |
152
|
|
|
{ |
153
|
6 |
|
return new Int8(ByteOrder::BE); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Add a little-endian Int8 serializer to the template |
158
|
|
|
* |
159
|
|
|
* @return Int8 |
160
|
|
|
*/ |
161
|
6 |
|
public function int8le(): Int8 |
162
|
|
|
{ |
163
|
6 |
|
return new Int8(ByteOrder::LE); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Add a int16 serializer to the template |
168
|
|
|
* |
169
|
|
|
* @return Int16 |
170
|
|
|
*/ |
171
|
6 |
|
public function int16(): Int16 |
172
|
|
|
{ |
173
|
6 |
|
return new Int16(ByteOrder::BE); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Add a little-endian Int16 serializer to the template |
178
|
|
|
* |
179
|
|
|
* @return Int16 |
180
|
|
|
*/ |
181
|
6 |
|
public function int16le(): Int16 |
182
|
|
|
{ |
183
|
6 |
|
return new Int16(ByteOrder::LE); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Add a int32 serializer to the template |
188
|
|
|
* |
189
|
|
|
* @return Int32 |
190
|
|
|
*/ |
191
|
6 |
|
public function int32(): Int32 |
192
|
|
|
{ |
193
|
6 |
|
return new Int32(ByteOrder::BE); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Add a little-endian Int serializer to the template |
198
|
|
|
* |
199
|
|
|
* @return Int32 |
200
|
|
|
*/ |
201
|
6 |
|
public function int32le(): Int32 |
202
|
|
|
{ |
203
|
6 |
|
return new Int32(ByteOrder::LE); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Add a int64 serializer to the template |
208
|
|
|
* |
209
|
|
|
* @return Int64 |
210
|
|
|
*/ |
211
|
6 |
|
public function int64(): Int64 |
212
|
|
|
{ |
213
|
6 |
|
return new Int64(ByteOrder::BE); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Add a little-endian Int64 serializer to the template |
218
|
|
|
* |
219
|
|
|
* @return Int64 |
220
|
|
|
*/ |
221
|
6 |
|
public function int64le(): Int64 |
222
|
|
|
{ |
223
|
6 |
|
return new Int64(ByteOrder::LE); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Add a int128 serializer to the template |
228
|
|
|
* |
229
|
|
|
* @return Int128 |
230
|
|
|
*/ |
231
|
6 |
|
public function int128(): Int128 |
232
|
|
|
{ |
233
|
6 |
|
return new Int128(ByteOrder::BE); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Add a little-endian Int128 serializer to the template |
238
|
|
|
* |
239
|
|
|
* @return Int128 |
240
|
|
|
*/ |
241
|
6 |
|
public function int128le(): Int128 |
242
|
|
|
{ |
243
|
6 |
|
return new Int128(ByteOrder::LE); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Add a int256 serializer to the template |
248
|
|
|
* |
249
|
|
|
* @return Int256 |
250
|
|
|
*/ |
251
|
6 |
|
public function int256(): Int256 |
252
|
|
|
{ |
253
|
6 |
|
return new Int256(ByteOrder::BE); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Add a little-endian Int256 serializer to the template |
258
|
|
|
* |
259
|
|
|
* @return Int256 |
260
|
|
|
*/ |
261
|
6 |
|
public function int256le(): Int256 |
262
|
|
|
{ |
263
|
6 |
|
return new Int256(ByteOrder::LE); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Add a VarInt serializer to the template |
268
|
|
|
* |
269
|
|
|
* @return VarInt |
270
|
|
|
*/ |
271
|
4 |
|
public function varint(): VarInt |
272
|
|
|
{ |
273
|
4 |
|
return new VarInt(); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Add a VarString serializer to the template |
278
|
|
|
* |
279
|
|
|
* @return VarString |
280
|
|
|
*/ |
281
|
2 |
|
public function varstring(): VarString |
282
|
|
|
{ |
283
|
2 |
|
return new VarString(new VarInt()); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Add a byte string serializer to the template. This serializer requires a length to |
288
|
|
|
* pad/truncate to. |
289
|
|
|
* |
290
|
|
|
* @param int $length |
291
|
|
|
* @return ByteString |
292
|
|
|
*/ |
293
|
|
|
public function bytestring(int $length): ByteString |
294
|
|
|
{ |
295
|
|
|
return new ByteString($length, ByteOrder::BE); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Add a little-endian byte string serializer to the template. This serializer requires |
300
|
|
|
* a length to pad/truncate to. |
301
|
|
|
* |
302
|
|
|
* @param int $length |
303
|
|
|
* @return ByteString |
304
|
|
|
*/ |
305
|
|
|
public function bytestringle(int $length): ByteString |
306
|
|
|
{ |
307
|
|
|
return new ByteString($length, ByteOrder::LE); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Add a vector serializer to the template. A $readHandler must be provided if the |
312
|
|
|
* template will be used to deserialize a vector, since it's contents are not known. |
313
|
|
|
* |
314
|
|
|
* The $readHandler should operate on the parser reference, reading the bytes for each |
315
|
|
|
* item in the collection. |
316
|
|
|
* |
317
|
|
|
* @param callable $readHandler |
318
|
|
|
* @return Vector |
319
|
|
|
*/ |
320
|
2 |
|
public function vector(callable $readHandler): Vector |
321
|
|
|
{ |
322
|
2 |
|
return new Vector($this->varint(), $readHandler); |
323
|
|
|
} |
324
|
|
|
} |
325
|
|
|
|