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

Enum Helper Laravel Package

oskarstark/enum-helper

Helpers for PHP 8.1+ enums: compare enum cases (equals, notEquals, equalsOneOf) and convert enums to arrays (backed and non-backed). Includes an abstract EnumTestCase to simplify testing enum behavior.

View on GitHub
Deep Wiki
Context7

enum-helper

This library provides helpers for several enum operations:

  • compare
  • to array

It also provides an abstract EnumTestCase.

CI

Installation

composer require oskarstark/enum-helper

Usage

For example, you have the following Enum:

<?php

declare(strict_types=1);

namespace App\Enum;

enum Color: string
{
    case RED = 'red';
    case BLUE = 'blue';
}

You can use the following traits:

<?php

declare(strict_types=1);

namespace App\Enum;

+use OskarStark\Enum\Trait\Comparable;
+use OskarStark\Enum\Trait\ToArray;

enum Color: string
{
+   use Comparable;
+   use ToArray;

    case RED = 'red';
    case BLUE = 'blue';
}

Comparable Trait

This trait gives you the possibility to compare your enum with another one or a collection of enums like the following:

App\Enum\Color::RED->equals(App\Enum\Color::BLUE); // returns false
App\Enum\Color::RED->notEquals(App\Enum\Color::RED); // returns false
App\Enum\Color::RED->equalsOneOf([
    App\Enum\Color::BLUE,
    App\Enum\Color::RED,
]); // returns true

For example, you want to check if a color is a nice color:

<?php

declare(strict_types=1);

namespace App\Enum;

use OskarStark\Enum\Trait\Comparable;
use OskarStark\Enum\Trait\ToArray;

enum Color: string
{
    use Comparable;
    use ToArray;

    case RED = 'red';
    case BLUE = 'blue';
    case GREEN = 'green';
    
    public function isNice(): bool
    {
        return self::equalsOneOf([
            self::BLUE,
            self::GREEN
        ]);
    }
}
App\Enum\Color::RED->isNice(); // returns false
App\Enum\Color::BLUE->isNice(); // returns true

ToArray Trait

This trait gives you the possibility to get an enum as array like the following:

For Backed Enum

App\Enum\Color::toArray(); // returns ['RED' => 'red', 'BLUE' => 'blue']

For Non-Backed Enum

App\Enum\NonBackedEnum::toArray(); // returns ['RED' => 'RED', 'BLUE' => 'BLUE']
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.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky