Completed
Pull Request — master (#222)
by thomas
24:34
created

TransactionOutputCollection::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 10
ccs 8
cts 8
cp 1
rs 9.4286
cc 3
eloc 6
nc 3
nop 1
crap 3
1
<?php
2
3
namespace BitWasp\Bitcoin\Collection\Transaction;
4
5
use BitWasp\Bitcoin\Collection\StaticCollection;
6
use BitWasp\Bitcoin\Transaction\TransactionOutputInterface;
7
8
class TransactionOutputCollection extends StaticCollection
9
{
10
11
    /**
12
     * Initialize a new collection with a list of outputs.
13
     *
14
     * @param TransactionOutputInterface[] $outputs
15
     */
16 1242
    public function __construct(array $outputs = [])
17
    {
18 1242
        $this->set = new \SplFixedArray(count($outputs));
19 1242
        foreach ($outputs as $idx => $output) {
20 405
            if (!$output instanceof TransactionOutputInterface) {
21 6
                throw new \InvalidArgumentException('Must provide TransactionOutputInterface[] to TransactionOutputCollection');
22
            }
23 399
            $this->set->offsetSet($idx, $output);
24 1236
        }
25 1236
    }
26
27 111
    public function __clone()
28
    {
29 111
        $outputs = $this->set;
30 111
        $this->set = new \SplFixedArray(count($outputs));
31
32 111
        foreach ($outputs as $idx => $output) {
33 42
            $this->set->offsetSet($idx, $output);
34 111
        }
35 111
    }
36
37
    /**
38
     * @return TransactionOutputInterface
39
     */
40 78
    public function current()
41
    {
42 78
        return $this->set->current();
43
    }
44
45
    /**
46
     * @param int $offset
47
     * @return TransactionOutputInterface
48
     */
49 171
    public function offsetGet($offset)
50
    {
51 171
        if (!$this->set->offsetExists($offset)) {
52 12
            throw new \OutOfRangeException('No offset found');
53
        }
54
55 159
        return $this->set->offsetGet($offset);
56
    }
57
}
58