Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
83.78% |
31 / 37 |
|
78.95% |
15 / 19 |
CRAP | |
0.00% |
0 / 1 |
| BinaryReader | |
83.78% |
31 / 37 |
|
78.95% |
15 / 19 |
22.88 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| readU8 | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| readU16 | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| readU32 | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| readU64 | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| readI8 | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| readI16 | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| readI32 | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| readI64 | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| readUnsignedInt | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| readSignedInt | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| readF32 | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| readF64 | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| readString | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| readFixedArray | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| readPubKey | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| readPubKeyAsString | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| readArray | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| readBuffer | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
2.03 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Attestto\SolanaPhpSdk\Borsh; |
| 4 | |
| 5 | use Attestto\SolanaPhpSdk\Exceptions\TodoException; |
| 6 | use Attestto\SolanaPhpSdk\Util\Buffer; |
| 7 | use Attestto\SolanaPhpSdk\PublicKey; |
| 8 | use Closure; |
| 9 | |
| 10 | class BinaryReader |
| 11 | { |
| 12 | protected Buffer $buffer; |
| 13 | protected int $offset; |
| 14 | |
| 15 | public function __construct(Buffer $buffer) |
| 16 | { |
| 17 | $this->buffer = $buffer; |
| 18 | $this->offset = 0; |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * @return int |
| 23 | */ |
| 24 | public function readU8(): int |
| 25 | { |
| 26 | return $this->readUnsignedInt(1, Buffer::TYPE_BYTE); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * @return int |
| 31 | */ |
| 32 | public function readU16(): int |
| 33 | { |
| 34 | return $this->readUnsignedInt(2, Buffer::TYPE_SHORT); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * @return int |
| 39 | */ |
| 40 | public function readU32(): int |
| 41 | { |
| 42 | return $this->readUnsignedInt(4, Buffer::TYPE_INT); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * @return int |
| 47 | */ |
| 48 | public function readU64(): int |
| 49 | { |
| 50 | return $this->readUnsignedInt(8, Buffer::TYPE_LONG); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * @return int |
| 55 | */ |
| 56 | public function readI8(): int |
| 57 | { |
| 58 | return $this->readSignedInt(1, Buffer::TYPE_BYTE); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * @return int |
| 63 | */ |
| 64 | public function readI16(): int |
| 65 | { |
| 66 | return $this->readSignedInt(2, Buffer::TYPE_SHORT); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * @return int |
| 71 | */ |
| 72 | public function readI32(): int |
| 73 | { |
| 74 | return $this->readSignedInt(4, Buffer::TYPE_INT); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * @return int |
| 79 | */ |
| 80 | public function readI64(): int |
| 81 | { |
| 82 | return $this->readSignedInt(8, Buffer::TYPE_LONG); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * @param $length |
| 87 | * @return int |
| 88 | */ |
| 89 | protected function readUnsignedInt($length, $datatype): int |
| 90 | { |
| 91 | $value = $this->buffer->slice($this->offset, $length, $datatype, false)->value(); |
| 92 | $this->offset += $length; |
| 93 | return $value; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * @param $length |
| 98 | * @return int |
| 99 | */ |
| 100 | protected function readSignedInt($length, $datatype): int |
| 101 | { |
| 102 | $value = $this->buffer->slice($this->offset, $length, $datatype, true)->value(); |
| 103 | $this->offset += $length; |
| 104 | return $value; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * @return float |
| 109 | */ |
| 110 | public function readF32(): float |
| 111 | { |
| 112 | $value = $this->buffer->slice($this->offset, 4, Buffer::TYPE_FLOAT, true)->value(); |
| 113 | $this->offset += 4; |
| 114 | return $value; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * @return float |
| 119 | */ |
| 120 | public function readF64(): float |
| 121 | { |
| 122 | $value = $this->buffer->slice($this->offset, 8, Buffer::TYPE_FLOAT, true)->value(); |
| 123 | $this->offset += 8; |
| 124 | return $value; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * @return string |
| 129 | * @throws BorshException |
| 130 | */ |
| 131 | public function readString(): string |
| 132 | { |
| 133 | $length = $this->readU32(); |
| 134 | return $this->readBuffer($length); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * @param int $length |
| 139 | * @return array |
| 140 | */ |
| 141 | public function readFixedArray(int $length): array |
| 142 | { |
| 143 | return $this->readBuffer($length)->toArray(); |
| 144 | } |
| 145 | |
| 146 | public function readPubKey(): PublicKey |
| 147 | { |
| 148 | return new PublicKey($this->readFixedArray(32)); |
| 149 | } |
| 150 | |
| 151 | public function readPubKeyAsString(): string |
| 152 | { |
| 153 | return $this->readPubKey()->toBase58(); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * @return array |
| 158 | */ |
| 159 | public function readArray(Closure $readEachItem): array |
| 160 | { |
| 161 | $length = $this->readU32(); |
| 162 | $array = []; |
| 163 | for ($i = 0; $i < $length; $i++) { |
| 164 | array_push($array, $readEachItem()); |
| 165 | } |
| 166 | return $array; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * @param $length |
| 171 | * @return Buffer |
| 172 | * @throws BorshException |
| 173 | */ |
| 174 | protected function readBuffer($length): Buffer |
| 175 | { |
| 176 | if ($this->offset + $length > sizeof($this->buffer)) { |
| 177 | throw new BorshException("Expected buffer length {$length} isn't within bounds"); |
| 178 | } |
| 179 | |
| 180 | $buffer = $this->buffer->slice($this->offset, $length); |
| 181 | $this->offset += $length; |
| 182 | return $buffer; |
| 183 | } |
| 184 | |
| 185 | } |