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