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