InputCollectionMutator   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Test Coverage

Coverage 73.52%

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 98
ccs 25
cts 34
cp 0.7352
rs 10
c 0
b 0
f 0
wmc 13

8 Methods

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