Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Enum Bundle Laravel Package

yeugone/enum-bundle

yeugone/enum-bundle is a PHP package for Laravel developers seeking a cleaner way to organize and work with enums. It bundles practical enum utilities that help define type-safe values, reduce scattered constants, and keep application code more structured and readable across your project.

View on GitHub
Deep Wiki
Context7

Getting Started

The package bundles a simple value-object enumeration system (constants on a class) with a Symfony bundle that wires it into the container, forms, Doctrine, and validation. It is not a Laravel package — do not try to install it there. Confirm the actual namespace and base class in the repo's src/ before writing code, since the API surface is small and version drift is a real risk at this maturity level.

  1. Install:
    composer require yeugone/enum-bundle
    
  2. Register the bundle in config/bundles.php (Symfony Flex usually does this automatically if the package ships a Flex recipe; verify):
    return [
        // ...
        Yeugone\EnumBundle\EnumBundle::class => ['all' => true],
    ];
    
  3. Look first at src/Enum.php (or similarly named base class) — that is the only file you really need to understand. Then check DependencyInjection/ for the extension alias (usually enum or yeugone_enum) and Resources/config/services.yaml for what gets registered.
  4. First use case — replace magic strings on an entity status:
    use Yeugone\EnumBundle\Enum;
    
    final class OrderStatus extends Enum
    {
        public const PENDING   = 'pending';
        public const SHIPPED   = 'shipped';
        public const CANCELLED = 'cancelled';
    }
    
    // Day-to-day usage
    $status = new OrderStatus(OrderStatus::SHIPPED);
    $status->getValue();          // 'shipped'  (verify method name)
    OrderStatus::values();        // ['pending','shipped','cancelled']
    OrderStatus::isValid('foo');  // false
    
    The immediate win is that OrderStatus::values() can feed a Symfony ChoiceType and a Doctrine choice/string column without duplicating the list in three places.

Implementation Patterns

Value-object enum classes. Treat every enum as an immutable wrapper around a scalar constant. Instantiate once per request (they are cheap), pass them around typed against the concrete class, and only unwrap to scalar at the persistence / serialization / template boundary:

public function ship(): void
{
    if (!$this->status->equals(new OrderStatus(OrderStatus::PENDING))) {
        throw new \DomainException('Cannot ship a non-pending order.');
    }
    $this->status = new OrderStatus(OrderStatus::SHIPPED);
}

Symfony form integration. Map the enum to a ChoiceType using choices: OrderStatus::values() plus a choice_label that humanizes the value, and convert the chosen string back to an enum in the DTO setter (or use a data transformer). This is the most common day-to-day integration and the one most likely to save time.

Doctrine mapping. Persist enums as a plain string/integer column and convert in the entity getter/setter. Don't rely on a custom DBAL type unless you've verified it ships in this bundle — the bundle is small, and a hand-rolled Type in your app is more predictable than betting on undocumented behavior.

Validation. Register a custom constraint (#[Enum(OrderStatus::class)]) or a closure on the property that calls SomeEnum::isValid($value). Keeping this in one constraint avoids scattering in_array checks across controllers.

Service wiring. If the bundle exposes a tagged Enum service or autoconfiguration, prefer explicit $container->register() in your own bundle config so you control the enum registry. Where the bundle offers a compiler pass for collecting enum classes, use it only for cross-cutting concerns (e.g. building a global "all statuses" list for admin filters).

Serialization. Enums in API responses should go out as their scalar value, not as objects. Add JsonSerializable handling (either on a shared base class or via a Symfony normalizer) so json_encode($order) yields "status": "shipped" — never {"value": "shipped"} leaking into a public contract.

Gotchas and Tips

  • Maturity risk. 12 stars, zero dependents, last release months ago — treat this as a small utility you could vendor in an afternoon. Pin the exact version in composer.json and add a couple of unit tests around your enum classes so a future incompatibility is caught by your own suite, not in production.
  • Constants must be unique scalar values. PHP constants on a class are int|string; the base class likely validates this, but don't assume — a duplicate value silently breaks from()/isValid() lookups. Add a test asserting count(Enum::values()) === count((new ReflectionClass(Enum::class))->getConstants()).
  • Static magic vs. typed methods. If the package uses __callStatic (OrderStatus::PENDING()), you lose IDE autocompletion and static analysis. Check src/ — if that's the API, prefer instantiating with new OrderStatus(OrderStatus::PENDING) and keep references to the raw constants, which are greppable.
  • Strict comparison. $enum->equals(...) is safer than == on the enum object. When comparing scalars, use === — Doctrine can hand you int where you expect string for integer-backed enums.
  • Bundle registration is easy to forget. If enums appear unregistered or services aren't found, the first thing to check is config/bundles.php and bin/console debug:container | grep enum.
  • No Laravel support. If you're a Laravel developer evaluating this, note it targets Symfony's DI and would need manual porting (a plain myclabs/php-enum-style base class is a drop-in alternative with far more community support).
  • Where to extend. The cleanest extension point is your own abstract App\Enum\Enum that extends the package base class — you can add toLabel(), jsonSerialize(), and #[Enum]-friendly helpers there without touching vendor code, so swapping the underlying library later is a one-file change.
  • Debugging tips. var_dump($enum) shows the wrapped value; if you get an "invalid value" exception, dump the raw input from the request/DB first — usually it's whitespace, a case mismatch ('Shipped' vs 'shipped'), or a null sneaking through a nullable column.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
middlewares/firewall
qubiqx/montapacking
nyoncode/laravel-package-console
pnscripts/filament-broadquill-lite
pnscripts/laravel-sentfold-lite
vleap/mx-sdk-laravel-internal
twidpay/utils
htetmyathlaing/scout-elasticsearch-driver
artisan-toolbox/front-awesome
ls/client-assistant
users-au/laravel-client
robertmarney/lara-hierarchial-collections
simonschaufi/rule-doc-generator
emissaoja/emissaoja
blackbricksoftware/gravityforms-civicrm-form-processor
al-rahabi/fancy-crud
jinas/dhivatar-component
mltstephane/laravel-quality
bernskiold/laravel-talentlms
bernskioldmedia/laravel-form-architect