|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Collections\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Collections\Iterable; |
|
6
|
|
|
use Collections\VectorInterface; |
|
7
|
|
|
|
|
8
|
|
|
trait StrictIterableTrait |
|
9
|
|
|
{ |
|
10
|
|
|
use CommonMutableContainerTrait; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* {@inheritDoc} |
|
14
|
|
|
* @return $this |
|
15
|
|
|
*/ |
|
16
|
1 |
|
public function map(callable $callable) |
|
17
|
|
|
{ |
|
18
|
1 |
|
$res = new static(); |
|
19
|
1 |
|
foreach ($this as $v) { |
|
|
|
|
|
|
20
|
1 |
|
$res[] = $callable($v); |
|
21
|
1 |
|
} |
|
22
|
|
|
|
|
23
|
1 |
|
return $res; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* {@inheritDoc} |
|
28
|
|
|
* @return $this |
|
29
|
|
|
*/ |
|
30
|
|
|
public function mapWithKey($callback) |
|
31
|
|
|
{ |
|
32
|
|
|
$res = new static(); |
|
33
|
|
|
foreach ($this as $k => $v) { |
|
|
|
|
|
|
34
|
|
|
$res[$k] = $callback($k, $v); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
return $res; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* {@inheritDoc} |
|
42
|
|
|
* @return $this |
|
43
|
|
|
*/ |
|
44
|
4 |
View Code Duplication |
public function filter(callable $callable) |
|
|
|
|
|
|
45
|
|
|
{ |
|
46
|
2 |
|
$res = new static(); |
|
47
|
2 |
|
foreach ($this as $v) { |
|
|
|
|
|
|
48
|
2 |
|
if ($callable($v)) { |
|
49
|
2 |
|
$res[] = $v; |
|
50
|
2 |
|
} |
|
51
|
2 |
|
} |
|
52
|
|
|
|
|
53
|
4 |
|
return $res; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* {@inheritDoc} |
|
58
|
|
|
* @return $this |
|
59
|
|
|
*/ |
|
60
|
|
View Code Duplication |
public function filterWithKey($callback) |
|
|
|
|
|
|
61
|
|
|
{ |
|
62
|
|
|
$res = new static(); |
|
63
|
|
|
foreach ($this as $k => $v) { |
|
|
|
|
|
|
64
|
|
|
if ($callback($k, $v)) { |
|
65
|
|
|
$res[$k] = $v; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return $res; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* {@inheritDoc} |
|
74
|
|
|
* @return $this |
|
75
|
|
|
*/ |
|
76
|
|
View Code Duplication |
public function zip(Iterable $iterable) |
|
|
|
|
|
|
77
|
|
|
{ |
|
78
|
|
|
$res = new static(); |
|
79
|
|
|
$it = $iterable->getIterator(); |
|
80
|
|
|
foreach ($this as $v) { |
|
|
|
|
|
|
81
|
|
|
if (!$it->valid()) { |
|
82
|
|
|
break; |
|
83
|
|
|
} |
|
84
|
|
|
$res[] = new Pair($v, $it->current()); |
|
85
|
|
|
$it->next(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return $res; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* {@inheritDoc} |
|
93
|
|
|
* @return $this |
|
94
|
|
|
*/ |
|
95
|
1 |
|
public function take($size = 1) |
|
96
|
|
|
{ |
|
97
|
1 |
|
$res = new static(); |
|
98
|
|
|
|
|
99
|
1 |
|
if ($size <= 0) { |
|
100
|
|
|
return $res; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
1 |
|
foreach ($this as $v) { |
|
|
|
|
|
|
104
|
1 |
|
$res[] = $v; |
|
105
|
1 |
|
if (--$size === 0) { |
|
106
|
1 |
|
break; |
|
107
|
|
|
} |
|
108
|
1 |
|
} |
|
109
|
|
|
|
|
110
|
1 |
|
return $res; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
View Code Duplication |
public function takeWhile(callable $callable) |
|
|
|
|
|
|
114
|
|
|
{ |
|
115
|
|
|
$res = new static(); |
|
116
|
|
|
foreach ($this as $v) { |
|
|
|
|
|
|
117
|
|
|
if (!$callable($v)) { |
|
118
|
|
|
break; |
|
119
|
|
|
} |
|
120
|
|
|
$res[] = $v; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return $res; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public function skip($n) |
|
127
|
|
|
{ |
|
128
|
|
|
$res = new static(); |
|
129
|
|
|
foreach ($this as $v) { |
|
|
|
|
|
|
130
|
|
|
if ($n <= 0) { |
|
131
|
|
|
$res[] = $v; |
|
132
|
|
|
} else { |
|
133
|
|
|
--$n; |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
return $res; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
public function skipWhile(callable $callable) |
|
141
|
|
|
{ |
|
142
|
|
|
$res = new static(); |
|
143
|
|
|
$skip = true; |
|
144
|
|
|
foreach ($this as $v) { |
|
|
|
|
|
|
145
|
|
|
if ($skip) { |
|
146
|
|
|
if ($callable($v)) { |
|
147
|
|
|
continue; |
|
148
|
|
|
} |
|
149
|
|
|
$skip = false; |
|
150
|
|
|
} |
|
151
|
|
|
$res[] = $v; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
return $res; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
View Code Duplication |
public function slice($start, $length) |
|
|
|
|
|
|
158
|
|
|
{ |
|
159
|
|
|
$res = new static(); |
|
160
|
|
|
if ($length <= 0) { |
|
161
|
|
|
return $res; |
|
162
|
|
|
} |
|
163
|
|
|
foreach ($this as $v) { |
|
|
|
|
|
|
164
|
|
|
if ($start !== 0) { |
|
165
|
|
|
--$start; |
|
166
|
|
|
continue; |
|
167
|
|
|
} |
|
168
|
|
|
$res[] = $v; |
|
169
|
|
|
if (--$length === 0) { |
|
170
|
|
|
break; |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
return $res; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
1 |
|
public function concat(\Traversable $iterable) |
|
178
|
|
|
{ |
|
179
|
1 |
|
$res = []; |
|
180
|
|
|
|
|
181
|
1 |
|
foreach ($this as $v) { |
|
|
|
|
|
|
182
|
1 |
|
$res[] = $v; |
|
183
|
1 |
|
} |
|
184
|
|
|
|
|
185
|
1 |
|
foreach ($iterable as $v) { |
|
186
|
1 |
|
$res[] = $v; |
|
187
|
1 |
|
} |
|
188
|
1 |
|
$this->container = $res; |
|
|
|
|
|
|
189
|
|
|
|
|
190
|
1 |
|
return $this; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* {@inheritDoc} |
|
195
|
|
|
* @return $this |
|
196
|
|
|
*/ |
|
197
|
2 |
|
public function first() |
|
198
|
|
|
{ |
|
199
|
2 |
|
foreach ($this as $v) { |
|
|
|
|
|
|
200
|
1 |
|
return $v; |
|
201
|
1 |
|
} |
|
202
|
|
|
|
|
203
|
1 |
|
return null; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* {@inheritDoc} |
|
208
|
|
|
* @return $this |
|
209
|
|
|
*/ |
|
210
|
2 |
|
public function last() |
|
211
|
|
|
{ |
|
212
|
2 |
|
$result = $this->toArray(); |
|
213
|
|
|
|
|
214
|
2 |
|
return array_pop($result); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* {@inheritDoc} |
|
219
|
|
|
* @return $this |
|
220
|
|
|
*/ |
|
221
|
|
|
public function each(callable $callable) |
|
222
|
|
|
{ |
|
223
|
|
|
foreach ($this as $v) { |
|
|
|
|
|
|
224
|
|
|
$callable($v); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
return $this; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* {@inheritdoc} |
|
232
|
|
|
*/ |
|
233
|
1 |
|
public function exists(callable $fn) |
|
234
|
|
|
{ |
|
235
|
1 |
|
foreach ($this as $element) { |
|
|
|
|
|
|
236
|
1 |
|
if ($fn($element)) { |
|
237
|
1 |
|
return true; |
|
238
|
|
|
} |
|
239
|
1 |
|
} |
|
240
|
|
|
|
|
241
|
1 |
|
return false; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
View Code Duplication |
public function concatAll() |
|
|
|
|
|
|
245
|
|
|
{ |
|
246
|
|
|
/** @var VectorInterface $results */ |
|
247
|
|
|
$results = new static(); |
|
248
|
|
|
$this->each(function (Iterable $subArray) use ($results) { |
|
249
|
|
|
$subArray->each(function ($item) use ($results) { |
|
250
|
|
|
$results->add($item); |
|
251
|
|
|
}); |
|
252
|
|
|
}); |
|
253
|
|
|
|
|
254
|
|
|
return $results; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* {@inheritdoc} |
|
259
|
|
|
*/ |
|
260
|
|
|
public function reduce(callable $callback, $initial = null) |
|
261
|
|
|
{ |
|
262
|
|
|
return array_reduce($this->toArray(), $callback, $initial); |
|
263
|
|
|
} |
|
264
|
|
|
} |
|
265
|
|
|
|