| Code Coverage
| Lines | Functions and Methods | Classes and Traits
Total | 69.23% covered (warning) | 69.23% | 18 / 26 | 68.75% covered (warning) | 68.75% | 11 / 16 | CRAP | 0.00% covered (danger) | 0.00% | 0 / 1
BinaryWriter | 69.23% covered (warning) | 69.23% | 18 / 26 | 68.75% covered (warning) | 68.75% | 11 / 16 | 25.42 | 0.00% covered (danger) | 0.00% | 0 / 1
__construct | 100.00% covered (success) | 100.00% | 2 / 2 | 100.00% covered (success) | 100.00% | 1 / 1 | 1 |
writeU8 | 100.00% covered (success) | 100.00% | 1 / 1 | 100.00% covered (success) | 100.00% | 1 / 1 | 1 |
writeU16 | 0.00% covered (danger) | 0.00% | 0 / 1 | 0.00% covered (danger) | 0.00% | 0 / 1 | 2 |
writeU32 | 100.00% covered (success) | 100.00% | 1 / 1 | 100.00% covered (success) | 100.00% | 1 / 1 | 1 |
writeU64 | 100.00% covered (success) | 100.00% | 1 / 1 | 100.00% covered (success) | 100.00% | 1 / 1 | 1 |
writeI8 | 100.00% covered (success) | 100.00% | 1 / 1 | 100.00% covered (success) | 100.00% | 1 / 1 | 1 |
writeI16 | 0.00% covered (danger) | 0.00% | 0 / 1 | 0.00% covered (danger) | 0.00% | 0 / 1 | 2 |
writeI32 | 100.00% covered (success) | 100.00% | 1 / 1 | 100.00% covered (success) | 100.00% | 1 / 1 | 1 |
writeI64 | 0.00% covered (danger) | 0.00% | 0 / 1 | 0.00% covered (danger) | 0.00% | 0 / 1 | 2 |
writeF32 | 0.00% covered (danger) | 0.00% | 0 / 1 | 0.00% covered (danger) | 0.00% | 0 / 1 | 2 |
writeF64 | 100.00% covered (success) | 100.00% | 1 / 1 | 100.00% covered (success) | 100.00% | 1 / 1 | 1 |
writeString | 100.00% covered (success) | 100.00% | 4 / 4 | 100.00% covered (success) | 100.00% | 1 / 1 | 1 |
writeFixedArray | 100.00% covered (success) | 100.00% | 2 / 2 | 100.00% covered (success) | 100.00% | 1 / 1 | 1 |
writeArray | 0.00% covered (danger) | 0.00% | 0 / 4 | 0.00% covered (danger) | 0.00% | 0 / 1 | 6 |
writeBuffer | 100.00% covered (success) | 100.00% | 3 / 3 | 100.00% covered (success) | 100.00% | 1 / 1 | 1 |
toArray | 100.00% covered (success) | 100.00% | 1 / 1 | 100.00% covered (success) | 100.00% | 1 / 1 | 1 |

1 | <?php
2 |
3 | namespace Attestto\SolanaPhpSdk\Borsh;
4 |
5 | use Attestto\SolanaPhpSdk\Exceptions\TodoException;
6 | use Attestto\SolanaPhpSdk\Util\Buffer;
7 | use Closure;
8 |
9 | class BinaryWriter
10 | {
11 | protected Buffer $buffer;
12 | protected int $length;
13 |
14 | public function __construct()
15 | {
16 | $this->buffer = Buffer::from();
17 | $this->length = 0;
18 | }
19 |
20 | /**
21 | * @param int $value
22 | * @return $this
23 | */
24 | public function writeU8(int $value)
25 | {
26 | return $this->writeBuffer(Buffer::from($value, Buffer::TYPE_BYTE, false));
27 | }
28 |
29 | /**
30 | * @param int $value
31 | * @return $this
32 | */
33 | public function writeU16(int $value)
34 | {
35 | return $this->writeBuffer(Buffer::from($value, Buffer::TYPE_SHORT, false));
36 | }
37 |
38 | /**
39 | * @param int $value
40 | * @return $this
41 | */
42 | public function writeU32(int $value)
43 | {
44 | return $this->writeBuffer(Buffer::from($value, Buffer::TYPE_INT, false));
45 | }
46 |
47 | /**
48 | * @param int $value
49 | * @return $this
50 | */
51 | public function writeU64(int $value)
52 | {
53 | return $this->writeBuffer(Buffer::from($value, Buffer::TYPE_LONG, false));
54 | }
55 |
56 | /**
57 | * @param int $value
58 | * @return $this
59 | */
60 | public function writeI8(int $value)
61 | {
62 | return $this->writeBuffer(Buffer::from($value, Buffer::TYPE_BYTE, true));
63 | }
64 |
65 | /**
66 | * @param int $value
67 | * @return $this
68 | */
69 | public function writeI16(int $value)
70 | {
71 | return $this->writeBuffer(Buffer::from($value, Buffer::TYPE_SHORT, true));
72 | }
73 |
74 | /**
75 | * @param int $value
76 | * @return $this
77 | */
78 | public function writeI32(int $value)
79 | {
80 | return $this->writeBuffer(Buffer::from($value, Buffer::TYPE_INT, true));
81 | }
82 |
83 | /**
84 | * @param int $value
85 | * @return $this
86 | */
87 | public function writeI64(int $value)
88 | {
89 | return $this->writeBuffer(Buffer::from($value, Buffer::TYPE_LONG, true));
90 | }
91 |
92 | /**
93 | * @param float $value
94 | * @return $this
95 | */
96 | public function writeF32(float $value)
97 | {
98 | return $this->writeBuffer(Buffer::from($value, Buffer::TYPE_FLOAT, true)->fixed(4));
99 | }
100 |
101 | /**
102 | * @param float $value
103 | * @return $this
104 | */
105 | public function writeF64(float $value)
106 | {
107 | return $this->writeBuffer(Buffer::from($value, Buffer::TYPE_FLOAT, true)->fixed(8));
108 | }
109 |
110 | /**
111 | * @param string $value
112 | * @return $this
113 | */
114 | public function writeString(string $value)
115 | {
116 | $valueBuffer = Buffer::from($value);
117 | $this->writeU32(sizeof($valueBuffer));
118 | $this->writeBuffer($valueBuffer);
119 | return $this;
120 | }
121 |
122 | /**
123 | * @param array $array
124 | * @return $this
125 | */
126 | public function writeFixedArray(array $array)
127 | {
128 | $this->writeBuffer(Buffer::from($array));
129 | return $this;
130 | }
131 |
132 | /**
133 | * @param array $array
134 | * @return $this
135 | */
136 | public function writeArray(array $array, Closure $writeFn)
137 | {
138 | $this->writeU32(sizeof($array));
139 | foreach ($array as $item) {
140 | $writeFn($item);
141 | }
142 | return $this;
143 | }
144 |
145 | /**
146 | * @param Buffer $buffer
147 | * @return $this
148 | */
149 | protected function writeBuffer(Buffer $buffer)
150 | {
151 | $this->buffer->push($buffer);
152 | $this->length += sizeof($buffer);
153 | return $this;
154 | }
155 |
156 | /**
157 | * @return array
158 | */
159 | public function toArray()
160 | {
161 | return $this->buffer->slice(0, $this->length)->toArray();
162 | }
163 | }