CachingTypeFactory::bytestringle()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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