|
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
|
|
|
|