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