Completed
Push — master ( aa76e6...0e5776 )
by thomas
66:20 queued 34:22
created

TransactionOutputSerializer::parse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\Bitcoin\Serializer\Transaction;
6
7
use BitWasp\Bitcoin\Script\Opcodes;
8
use BitWasp\Bitcoin\Script\Script;
9
use BitWasp\Bitcoin\Serializer\Types;
10
use BitWasp\Bitcoin\Transaction\TransactionOutput;
11
use BitWasp\Bitcoin\Transaction\TransactionOutputInterface;
12
use BitWasp\Buffertools\Buffer;
13
use BitWasp\Buffertools\BufferInterface;
14
use BitWasp\Buffertools\Parser;
15
16
class TransactionOutputSerializer
17
{
18
    /**
19
     * @var \BitWasp\Buffertools\Types\Uint64
20
     */
21
    private $uint64le;
22
23
    /**
24
     * @var \BitWasp\Buffertools\Types\VarString
25
     */
26
    private $varstring;
27
28
    /**
29
     * @var Opcodes
30
     */
31
    private $opcodes;
32
33
    /**
34
     * TransactionOutputSerializer constructor.
35
     * @param Opcodes|null $opcodes
36
     */
37 5243
    public function __construct(Opcodes $opcodes = null)
38
    {
39 5243
        $this->uint64le = Types::uint64le();
40 5243
        $this->varstring = Types::varstring();
41 5243
        $this->opcodes = $opcodes ?: new Opcodes();
42 5243
    }
43
44
    /**
45
     * @param TransactionOutputInterface $output
46
     * @return BufferInterface
47
     */
48 5228
    public function serialize(TransactionOutputInterface $output): BufferInterface
49
    {
50 5228
        return new Buffer(
51 5228
            $this->uint64le->write($output->getValue()) .
52 5228
            $this->varstring->write($output->getScript()->getBuffer())
53
        );
54
    }
55
56
    /**
57
     * @param Parser $parser
58
     * @return TransactionOutputInterface
59
     * @throws \BitWasp\Buffertools\Exceptions\ParserOutOfRange
60
     */
61 68
    public function fromParser(Parser $parser): TransactionOutputInterface
62
    {
63 68
        return new TransactionOutput(
64 68
            (int) $this->uint64le->read($parser),
65 68
            new Script($this->varstring->read($parser), $this->opcodes)
66
        );
67
    }
68
69
    /**
70
     * @param BufferInterface $string
71
     * @return TransactionOutputInterface
72
     * @throws \BitWasp\Buffertools\Exceptions\ParserOutOfRange
73
     */
74 1
    public function parse(BufferInterface $string): TransactionOutputInterface
75
    {
76 1
        return $this->fromParser(new Parser($string));
77
    }
78
}
79