1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BitWasp\Bitcoin\Collection; |
6
|
|
|
|
7
|
|
|
use BitWasp\Buffertools\BufferInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @deprecated v2.0.0 |
11
|
|
|
*/ |
12
|
|
|
abstract class StaticCollection implements CollectionInterface |
|
|
|
|
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
protected $set; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var int |
21
|
|
|
*/ |
22
|
|
|
protected $position = 0; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @return array |
26
|
|
|
*/ |
27
|
49 |
|
public function all(): array |
28
|
|
|
{ |
29
|
49 |
|
return $this->set; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param int $start |
34
|
|
|
* @param int $length |
35
|
|
|
* @return self |
36
|
|
|
*/ |
37
|
228 |
|
public function slice(int $start, int $length) |
38
|
|
|
{ |
39
|
228 |
|
$end = count($this->set); |
40
|
228 |
|
if ($start > $end || $length > $end) { |
41
|
|
|
throw new \RuntimeException('Invalid start or length'); |
42
|
|
|
} |
43
|
|
|
|
44
|
228 |
|
$sliced = array_slice($this->set, $start, $length); |
45
|
228 |
|
return new static(...$sliced); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return int |
50
|
|
|
*/ |
51
|
453 |
|
public function count(): int |
52
|
|
|
{ |
53
|
453 |
|
return count($this->set); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return BufferInterface |
58
|
|
|
*/ |
59
|
24 |
|
public function bottom(): BufferInterface |
60
|
|
|
{ |
61
|
24 |
|
if (count($this->set) === 0) { |
62
|
|
|
throw new \RuntimeException('No bottom for empty collection'); |
63
|
|
|
} |
64
|
|
|
|
65
|
24 |
|
return $this->offsetGet(count($this) - 1); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return BufferInterface |
70
|
|
|
*/ |
71
|
|
|
public function top(): BufferInterface |
72
|
|
|
{ |
73
|
|
|
if (count($this->set) === 0) { |
74
|
|
|
throw new \RuntimeException('No top for empty collection'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $this->offsetGet(0); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return bool |
82
|
|
|
*/ |
83
|
44 |
|
public function isNull(): bool |
84
|
|
|
{ |
85
|
44 |
|
return count($this->set) === 0; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return void |
90
|
|
|
*/ |
91
|
374 |
|
public function rewind() |
92
|
|
|
{ |
93
|
374 |
|
$this->position = 0; |
94
|
374 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return BufferInterface |
98
|
|
|
*/ |
99
|
|
|
public function current(): BufferInterface |
100
|
|
|
{ |
101
|
|
|
return $this->set[$this->position]; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return int |
106
|
|
|
*/ |
107
|
|
|
public function key(): int |
108
|
|
|
{ |
109
|
|
|
return $this->position; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return void |
114
|
|
|
*/ |
115
|
338 |
|
public function next() |
116
|
|
|
{ |
117
|
338 |
|
++$this->position; |
118
|
338 |
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return bool |
122
|
|
|
*/ |
123
|
374 |
|
public function valid(): bool |
124
|
|
|
{ |
125
|
374 |
|
return isset($this->set[$this->position]); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param int $offset |
130
|
|
|
* @return bool |
131
|
|
|
*/ |
132
|
|
|
public function offsetExists($offset) |
133
|
|
|
{ |
134
|
|
|
return array_key_exists($offset, $this->set); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param int $offset |
139
|
|
|
*/ |
140
|
|
|
public function offsetUnset($offset) |
141
|
|
|
{ |
142
|
|
|
throw new \RuntimeException('Cannot unset from a Static Collection'); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param int $offset |
147
|
|
|
* @return mixed |
148
|
|
|
*/ |
149
|
|
|
public function offsetGet($offset) |
150
|
|
|
{ |
151
|
|
|
if (!array_key_exists($offset, $this->set)) { |
152
|
|
|
throw new \OutOfRangeException('Nothing found at this offset'); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $this->set[$offset]; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param int $offset |
160
|
|
|
* @param mixed $value |
161
|
|
|
*/ |
162
|
|
|
public function offsetSet($offset, $value) |
163
|
|
|
{ |
164
|
|
|
throw new \RuntimeException('Cannot add to a Static Collection'); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
This interface has been deprecated. The supplier of the interface has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the interface will be removed and what other interface to use instead.