1
|
|
|
<?php namespace Mbh\Collection\Traits; |
2
|
|
|
|
3
|
|
|
use Mbh\Collection\Interfaces\Collection as CollectionInterface; |
4
|
|
|
use Mbh\Collection\Interfaces\Sequenceable as SequenceableInterface; |
5
|
|
|
use Mbh\Collection\FixedArray; |
6
|
|
|
use Mbh\Collection\CallbackHeap; |
7
|
|
|
use Mbh\Iterator\SliceIterator; |
8
|
|
|
use Mbh\Iterator\ConcatIterator; |
9
|
|
|
use SplFixedArray; |
10
|
|
|
use SplHeap; |
11
|
|
|
use SplStack; |
12
|
|
|
use LimitIterator; |
13
|
|
|
use Iterator; |
14
|
|
|
use ArrayAccess; |
15
|
|
|
use Countable; |
16
|
|
|
use CallbackFilterIterator; |
17
|
|
|
use JsonSerializable; |
18
|
|
|
use RuntimeException; |
19
|
|
|
use Traversable; |
20
|
|
|
use ReflectionClass; |
21
|
|
|
use UnderflowException; |
22
|
|
|
use OutOfRangeException; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* MBHFramework |
26
|
|
|
* |
27
|
|
|
* @link https://github.com/MBHFramework/mbh-framework |
28
|
|
|
* @copyright Copyright (c) 2017 Ulises Jeremias Cornejo Fandos |
29
|
|
|
* @license https://github.com/MBHFramework/mbh-framework/blob/master/LICENSE (MIT License) |
30
|
|
|
*/ |
31
|
|
|
|
32
|
|
|
trait Sequenceable |
33
|
|
|
{ |
34
|
|
|
use Collection; |
35
|
|
|
use Functional; |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
protected $sfa = null; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Create an fixed array |
42
|
|
|
* |
43
|
|
|
* @param Traversable $array data |
44
|
|
|
*/ |
45
|
|
|
protected function __construct(Traversable $array) |
46
|
|
|
{ |
47
|
|
|
$this->sfa = $array; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function toArray(): array |
51
|
|
|
{ |
52
|
|
|
return $this->sfa->toArray(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
protected function validIndex(int $index) |
56
|
|
|
{ |
57
|
|
|
return $index >= 0 && $index < count($this); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Countable |
62
|
|
|
*/ |
63
|
|
|
public function count(): int |
64
|
|
|
{ |
65
|
|
|
return count($this->sfa); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Iterator |
70
|
|
|
*/ |
71
|
|
|
public function current() |
72
|
|
|
{ |
73
|
|
|
return $this->sfa->current(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function key(): int |
77
|
|
|
{ |
78
|
|
|
return $this->sfa->key(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function next() |
82
|
|
|
{ |
83
|
|
|
return $this->sfa->next(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function rewind() |
87
|
|
|
{ |
88
|
|
|
return $this->sfa->rewind(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function valid() |
92
|
|
|
{ |
93
|
|
|
return $this->sfa->valid(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* ArrayAccess |
98
|
|
|
*/ |
99
|
|
|
public function offsetExists($offset): bool |
100
|
|
|
{ |
101
|
|
|
return is_integer($offset) |
102
|
|
|
&& $this->validIndex($offset) |
103
|
|
|
&& $this->sfa->offsetExists($offset); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function offsetGet($offset) |
107
|
|
|
{ |
108
|
|
|
return $this->sfa->offsetGet($offset); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function offsetSet($offset, $value) |
112
|
|
|
{ |
113
|
|
|
return is_integer($offset) |
114
|
|
|
&& $this->validIndex($offset) |
115
|
|
|
&& $this->sfa->offsetSet($offset, $value); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function offsetUnset($offset) |
119
|
|
|
{ |
120
|
|
|
return is_integer($offset) |
121
|
|
|
&& $this->validIndex($offset) |
122
|
|
|
&& $this->sfa->offsetUnset($offset); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function clear() |
126
|
|
|
{ |
127
|
|
|
return $this->sfa->clear(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
protected function getMainTraversable(): Traversable |
131
|
|
|
{ |
132
|
|
|
return $this->sfa; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
protected function setTraversable(Traversable $traversable) |
136
|
|
|
{ |
137
|
|
|
$this->sfa = $traversable; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @inheritDoc |
142
|
|
|
*/ |
143
|
|
|
public static function fromItems(Traversable $array) |
144
|
|
|
{ |
145
|
|
|
// We can only do it this way if we can count it |
146
|
|
|
if ($array instanceof Countable) { |
147
|
|
|
$sfa = new SplFixedArray(count($array)); |
148
|
|
|
|
149
|
|
|
foreach ($array as $i => $elem) { |
150
|
|
|
$sfa[$i] = $elem; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return new static($sfa); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
// If we can't count it, it's simplest to iterate into an array first |
157
|
|
|
return static::fromArray(iterator_to_array($array)); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @inheritDoc |
162
|
|
|
*/ |
163
|
|
|
public static function fromArray(array $array) |
164
|
|
|
{ |
165
|
|
|
return new static(SplFixedArray::fromArray($array)); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @inheritDoc |
170
|
|
|
*/ |
171
|
|
|
public function copy() |
172
|
|
|
{ |
173
|
|
|
return static::fromArray($this->toArray()); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @inheritDoc |
178
|
|
|
*/ |
179
|
|
|
public function contains(...$values): bool |
180
|
|
|
{ |
181
|
|
|
foreach ($values as $value) { |
182
|
|
|
if (!$this->find($value)) { |
183
|
|
|
return false; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return true; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @inheritDoc |
192
|
|
|
*/ |
193
|
|
|
public function first() |
194
|
|
|
{ |
195
|
|
|
if ($this->isEmpty()) { |
196
|
|
|
throw new UnderflowException(); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
return $this[0]; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @inheritDoc |
204
|
|
|
*/ |
205
|
|
|
public function get(int $index) |
206
|
|
|
{ |
207
|
|
|
if (! $this->validIndex($index)) { |
208
|
|
|
throw new OutOfRangeException(); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
return $this[$index]; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @inheritDoc |
216
|
|
|
*/ |
217
|
|
|
public function insert(int $index, ...$values) |
|
|
|
|
218
|
|
|
{ |
219
|
|
|
if (! $this->validIndex($index) && $index !== count($this)) { |
|
|
|
|
220
|
|
|
throw new OutOfRangeException(); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @inheritDoc |
226
|
|
|
*/ |
227
|
|
|
public function last() |
228
|
|
|
{ |
229
|
|
|
if ($this->isEmpty()) { |
230
|
|
|
throw new UnderflowException(); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
return $this[count($this) - 1]; |
|
|
|
|
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Pushes all values of either an array or traversable object. |
238
|
|
|
*/ |
239
|
|
|
private function pushAll($values) |
240
|
|
|
{ |
241
|
|
|
foreach ($values as $value) { |
242
|
|
|
$this[] = $value; |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @inheritDoc |
248
|
|
|
*/ |
249
|
|
|
public function push(...$values) |
250
|
|
|
{ |
251
|
|
|
$this->pushAll($values); |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|