Completed
Push — master ( 29ec42...778040 )
by thomas
9s
created

OutPointSerializer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
namespace BitWasp\Bitcoin\Serializer\Transaction;
4
5
use BitWasp\Bitcoin\Transaction\OutPoint;
6
use BitWasp\Bitcoin\Transaction\OutPointInterface;
7
use BitWasp\Buffertools\Parser;
8
use BitWasp\Buffertools\TemplateFactory;
9
10
class OutPointSerializer implements OutPointSerializerInterface
11
{
12
    /**
13
     * @var \BitWasp\Buffertools\Template
14
     */
15
    private $template;
16
17 171
    public function __construct()
18
    {
19 171
        $this->template = $this->getTemplate();
20 171
    }
21
22
    /**
23
     * @return \BitWasp\Buffertools\Template
24
     */
25 171
    public function getTemplate()
26
    {
27 171
        return (new TemplateFactory())
28 171
            ->bytestringle(32)
29 171
            ->uint32le()
30 171
            ->getTemplate();
31
    }
32
33
    /**
34
     * @param OutPointInterface $outpoint
35
     * @return \BitWasp\Buffertools\BufferInterface
36
     */
37 120
    public function serialize(OutPointInterface $outpoint)
38
    {
39 120
        return $this->template->write([
40 120
            $outpoint->getTxId(),
41 120
            $outpoint->getVout()
42 80
        ]);
43
    }
44
45
    /**
46
     * @param Parser $parser
47
     * @return OutPointInterface
48
     */
49 114
    public function fromParser(Parser $parser)
50
    {
51 114
        list ($txid, $vout) = $this->template->parse($parser);
52
53 114
        return new OutPoint($txid, $vout);
54
    }
55
56
    /**
57
     * @param string|\BitWasp\Buffertools\BufferInterface $data
58
     * @return OutPointInterface
59
     */
60
    public function parse($data)
61
    {
62
        return $this->fromParser(new Parser($data));
63
    }
64
}
65