Completed
Pull Request — master (#278)
by thomas
73:26 queued 01:49
created

OutPoint::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 4
Bugs 0 Features 2
Metric Value
c 4
b 0
f 2
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 9.4285
cc 2
eloc 8
nc 2
nop 2
crap 2
1
<?php
2
3
namespace BitWasp\Bitcoin\Transaction;
4
5
use BitWasp\Bitcoin\Serializable;
6
use BitWasp\Bitcoin\Serializer\Transaction\OutPointSerializer;
7
use BitWasp\Buffertools\BufferInterface;
8
use BitWasp\CommonTrait\FunctionAliasArrayAccess;
9
10
class OutPoint extends Serializable implements OutPointInterface
11
{
12
    use FunctionAliasArrayAccess;
13
14
    /**
15
     * @var BufferInterface
16
     */
17
    private $hashPrevOutput;
18
19
    /**
20
     * @var int
21
     */
22
    private $nPrevOutput;
23
24
    /**
25
     * OutPoint constructor.
26
     * @param BufferInterface $hashPrevOutput
27
     * @param int $nPrevOutput
28
     */
29 1140
    public function __construct(BufferInterface $hashPrevOutput, $nPrevOutput)
30
    {
31 1140
        if ($hashPrevOutput->getSize() !== 32) {
32 6
            throw new \InvalidArgumentException('OutPoint: hashPrevOut must be a 32-byte Buffer');
33
        }
34
        
35 1134
        $this->hashPrevOutput = $hashPrevOutput;
36 1134
        $this->nPrevOutput = $nPrevOutput;
37
38 1134
        $this
39 1134
            ->initFunctionAlias('txid', 'getTxId')
40 1134
            ->initFunctionAlias('vout', 'getVout');
41 1134
    }
42
43
    /**
44
     * @return BufferInterface
45
     */
46 342
    public function getTxId()
47
    {
48 342
        return $this->hashPrevOutput;
49
    }
50
51
    /**
52
     * @return int
53
     */
54 348
    public function getVout()
55
    {
56 348
        return $this->nPrevOutput;
57
    }
58
59
    /**
60
     * @param OutPointInterface $outPoint
61
     * @return int
62 72
     */
63
    public function equals(OutPointInterface $outPoint)
64 72
    {
65
        $txid = strcmp($this->getTxId()->getBinary(), $outPoint->getTxId()->getBinary());
66
        if ($txid !== 0) {
67
            return false;
68
        }
69
70
        return gmp_cmp($this->getVout(), $outPoint->getVout()) === 0;
71
    }
72
73
    /**
74
     * @return BufferInterface
75
     */
76
    public function getBuffer()
77
    {
78
        return (new OutPointSerializer())->serialize($this);
79
    }
80
}
81