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

Reporters

Sometimes you don't want your code to fail, yet you want to know when an enum is tried to be instantiated, but failed. This allows you to do so.

usage

use Henzeb\Enumhancer\Concerns\Reporters;


enum YourEnum: string {
    use Reporters;

    case ENUM = 'your_enum';
    case ENUM2 = 'your_other_enum';
}
use Henzeb\Enumhancer\Contracts\Reporter;

class YourReporter implements Reporter {

    public function report(
    string $enum,
    ?string $key,
    ?BackedEnum $context
    ) :void {
        // TODO: Implement report() method.
    }
}

Examples

/** in the root of your project (or ServiceProvider in case of Laravel) */
Henzeb\Enumhancer\Helpers\EnumReporter::set(new YourReporter());
Henzeb\Enumhancer\Helpers\EnumReporter::set(YourReporter::class);

/** getOrReport */
YourEnum::getOrReport('ENUM'); // will just return the enum
YourEnum::getOrReport('your_enum'); // will just return the enum
YourEnum::getOrReport('unknown'); // will return null and call the Reporter

YourEnum::getOrReportArray(
    ['ENUM', 'your_other_enum']
); // will just return the enums

YourEnum::getOrReportArray(
    ['ENUM', 'unknown']
); // will return [YourEnum::ENUM] and call the Reporter for 'unknown'

Note: each method accepts a BackedEnum so you can give a bit more context when logging.

Reporters per enum

You can also set a reporter for an enum by overriding the reporter method. Enumhancer will use that one instead

use Henzeb\Enumhancer\Contracts\Reporter;

enum YourEnum: string {
    use Reporters;

    case ENUM = 'your_enum';
    case ENUM2 = 'your_other_enum';

    public function reporter(): Reporter
    {
        return YourCustomReporter();
    }
}

Laravel

For laravel, there is out of the box support, which is automatically loaded see README.

Out of the box, the reporter will report to your configured default channel, but you can change the channel and the LogLevel.

Note: You don't need to disable autodiscovery for this, as it will override the configuration.

use Henzeb\Enumhancer\Enums\LogLevel;
use Henzeb\Enumhancer\Helpers\EnumReporter;

EnumReporter::laravel(LogLevel::Alert); // alerts to configured channel
EnumReporter::laravel(null, 'stack'); // notices to stack
EnumReporter::laravel(null, 'stack', 'daily'); // notices to stack and daily
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