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