Skip to content

Core\Reflect\EnumInfo::cases

$enumInfo->cases(): array<string>

Returns the name of every case an enum declares, as a list of strings.

You get the description of an enum with Core\Reflect\EnumInfo::of. cases() then returns one string for each case. The list is sorted by the value of each case, from the smallest to the largest. It is not sorted in the order of the declaration. Two cases can have the same value, and then the one whose name comes first in the alphabet is listed first. An enum with no case gives an empty list.

Good to know: every name in this list works with valueOf, which returns the value of the case.

The examples below print the cases of an enum, show two cases with the same value, and check a filter from a web address against the list of cases.

Return value

Returns array<string>. One string per case, sorted by the value of the case from the smallest to the largest. Two cases with the same value are sorted by name. The order is not the order of the declaration. An enum with no case gives an empty list.

Examples

Print every case

<?nvs
// Shows how to print every case of an enum by name.
enum Priority: int
{
High = 30,
Low = 10,
Normal = 20,
}
// `Core\Reflect\EnumInfo::of` returns `null` for a name that is not an enum.
// `Priority` is an enum, so this check passes.
?Core\Reflect\EnumInfo $found = Core\Reflect\EnumInfo::of(Priority::class);
if ($found != null) {
Core\Reflect\EnumInfo $info = $found;
// The cases are sorted by value, so this prints Low, Normal and High.
foreach ($info->cases() as string $case) {
echo $case, "\n";
}
}
Output
Low
Normal
High

Two cases with one value

<?nvs
// Shows what `cases()` returns when two cases have the same value.
// A case with no value is the case before it plus one. So `Small` is 0, `Medium` is 1
// and `Large` is 2. `Default` is 1 too, so it is a second name for `Medium`.
enum Size: int
{
Small,
Medium,
Large,
Default = 1,
}
?Core\Reflect\EnumInfo $found = Core\Reflect\EnumInfo::of(Size::class);
if ($found != null) {
Core\Reflect\EnumInfo $info = $found;
// Both names are in the list. `Default` and `Medium` have the same value,
// so they are sorted by name. This prints `Small, Default, Medium, Large`.
echo Core\Str::join($info->cases(), ', '), "\n";
// This prints 4.
echo Core\Arr::count($info->cases()), "\n";
}
Output
Small, Default, Medium, Large
4

Check a filter from a url

<?nvs
// Checks the value of a `?status=` filter from a web address against the cases of an enum.
enum OrderStatus: int
{
Open = 1,
Paid = 2,
Shipped = 3,
Cancelled = 9,
}
// These are values a visitor could type after `?status=`.
array<string> $filters = ['Shipped', 'Lost', 'open'];
?Core\Reflect\EnumInfo $found = Core\Reflect\EnumInfo::of(OrderStatus::class);
if ($found != null) {
Core\Reflect\EnumInfo $info = $found;
array<string> $allowed = $info->cases();
foreach ($filters as string $filter) {
// The check is exact, so "open" is not the same as "Open".
if (Core\Arr::contains($allowed, $filter)) {
echo 'Filter by ', $filter, "\n";
} else {
// The error message lists every case, so the visitor knows what to type.
echo "Unknown status '", $filter, "'. Use one of: ", Core\Str::join($allowed, ', '), "\n";
}
}
}
Output
Filter by Shipped
Unknown status 'Lost'. Use one of: Open, Paid, Shipped, Cancelled
Unknown status 'open'. Use one of: Open, Paid, Shipped, Cancelled