Core\Reflect\EnumInfo::isUnsigned
$enumInfo->isUnsigned(): boolChecks whether the values of an enum's cases are uint or int numbers.
Every case of an enum has a whole number as its value. The enum declaration chooses the type of that number: : uint for numbers that are never negative, or : int for numbers that can be. An enum written with no type is an int enum. isUnsigned() returns true for a uint enum and false for an int enum. An enum with no case still has its type.
Good to know: valueOf returns int|uint. Check isUnsigned() first to know which of the two you get.
Return value
Returns bool. true for an enum declared with : uint. false for an enum declared with : int or with no type, because an enum with no type is an int enum.
Examples
Print the type of two enums
<?nvs// Shows how to check whether an enum has `uint` or `int` values.
enum Permission: uint{ Read = 1, Write = 2, Delete = 4,}
enum Temperature: int{ Freezing = -10, Mild = 15,}
array<string> $names = [Permission::class, Temperature::class];foreach ($names as string $name) { ?Core\Reflect\EnumInfo $found = Core\Reflect\EnumInfo::of($name); if ($found != null) { Core\Reflect\EnumInfo $info = $found; // This prints `Permission: uint` and then `Temperature: int`. echo $name, ': ', $info->isUnsigned() ? 'uint' : 'int', "\n"; }}Permission: uintTemperature: intAn enum with no type
<?nvs// Shows that an enum declared with no type has `int` values.
// This enum has no `: int` or `: uint` after its name.enum Weekday{ Monday = 1, Tuesday, Wednesday,}
?Core\Reflect\EnumInfo $found = Core\Reflect\EnumInfo::of(Weekday::class);if ($found != null) { Core\Reflect\EnumInfo $info = $found; // An enum with no type is an `int` enum, so this prints `false`. echo $info->isUnsigned() ? 'true' : 'false', "\n";}falseRead a value with the right type
<?nvs// Reads the value of a case from any enum and stores it with the right number type.
enum Flag: uint{ Hidden = 1, Pinned = 2,}
enum Offset: int{ Before = -1, After = 1,}
class CaseValue{ // `valueOf` returns `int|uint`. `isUnsigned()` says which one it is, // so the value can be converted with `as uint` or `as int`. public static function describe(string $enum, string $case): string { ?Core\Reflect\EnumInfo $found = Core\Reflect\EnumInfo::of($enum); if ($found == null) { return $enum . ' is not an enum'; } Core\Reflect\EnumInfo $info = $found; if ($info->isUnsigned()) { uint $bits = $info->valueOf($case) as uint; return $case . ' is the uint ' . $bits; } int $number = $info->valueOf($case) as int; return $case . ' is the int ' . $number; }}
// This prints `Pinned is the uint 2` and `Before is the int -1`.echo CaseValue::describe(Flag::class, 'Pinned'), "\n";echo CaseValue::describe(Offset::class, 'Before'), "\n";Pinned is the uint 2Before is the int -1