Completed
Pull Request — master (#446)
by thomas
228:15 queued 224:06
created

TransactionOutputSerializer::getTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

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