Skip to content

Lookup by name

phil/enums

AsFromambleEnumTrait finds a case by its name. Every enum gets fromName() and tryFromName(). A pure enum, which PHP gives no from(), also gets from() and tryFrom() as the same lookup; a backed enum keeps PHP's own from() and tryFrom(), which read its values.

use Phil\Enums\Traits\AsFromambleEnumTrait;

enum Unit: string
{
    use AsFromambleEnumTrait;

    case GRAM = 'g';
    case LITRE = 'l';
}

enum Coin
{
    use AsFromambleEnumTrait;

    case HEADS;
    case TAILS;
}
MethodOnAnswersWhen no case has the name
fromName(string $case)every enumthe case with this namethrows ValueError
tryFromName(string $case)every enumthe case with this namenull
from(int|string $case)a pure enumthe case with this namethrows ValueError
tryFrom(int|string $case)a pure enumthe case with this namenull
Unit::fromName('LITRE');   // Unit::LITRE
Unit::from('l');           // Unit::LITRE: PHP's from(), by value
Unit::tryFromName('l');    // null: 'l' is a value, not a name

Coin::from('HEADS');       // Coin::HEADS
Coin::tryFrom('EDGE');     // null
Coin::fromName('heads');   // ValueError: names are compared exactly, letter case included

The message of the ValueError names the input and the enum: "EDGE" is not a valid name for enum App\Coin. For a lookup that also accepts values, aliases or another letter case, use parse() (Order, parsing, defaults and transitions).