Skip to content

Commit

Permalink
Allow groupBy to work with enums. (#17574)
Browse files Browse the repository at this point in the history
Allow groupBy() to work with enums.

Remove exception for unit enums for indexBy().
  • Loading branch information
dereuromark committed Feb 8, 2024
1 parent c57bbe5 commit cd13f9d
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion CollectionTrait.php
Expand Up @@ -292,6 +292,12 @@ public function groupBy(callable|string $path): CollectionInterface
'Use a callback to return a default value for that path.'
);
}
if ($pathValue instanceof BackedEnum) {
$pathValue = $pathValue->value;
} elseif ($pathValue instanceof UnitEnum) {
$pathValue = $pathValue->name;
}

$group[$pathValue][] = $value;
}

Expand All @@ -313,6 +319,12 @@ public function indexBy(callable|string $path): CollectionInterface
'Use a callback to return a default value for that path.'
);
}
if ($pathValue instanceof BackedEnum) {
$pathValue = $pathValue->value;
} elseif ($pathValue instanceof UnitEnum) {
$pathValue = $pathValue->name;
}

$group[$pathValue] = $value;
}

Expand Down Expand Up @@ -617,7 +629,7 @@ public function combine(
if ($mapKey instanceof BackedEnum) {
$mapKey = $mapKey->value;
} elseif ($mapKey instanceof UnitEnum) {
throw new InvalidArgumentException('Cannot index by path that is a non-backed enum.');
$mapKey = $mapKey->name;
}

$mapReduce->emit($rowVal($value, $key), $mapKey);
Expand Down

0 comments on commit cd13f9d

Please sign in to comment.