Completed
Push — 0.0.34 ( 61212b...2e9020 )
by thomas
20:55
created

OutputCollectionMutator   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 93.94%

Importance

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