Core\Reflect\EnumInfo::of
Core\Reflect\EnumInfo::of(string $name): ?Core\Reflect\EnumInfoCore\Reflect\EnumInfo::of( string $name, ): ?Core\Reflect\EnumInfo
Returns a description of an enum, found by the name of the enum.
An enum has no methods of its own. To read the list of its cases, or the value of one case, you first get its description with Core\Reflect\EnumInfo::of. You pass the full name of the enum, and Status::class gives that name. The result is a Core\Reflect\EnumInfo, or null if the program has no enum with that name. The name of a class also gives null, because a class is not an enum. The type of the result is ?Core\Reflect\EnumInfo, so check it for null before you use it.
Good to know: of only needs a string. One function can work with every enum in a program.
The examples below describe an enum by its name, show the names that give null, and build the choices of a form from any enum.
Parameters
$namestringThe full name of the enum, with its namespace and with no\at the start.Status::classgives this name. The text cannot betainted. Check text from a request withCore\Taint::assertTrustedfirst.
Return value
Returns ?Core\Reflect\EnumInfo. A Core\Reflect\EnumInfo for that enum. The result is null if the program has no enum with that name, and also for the name of a class.
Changelog
| Version | Change |
|---|---|
0.0.6 | The server now builds the description of each class once, when it loads your program. Core\Reflect::forClass, Core\Reflect::forObject and Core\Reflect\EnumInfo::of return that description, so two calls for the same class return the same object. A list such as properties() or methods() is built once per request. |
Examples
Describe an enum by its name
<?nvs// Shows how to get the description of an enum from its name.
enum Size: int{ Small = 1, Medium = 2, Large = 3,}
// `Size::class` is the name of the enum, the string "Size".?Core\Reflect\EnumInfo $found = Core\Reflect\EnumInfo::of(Size::class);// The result is `null` when there is no enum with that name, so check it first.if ($found != null) { Core\Reflect\EnumInfo $info = $found; // This prints `Size has 3 cases`. echo $info->name(), ' has ', Core\Arr::count($info->cases()), " cases\n";}Size has 3 casesA name that is not an enum
<?nvs// Shows that `of` returns `null` for a name that is not the name of an enum.
enum Color: int{ Red = 1, Green = 2,}
class Invoice{ public int $number = 0;}
// `Invoice` is a class, and `Colour` is spelled wrong. Only `Color` is an enum.array<string> $names = [Color::class, Invoice::class, 'Colour'];foreach ($names as string $name) { if (Core\Reflect\EnumInfo::of($name) == null) { echo $name, ": not an enum\n"; } else { echo $name, ": enum\n"; }}Color: enumInvoice: not an enumColour: not an enumBuild a select list for a form
<?nvs// Builds the choices of a form's select list from any enum, given only its name.
enum Shipping: int{ Standard = 1, Express = 2, Pickup = 3,}
enum Payment: int{ Card = 1, Invoice = 2,}
class SelectList{ // One function works for every enum, because it only needs the name of the enum. public static function html(string $field, string $enum): string { ?Core\Reflect\EnumInfo $found = Core\Reflect\EnumInfo::of($enum); if ($found == null) { return '<!-- ' . $enum . ' is not an enum -->'; } Core\Reflect\EnumInfo $info = $found; string $html = "<select name=\"" . $field . "\">"; foreach ($info->cases() as string $case) { $html = $html . '<option>' . $case . '</option>'; } return $html . '</select>'; }}
// This prints one `<select>` with three options and one with two.echo SelectList::html('shipping', Shipping::class), "\n";echo SelectList::html('payment', Payment::class), "\n";<select name="shipping"><option>Standard</option><option>Express</option><option>Pickup</option></select><select name="payment"><option>Card</option><option>Invoice</option></select>