@@ 279-309 (lines=31) @@ | ||
276 | /** |
|
277 | * {@inheritdoc} |
|
278 | */ |
|
279 | public function groupBy(\Closure $discriminator): MapInterface |
|
280 | { |
|
281 | if ($this->size() === 0) { |
|
282 | throw new GroupEmptyMapException; |
|
283 | } |
|
284 | ||
285 | $map = null; |
|
286 | ||
287 | foreach ($this->pairs as $pair) { |
|
288 | $key = $discriminator($pair->key(), $pair->value()); |
|
289 | ||
290 | if ($map === null) { |
|
291 | $type = gettype($key); |
|
292 | $map = new self( |
|
293 | $type === 'object' ? get_class($key) : $type, |
|
294 | SequenceInterface::class |
|
295 | ); |
|
296 | } |
|
297 | ||
298 | if ($map->contains($key)) { |
|
299 | $map = $map->put( |
|
300 | $key, |
|
301 | $map->get($key)->add($pair) |
|
302 | ); |
|
303 | } else { |
|
304 | $map = $map->put($key, new Sequence($pair)); |
|
305 | } |
|
306 | } |
|
307 | ||
308 | return $map; |
|
309 | } |
|
310 | ||
311 | /** |
|
312 | * {@inheritdoc} |
@@ 213-243 (lines=31) @@ | ||
210 | /** |
|
211 | * {@inheritdoc} |
|
212 | */ |
|
213 | public function groupBy(\Closure $discriminator): MapInterface |
|
214 | { |
|
215 | if ($this->size() === 0) { |
|
216 | throw new GroupEmptySequenceException; |
|
217 | } |
|
218 | ||
219 | $map = null; |
|
220 | ||
221 | foreach ($this->values as $value) { |
|
222 | $key = $discriminator($value); |
|
223 | ||
224 | if ($map === null) { |
|
225 | $type = gettype($key); |
|
226 | $map = new Map( |
|
227 | $type === 'object' ? get_class($key) : $type, |
|
228 | SequenceInterface::class |
|
229 | ); |
|
230 | } |
|
231 | ||
232 | if ($map->contains($key)) { |
|
233 | $map = $map->put( |
|
234 | $key, |
|
235 | $map->get($key)->add($value) |
|
236 | ); |
|
237 | } else { |
|
238 | $map = $map->put($key, new self($value)); |
|
239 | } |
|
240 | } |
|
241 | ||
242 | return $map; |
|
243 | } |
|
244 | ||
245 | /** |
|
246 | * {@inheritdoc} |