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