Completed
Push — master ( 31933a...221a1f )
by thomas
20:56
created

InputCollectionMutator   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 73.52%

Importance

Changes 0
Metric Value
dl 0
loc 100
ccs 25
cts 34
cp 0.7352
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 2

8 Methods

Rating   Name   Duplication   Size   Complexity  
A done() 0 9 2
A current() 0 4 1
A slice() 0 10 3
A null() 0 5 1
A set() 0 5 1
A offsetGet() 0 8 2
A __construct() 0 10 2
A add() 0 8 1
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 56
    public function __construct(array $inputs)
14
    {
15
        /** @var InputMutator[] $set */
16 56
        $set = [];
17 56
        foreach ($inputs as $i => $input) {
18 54
            $set[$i] = new InputMutator($input);
19
        }
20
21 56
        $this->set = \SplFixedArray::fromArray($set, false);
22 56
    }
23
24
    /**
25
     * @return InputMutator
26
     */
27 50
    public function current()
28
    {
29 50
        return $this->set->current();
30
    }
31
32
    /**
33
     * @param int $offset
34
     * @return InputMutator
35
     */
36 52
    public function offsetGet($offset)
37
    {
38 52
        if (!$this->set->offsetExists($offset)) {
39
            throw new \OutOfRangeException('Input does not exist');
40
        }
41
42 52
        return $this->set->offsetGet($offset);
43
    }
44
45
    /**
46
     * @return TransactionInputInterface[]
47
     */
48 54
    public function done()
49
    {
50 54
        $set = [];
51 54
        foreach ($this->set as $mutator) {
52 52
            $set[] = $mutator->done();
53
        }
54
55 54
        return $set;
56
    }
57
58
    /**
59
     * @param int $start
60
     * @param int $length
61
     * @return $this
62
     */
63 4
    public function slice($start, $length)
64
    {
65 4
        $end = $this->set->getSize();
66 4
        if ($start > $end || $length > $end) {
67 2
            throw new \RuntimeException('Invalid start or length');
68
        }
69
70 2
        $this->set = \SplFixedArray::fromArray(array_slice($this->set->toArray(), $start, $length), false);
71 2
        return $this;
72
    }
73
74
    /**
75
     * @return $this
76
     */
77 2
    public function null()
78
    {
79 2
        $this->slice(0, 0);
80 2
        return $this;
81
    }
82
83
    /**
84
     * @param TransactionInputInterface $input
85
     * @return $this
86
     */
87
    public function add(TransactionInputInterface $input)
88
    {
89
        $size = $this->set->getSize();
90
        $this->set->setSize($size + 1);
91
92
        $this->set[$size] = new InputMutator($input);
93
        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