| Code Coverage
| Lines | Functions and Methods | Classes and Traits
Total | 100.00% covered (success) | 100.00% | 20 / 20 | 100.00% covered (success) | 100.00% | 2 / 2 | CRAP | 100.00% covered (success) | 100.00% | 1 / 1
ShortVec | 100.00% covered (success) | 100.00% | 20 / 20 | 100.00% covered (success) | 100.00% | 2 / 2 | 6 | 100.00% covered (success) | 100.00% | 1 / 1
decodeLength | 100.00% covered (success) | 100.00% | 10 / 10 | 100.00% covered (success) | 100.00% | 1 / 1 | 3 |
encodeLength | 100.00% covered (success) | 100.00% | 10 / 10 | 100.00% covered (success) | 100.00% | 1 / 1 | 3 |

1 | <?php
2 |
3 | namespace Attestto\SolanaPhpSdk\Util;
4 |
5 | class ShortVec
6 | {
7 | /**
8 | * @param $buffer
9 | * @return array list($length, $size)
10 | */
11 | public static function decodeLength($buffer): array
12 | {
13 | $buffer = Buffer::from($buffer)->toArray();
14 |
15 | $len = 0;
16 | $size = 0;
17 | while ($size < sizeof($buffer)) {
18 | $elem = $buffer[$size];
19 | $len |= ($elem & 0x7F) << ($size * 7);
20 | $size++;
21 | if (($elem & 0x80) == 0) {
22 | break;
23 | }
24 | }
25 | return [$len, $size];
26 | }
27 |
28 | public static function encodeLength(int $length): array
29 | {
30 | $elems = [];
31 | $rem_len = $length;
32 |
33 | for (;;) {
34 | $elem = $rem_len & 0x7f;
35 | $rem_len >>= 7;
36 | if (! $rem_len) {
37 | array_push($elems, $elem);
38 | break;
39 | }
40 | $elem |= 0x80;
41 | array_push($elems, $elem);
42 | }
43 |
44 | return $elems;
45 | }
46 | }