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

TransactionCollection::all()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace BitWasp\Bitcoin\Collection\Transaction;
4
5
use BitWasp\Bitcoin\Collection\StaticCollection;
6
use BitWasp\Bitcoin\Transaction\TransactionInterface;
7
8
class TransactionCollection extends StaticCollection
9
{
10
    /**
11
     * Initialize a new collection with a list of transactions.
12
     *
13
     * @param TransactionInterface[] $transactions
14
     */
15 174
    public function __construct(array $transactions = [])
16
    {
17 174
        foreach ($transactions as $tx) {
18 144
            if (!$tx instanceof TransactionInterface) {
19 6
                throw new \InvalidArgumentException('Must provide TransactionInterface[] to TransactionCollection');
20
            }
21 174
        }
22
23 168
        $this->set = \SplFixedArray::fromArray($transactions);
24 168
    }
25
26 6
    public function __clone()
27
    {
28 6
        $this->set = \SplFixedArray::fromArray(array_map(
29 6
            function (TransactionInterface $tx) {
30 6
                return clone $tx;
31 6
            },
32 6
            $this->set->toArray()
33 6
        ));
34 6
    }
35
36
    /**
37
     * @return TransactionInterface
38
     */
39 66
    public function current()
40
    {
41 66
        return $this->set->current();
42
    }
43
44
    /**
45
     * @param int $offset
46
     * @return TransactionInterface
47
     */
48 48
    public function offsetGet($offset)
49
    {
50 48
        if (!$this->set->offsetExists($offset)) {
51 6
            throw new \OutOfRangeException('No offset found');
52
        }
53
54 42
        return $this->set->offsetGet($offset);
55
    }
56
57
    /**
58
     * Returns all the transactions in the collection.
59
     *
60
     * @return TransactionInterface[]
61
     */
62 30
    public function all()
63
    {
64 30
        return $this->set->toArray();
65
    }
66
}
67