| @@ 216-245 (lines=30) @@ | ||
| 213 | /** |
|
| 214 | * {@inheritdoc} |
|
| 215 | */ |
|
| 216 | public function groupBy(callable $discriminator): MapInterface |
|
| 217 | { |
|
| 218 | if ($this->size() === 0) { |
|
| 219 | throw new GroupEmptySequenceException; |
|
| 220 | } |
|
| 221 | ||
| 222 | $map = null; |
|
| 223 | ||
| 224 | foreach ($this->values as $value) { |
|
| 225 | $key = $discriminator($value); |
|
| 226 | ||
| 227 | if ($map === null) { |
|
| 228 | $map = new Map( |
|
| 229 | $this->determineType($key), |
|
| 230 | SequenceInterface::class |
|
| 231 | ); |
|
| 232 | } |
|
| 233 | ||
| 234 | if ($map->contains($key)) { |
|
| 235 | $map = $map->put( |
|
| 236 | $key, |
|
| 237 | $map->get($key)->add($value) |
|
| 238 | ); |
|
| 239 | } else { |
|
| 240 | $map = $map->put($key, new self($value)); |
|
| 241 | } |
|
| 242 | } |
|
| 243 | ||
| 244 | return $map; |
|
| 245 | } |
|
| 246 | ||
| 247 | /** |
|
| 248 | * {@inheritdoc} |
|
| @@ 226-258 (lines=33) @@ | ||
| 223 | /** |
|
| 224 | * {@inheritdoc} |
|
| 225 | */ |
|
| 226 | public function groupBy(callable $discriminator): MapInterface |
|
| 227 | { |
|
| 228 | if ($this->size() === 0) { |
|
| 229 | throw new GroupEmptySequenceException; |
|
| 230 | } |
|
| 231 | ||
| 232 | $map = null; |
|
| 233 | ||
| 234 | foreach ($this->values as $value) { |
|
| 235 | $key = $discriminator($value); |
|
| 236 | ||
| 237 | if ($map === null) { |
|
| 238 | $map = new Map( |
|
| 239 | $this->determineType($key), |
|
| 240 | StreamInterface::class |
|
| 241 | ); |
|
| 242 | } |
|
| 243 | ||
| 244 | if ($map->contains($key)) { |
|
| 245 | $map = $map->put( |
|
| 246 | $key, |
|
| 247 | $map->get($key)->add($value) |
|
| 248 | ); |
|
| 249 | } else { |
|
| 250 | $map = $map->put( |
|
| 251 | $key, |
|
| 252 | (new self((string) $this->type))->add($value) |
|
| 253 | ); |
|
| 254 | } |
|
| 255 | } |
|
| 256 | ||
| 257 | return $map; |
|
| 258 | } |
|
| 259 | ||
| 260 | /** |
|
| 261 | * {@inheritdoc} |
|