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