Weave Code
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

Enumhancer Laravel Package

henzeb/enumhancer

View on GitHub
Deep Wiki
Context7

Macros

If you use my library inside your own package, you may want to allow people to 'extend' the functionality on your enum object. Extension is not possible, and you can't use Laravel's or Spatie's Macroable as this is using a property, and properties are not allowed on an enum as state is forbidden.

NOTE: Macros are not enabled by default.

Also be aware that macros will follow PHP standards. Meaning that static macro's can be called statically and non-statically, but non-static macros can only be called non-statically.

Example

enum Suit {
    use Henzeb\Enumhancer\Concerns\Macros;

    case Hearts;
    case Clubs;
    // ...
}

Macro

/** static **/
Suit::macro('shuffle', static fn() => self::cases()[array_rand(self::cases())] );
Suit::shuffle(); // returns one of the available cases
Suit::Hearts->shuffle(); // returns one of the available cases

/** non-static **/
Suit::macro('serialize', fn(bool $json) => $json?json_encode($this->name):serialize($this));
Suit::Hearts->serialize(true); // returns "Hearts"
Suit::Hearts->serialize(false); // returns E:11:"Suit:Hearts"
Suit::serialize(true); // throws fatal error

Mixin

Just like other libraries, Macros supports Mixin.

class SuitMixin {
    public function shuffle(): Closure
    {
        return static fn() => self::cases()[array_rand(self::cases())];
    }

    public function serialize(): Closure
    {
        return fn(bool $json) => $json?json_encode($this->name):serialize($this);
    }
}

Suit::mixin(SuitMixin::class);
Suit::mixin(new SuitMixin());

hasMacro

This method will allow you to test if a method exists before using it.

Suit::hasMacro('toJson'); // returns true if exists, false otherwise

flushMacros

You may want to flush macro's in certain situations. This will only flush macro's for the enum it's called on.

Suit::flushMacros(); // only flushes macro's belonging to Suit

Global macro's

Enumhancer also supports global macro's, which will be available to you on every enum using the Macros trait.

\Henzeb\Enumhancer\Helpers\Enumhancer::macro('macro', fn()=>true);

\Henzeb\Enumhancer\Helpers\Enumhancer::mixin(GlobalMixin::class);
\Henzeb\Enumhancer\Helpers\Enumhancer::mixin(new GlobalMxin());

\Henzeb\Enumhancer\Helpers\Enumhancer::flushMacros();

Note: flushing macro's using the global method only flushes global macro's. flushing macro's on an enum will not touch the global macro's

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.
emuniq/filament-browser-notifications
syriable/filament-translator
hungnm28/livewire-form
wenprise/eloquent
crudly/encrypted
fadion/bouncy
cuci/prototurk-sdk
gos/pubsub-router-bundle
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui