1 | <?php |
||
7 | class Stack implements \Countable, \ArrayAccess, \Iterator |
||
8 | { |
||
9 | /** |
||
10 | * @var int |
||
11 | */ |
||
12 | private $position = 0; |
||
13 | |||
14 | /** |
||
15 | * @var BufferInterface[] |
||
16 | */ |
||
17 | private $values = []; |
||
18 | |||
19 | /** |
||
20 | * Stack constructor. |
||
21 | * @param BufferInterface[] $values |
||
22 | */ |
||
23 | public function __construct(array $values = []) |
||
24 | { |
||
25 | 1312 | $this->values = array_map(function (BufferInterface $value) { |
|
26 | 44 | return $value; |
|
27 | 1312 | }, $values); |
|
28 | 1312 | } |
|
29 | |||
30 | /** |
||
31 | * @return BufferInterface[] |
||
32 | */ |
||
33 | 68 | public function all() |
|
37 | |||
38 | /** |
||
39 | * @return BufferInterface |
||
40 | */ |
||
41 | 754 | public function current() |
|
42 | { |
||
43 | 754 | return $this->values[$this->position]; |
|
44 | } |
||
45 | |||
46 | 754 | public function next() |
|
47 | { |
||
48 | 754 | ++$this->position; |
|
49 | 754 | } |
|
50 | |||
51 | /** |
||
52 | * @return int |
||
53 | */ |
||
54 | public function key() |
||
55 | { |
||
56 | return $this->position; |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * @return bool |
||
61 | */ |
||
62 | 1277 | public function valid() |
|
66 | |||
67 | 1277 | public function rewind() |
|
68 | { |
||
69 | 1277 | $this->position = 0; |
|
71 | |||
72 | /** |
||
73 | * @return int |
||
74 | */ |
||
75 | 1281 | public function count() |
|
79 | |||
80 | /** |
||
81 | * @return bool |
||
82 | */ |
||
83 | 1042 | public function isEmpty() |
|
87 | |||
88 | /** |
||
89 | * @return BufferInterface |
||
90 | */ |
||
91 | 102 | public function bottom() |
|
100 | |||
101 | /** |
||
102 | * @see \ArrayAccess::offsetGet() |
||
103 | * @param int $offset |
||
104 | * @return \BitWasp\Buffertools\BufferInterface |
||
105 | */ |
||
106 | 1123 | public function offsetGet($offset) |
|
115 | |||
116 | /** |
||
117 | * @see \ArrayAccess::offsetSet() |
||
118 | * @param int $offset |
||
119 | * @param BufferInterface $value |
||
120 | * @throws \InvalidArgumentException |
||
121 | */ |
||
122 | 19 | public function offsetSet($offset, $value) |
|
139 | |||
140 | /** |
||
141 | * @see \ArrayAccess::offsetExists() |
||
142 | * @param int $offset |
||
143 | * @return bool |
||
144 | */ |
||
145 | 2 | public function offsetExists($offset) |
|
150 | |||
151 | /** |
||
152 | * @see \ArrayAccess::offsetUnset() |
||
153 | * @param int $offset |
||
154 | */ |
||
155 | 25 | public function offsetUnset($offset) |
|
165 | |||
166 | /** |
||
167 | * @param int $first |
||
168 | * @param int $second |
||
169 | */ |
||
170 | 19 | public function swap($first, $second) |
|
177 | |||
178 | /** |
||
179 | * @param int $offset |
||
180 | * @param BufferInterface $value |
||
181 | */ |
||
182 | 7 | public function add($offset, $value) |
|
183 | { |
||
184 | 7 | $size = count($this); |
|
185 | 7 | $index = $size + $offset; |
|
186 | 7 | if ($index > $size) { |
|
187 | throw new \RuntimeException('Invalid add position'); |
||
188 | } |
||
189 | |||
190 | // Unwind current values, push provided value, reapply popped values |
||
191 | 7 | $values = []; |
|
192 | 7 | for ($i = $size; $i > $index; $i--) { |
|
193 | 7 | $values[] = $this->pop(); |
|
194 | } |
||
195 | |||
196 | 7 | $this->push($value); |
|
197 | 7 | for ($i = count($values); $i > 0; $i--) { |
|
198 | 7 | $this->push(array_pop($values)); |
|
199 | } |
||
200 | 7 | } |
|
201 | |||
202 | 901 | public function pop() |
|
212 | |||
213 | 1216 | public function push($buffer) |
|
217 | |||
218 | /** |
||
219 | * @return int |
||
220 | */ |
||
221 | public function end() |
||
230 | |||
231 | /** |
||
232 | * @param int $length |
||
233 | * @return $this |
||
234 | */ |
||
235 | 40 | public function resize($length) |
|
247 | } |
||
248 |