ScriptWitnessSerializer   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 14
c 2
b 0
f 0
dl 0
loc 45
ccs 15
cts 15
cp 1
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A fromParser() 0 9 2
A serialize() 0 8 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\Bitcoin\Serializer\Script;
6
7
use BitWasp\Bitcoin\Script\ScriptWitness;
8
use BitWasp\Bitcoin\Script\ScriptWitnessInterface;
9
use BitWasp\Bitcoin\Serializer\Types;
10
use BitWasp\Buffertools\Buffer;
11
use BitWasp\Buffertools\BufferInterface;
12
use BitWasp\Buffertools\Parser;
13
14
class ScriptWitnessSerializer
15
{
16
    /**
17
     * @var \BitWasp\Buffertools\Types\VarString
18
     */
19
    private $varstring;
20
21
    /**
22
     * @var \BitWasp\Buffertools\Types\VarInt
23
     */
24
    private $varint;
25
26 5237
    public function __construct()
27
    {
28 5237
        $this->varstring = Types::varstring();
29 5237
        $this->varint = Types::varint();
30 5237
    }
31
32
    /**
33
     * @param Parser $parser
34
     * @return ScriptWitnessInterface
35
     */
36 11
    public function fromParser(Parser $parser): ScriptWitnessInterface
37
    {
38 11
        $size = $this->varint->read($parser);
39 11
        $entries = [];
40 11
        for ($j = 0; $j < $size; $j++) {
41 11
            $entries[] = $this->varstring->read($parser);
42
        }
43
44 11
        return new ScriptWitness(...$entries);
45
    }
46
47
    /**
48
     * @param ScriptWitnessInterface $witness
49
     * @return BufferInterface
50
     */
51 130
    public function serialize(ScriptWitnessInterface $witness): BufferInterface
52
    {
53 130
        $binary = $this->varint->write($witness->count());
54 130
        foreach ($witness as $value) {
55 130
            $binary .= $this->varstring->write($value);
56
        }
57
58 130
        return new Buffer($binary);
59
    }
60
}
61