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
|
|
|
public function take($size = 1) |
68
|
|
|
{ |
69
|
|
|
$res = new static(); |
70
|
|
|
if ($size <= 0) { |
71
|
|
|
return $res; |
72
|
|
|
} |
73
|
|
|
foreach ($this as $k => $v) { |
|
|
|
|
74
|
|
|
$res[$k] = $v; |
75
|
|
|
if (--$size === 0) { |
76
|
|
|
break; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $res; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
View Code Duplication |
public function takeWhile($fn) |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
$res = new static(); |
86
|
|
|
foreach ($this as $k => $v) { |
|
|
|
|
87
|
|
|
if (!$fn($v)) { |
88
|
|
|
break; |
89
|
|
|
} |
90
|
|
|
$res[$k] = $v; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $res; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
View Code Duplication |
public function skip($n) |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
$res = new static(); |
99
|
|
|
foreach ($this as $k => $v) { |
|
|
|
|
100
|
|
|
if ($n <= 0) { |
101
|
|
|
$res[$k] = $v; |
102
|
|
|
} else { |
103
|
|
|
--$n; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $res; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function skipWhile($fn) |
111
|
|
|
{ |
112
|
|
|
$res = new static(); |
113
|
|
|
$skip = true; |
114
|
|
|
foreach ($this as $k => $v) { |
|
|
|
|
115
|
|
|
if ($skip) { |
116
|
|
|
if ($fn($v)) { |
117
|
|
|
continue; |
118
|
|
|
} |
119
|
|
|
$skip = false; |
120
|
|
|
} |
121
|
|
|
$res[$k] = $v; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $res; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
View Code Duplication |
public function slice($start, $lenght) |
|
|
|
|
128
|
|
|
{ |
129
|
|
|
$res = new static(); |
130
|
|
|
if ($lenght <= 0) { |
131
|
|
|
return $res; |
132
|
|
|
} |
133
|
|
|
foreach ($this as $k => $v) { |
|
|
|
|
134
|
|
|
if ($start !== 0) { |
135
|
|
|
--$start; |
136
|
|
|
continue; |
137
|
|
|
} |
138
|
|
|
$res[$k] = $v; |
139
|
|
|
if (--$lenght === 0) { |
140
|
|
|
break; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $res; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function first() |
148
|
|
|
{ |
149
|
|
|
foreach ($this as $v) { |
|
|
|
|
150
|
|
|
return $v; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return null; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function firstKey() |
157
|
|
|
{ |
158
|
|
|
foreach ($this as $k => $_) { |
|
|
|
|
159
|
|
|
return $k; |
160
|
|
|
} |
161
|
|
|
|
162
|
1 |
|
return null; |
163
|
|
|
} |
164
|
1 |
|
|
165
|
|
|
public function last() |
166
|
1 |
|
{ |
167
|
1 |
|
$v = null; |
168
|
1 |
|
foreach ($this as $v) { |
|
|
|
|
169
|
|
|
} |
170
|
1 |
|
|
171
|
1 |
|
return $v; |
172
|
1 |
|
} |
173
|
1 |
|
|
174
|
|
|
public function lastKey() |
175
|
1 |
|
{ |
176
|
|
|
$k = null; |
177
|
|
|
foreach ($this as $k => $_) { |
|
|
|
|
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return $k; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* {@inheritDoc} |
185
|
|
|
* @return $this |
186
|
|
|
*/ |
187
|
|
|
public function each(callable $callable) |
188
|
|
|
{ |
189
|
|
|
foreach ($this as $k => $v) { |
|
|
|
|
190
|
|
|
$callable($v, $k); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
return $this; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* {@inheritdoc} |
198
|
|
|
*/ |
199
|
|
|
public function exists(callable $fn) |
200
|
|
|
{ |
201
|
|
|
foreach ($this as $key => $element) { |
|
|
|
|
202
|
|
|
if ($fn($key, $element)) { |
203
|
|
|
return true; |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
return false; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
View Code Duplication |
public function concatAll() |
|
|
|
|
211
|
|
|
{ |
212
|
|
|
/** @var MapInterface $results */ |
213
|
|
|
$results = new static(); |
214
|
|
|
$this->each(function (Iterable $subArray) use ($results) { |
215
|
|
|
$subArray->each(function ($item, $key) use ($results) { |
216
|
|
|
$results->add($key, $item); |
217
|
|
|
}); |
218
|
|
|
}); |
219
|
|
|
|
220
|
|
|
return $results; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
View Code Duplication |
protected function concatRecurse($array, $array1) |
|
|
|
|
224
|
|
|
{ |
225
|
|
|
$merged = $array; |
226
|
|
|
|
227
|
|
|
foreach ($array1 as $key => $value) { |
228
|
|
|
$isValid = function ($value) { |
229
|
|
|
return (is_array($value) || $value instanceof \Traversable); |
230
|
|
|
}; |
231
|
|
|
|
232
|
|
|
if (($isValid($value) && isset($merged[$key])) && $isValid($merged[$key])) { |
233
|
|
|
$merged[$key] = $this->concatRecurse($merged[$key], $value); |
234
|
|
|
} else { |
235
|
|
|
$merged[$key] = $value; |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
return $merged; |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|