Core\Reflect\EnumInfo::valueOf
Returns the value of one case of an enum, found by the name of the case.
Every case of an enum has a whole number as its value. You get the description of the enum with Core\Reflect\EnumInfo::of, and then valueOf("Paid") returns the value of the case Paid. The name is case-sensitive. The result is a uint for an enum declared with : uint, and an int for every other enum. isUnsigned() says which one you get. If the enum has no case with that name, valueOf throws a LogicError.
Good to know: cases() returns every name that valueOf accepts. Check a name against that list first when it comes from a user.
The examples below print the value of each case, catch the error for a name that does not exist, and turn a choice from a form into the number saved in a database.
Parameters
$casestringThe name of the case, ascasesgives it. The name is case-sensitive. The text cannot betainted. Check text from a request withCore\Taint::assertTrustedfirst.
Return value
Returns int|uint. The value of the case. It is a uint if isUnsigned returns true, and an int otherwise. Use as uint or as int to store it in a variable of that type.
Errors
LogicErrorThe enum has no case with that name. Usecasesto get the names that exist.
Examples
Read the value of a case
<?nvs// Shows how to read the value of each case of an enum.
enum HttpStatus: int{ Ok = 200, NotFound = 404, ServerError = 500,}
?Core\Reflect\EnumInfo $found = Core\Reflect\EnumInfo::of(HttpStatus::class);if ($found != null) { Core\Reflect\EnumInfo $info = $found; // This prints `Ok = 200`, `NotFound = 404` and `ServerError = 500`. foreach ($info->cases() as string $case) { echo $case, ' = ', $info->valueOf($case), "\n"; }}Ok = 200NotFound = 404ServerError = 500A case that does not exist
<?nvs// Shows the error `valueOf` throws for a case name the enum does not have.
enum Plan: int{ Free = 0, Pro = 1,}
?Core\Reflect\EnumInfo $found = Core\Reflect\EnumInfo::of(Plan::class);if ($found != null) { Core\Reflect\EnumInfo $info = $found; // Case names are case-sensitive, so "pro" is not the case `Pro`. try { echo $info->valueOf('pro'), "\n"; } catch (LogicError $error) { // This prints the message of the error, which names the enum and the case. echo $error->message, "\n"; }}enum `Plan` declares no case `pro`Save a choice from a form
<?nvs// Turns the case name a form sends into the number that is saved in a database column.
enum Rating: int{ Poor = 1, Good = 3, Great = 5,}
class RatingField{ // `cases()` is checked first, so `valueOf` never gets a name that does not exist. public static function toColumn(string $sent): ?int { ?Core\Reflect\EnumInfo $found = Core\Reflect\EnumInfo::of(Rating::class); if ($found == null) { return null; } Core\Reflect\EnumInfo $info = $found; if (!Core\Arr::contains($info->cases(), $sent)) { return null; } // `valueOf` returns `int|uint`. `Rating` is an `int` enum, so `as int` is safe here. return $info->valueOf($sent) as int; }}
// These are values a visitor could send. This prints `3`, then `invalid` twice.array<string> $sent = ['Good', 'Excellent', ''];foreach ($sent as string $value) { ?int $column = RatingField::toColumn($value); echo $column == null ? 'invalid' : $column, "\n";}3invalidinvalid