NewWe open-sourced 50+ Laravel packages
Custom AI apps, agents and automation — Roundly ConsultingRoundly
All packages

fromName() and fromLabel() throw RoundlyConsulting\Enums\Exceptions\EnumException when no case matches; random() throws it on an enum with no cases, validationRule() on a value it can’t write into an in: rule that reads back unchanged, and untranslated() on an enum without #[TranslatedLabels]. An invalid #[TranslatedLabels] group throws it from readable() — so from every label method — on first use. It extends PHP’s InvalidArgumentException, so either catch works:

use RoundlyConsulting\Enums\Exceptions\EnumException;

try {
    NewsCategory::fromName('Breaking');
} catch (EnumException $e) {
    $e->getMessage();
    // "No case named [Breaking] exists on enum [App\Enums\NewsCategory]."
}

try {
    NewsCategory::fromLabel('Breaking News');
} catch (InvalidArgumentException $e) {
    // EnumException extends InvalidArgumentException, so this catches it too
}

It has six named constructors:

Named constructorThrown byMessage
EnumException::nameNotFound(string $enum, string $name)fromName()No case named [{name}] exists on enum [{enum}].
EnumException::labelNotFound(string $enum, string $label)fromLabel()No case with label [{label}] exists on enum [{enum}].
EnumException::noCases(string $enum)random()Enum [{enum}] has no cases.
EnumException::valueNotRepresentable(string $enum, string $value)validationRule()Value [{value}] of enum [{enum}] cannot be written into an in: rule that reads back unchanged; validate it with Rule::enum() instead.
EnumException::invalidLabelGroup(string $enum, string $group)readable() — so every label method — and untranslated()Enum [{enum}] declares an invalid #[TranslatedLabels] group [{group}]: a group must not be blank or start or end with whitespace, '.' or ':'.
EnumException::labelsNotTranslated(string $enum)untranslated()Enum [{enum}] has no #[TranslatedLabels] attribute, so it has no translation keys to check.

EnumException is deliberately not final, so you can extend it. For user input, prefer the tryFrom* variants plus validation, and keep from* for trusted data, where a miss is a bug worth surfacing.

Show your open-source love

This package is free and MIT-licensed. If it saves you time, a one-off donation or a Patreon membership keeps it maintained, tested and documented.

More ways to support, including crypto

By donating, you agree to our donation terms.

Want this built into your product?

We integrate our packages into custom Laravel and AI builds. Tell us what you're working on and we'll reply within 48 hours.