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