Completed
Pull Request — master (#403)
by thomas
23:44
created

InputCollectionMutator::slice()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 2
nop 2
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace BitWasp\Bitcoin\Transaction\Mutator;
4
5
use BitWasp\Bitcoin\Transaction\TransactionInputInterface;
6
7
class InputCollectionMutator extends AbstractCollectionMutator
8
{
9
10
    /**
11
     * @param TransactionInputInterface[] $inputs
12
     */
13 254
    public function __construct(array $inputs)
14
    {
15
        /** @var InputMutator[] $set */
16 254
        $set = [];
17 254
        foreach ($inputs as $i => $input) {
18 248
            $set[$i] = new InputMutator($input);
19 105
        }
20
21 254
        $this->set = \SplFixedArray::fromArray($set, false);
22 254
    }
23
24
    /**
25
     * @return InputMutator
26
     */
27 236
    public function current()
28
    {
29 236
        return $this->set->current();
30
    }
31
32
    /**
33
     * @param int $offset
34
     * @return InputMutator
35
     */
36 242
    public function offsetGet($offset)
37
    {
38 242
        if (!$this->set->offsetExists($offset)) {
39
            throw new \OutOfRangeException('Input does not exist');
40
        }
41
42 242
        return $this->set->offsetGet($offset);
43
    }
44
45
    /**
46
     * @return TransactionInputInterface[]
47
     */
48 248
    public function done()
49
    {
50 248
        $set = [];
51 248
        foreach ($this->set as $mutator) {
52 242
            $set[] = $mutator->done();
53 102
        }
54
55 248
        return $set;
56
    }
57
58
    /**
59
     * @param int $start
60
     * @param int $length
61
     * @return $this
62
     */
63 31
    public function slice($start, $length)
64
    {
65 31
        $end = $this->set->getSize();
66 31
        if ($start > $end || $length > $end) {
67 6
            throw new \RuntimeException('Invalid start or length');
68
        }
69
70 25
        $this->set = \SplFixedArray::fromArray(array_slice($this->set->toArray(), $start, $length), false);
71 25
        return $this;
72
    }
73
74
    /**
75
     * @return $this
76
     */
77 25
    public function null()
78
    {
79 25
        $this->slice(0, 0);
80 25
        return $this;
81
    }
82
83
    /**
84
     * @param TransactionInputInterface $input
85
     * @return $this
86
     */
87 19
    public function add(TransactionInputInterface $input)
88
    {
89 19
        $size = $this->set->getSize();
90 19
        $this->set->setSize($size + 1);
91
92 19
        $this->set[$size] = new InputMutator($input);
93 19
        return $this;
94
    }
95
96
    /**
97
     * @param int $i
98
     * @param TransactionInputInterface $input
99
     * @return $this
100
     */
101
    public function set($i, TransactionInputInterface $input)
102
    {
103
        $this->set[$i] = new InputMutator($input);
104
        return $this;
105
    }
106
}
107