/php-enum-util

The extension library for PHP native enum.

Primary LanguagePHP

🛠 fuwasegu/php-enum-util

Coverage Status example workflow MIT License PHP Version Require Total Downloads

The extension library for PHP native enum.

📦 Installation

composer require fuwasegu/php-enum-util

✏️ Usage

This library provides traits to make Enum, implemented since PHP8.1, easier to use.

HasDescription trait

This trait provides an interface to the description() method and an implementation of the descriptions() method.

enum Status: string
{
    use HasDescription;

    case ACTIVE = 'active';

    case INACTIVE = 'inactive';

    case RETIED = 'retired';

    public function description(): string
    {
        return match ($this) {
            self::ACTIVE => 'State in which the employee is enrolled.',
            self::INACTIVE => 'State in which the employee is on administrative leave.',
            self::RETIED => 'State in which employee is retiring.',
        };
    }
}
// Getter for Enum description
Status::ACTIVE->description();

// Getter for Enum value and description maps
Status::descriptions();

HasLabel trait

This trait provides an interface to the label() method and an implementation of the labels() method.

enum Capital: int
{
    use HasLabel;

    case TOKYO = 1;

    case BEIJING = 2;

    case WASHINGTON = 3;

    case LONDON = 4;

    public function label(): string
    {
        return match ($this) {
            self::TOKYO => 'Tokyo',
            self::BEIJING => 'Beijing',
            self::WASHINGTON => 'Washington',
            self::LONDON => 'London',
        };
    }
}
// Getter for Enum label
Status::ACTIVE->label();

// Getter for Enum value and label maps
Status::labels();

📌 NOTICE:

There is no implementation difference between the HasLabel and HasDescription traits. The difference is the method name.

Choose the former if you want a short label for an enum, and choose the latter if you want a long description.

HasValues trait

This trait is the value version of the cases() method originally implemented in Enum, and provides auxiliary methods.

enum Status: string
{
    use HasValues;

    case ACTIVE = 'active';

    case INACTIVE = 'inactive';

    case RETIED = 'retired';
}
// Getter for Enum values
Status::values();

// Join Enum values with a string
Status::implodeValues();

HasNames trait

This trait is the name version of the cases() method originally implemented in Enum, and provides auxiliary methods.

enum Status: string
{
    use HasNames;

    case ACTIVE = 'active';

    case INACTIVE = 'inactive';

    case RETIED = 'retired';
}
// Getter for Enum names
Status::names();

// Join Enum names with a string
Status::implodeNames();

Comparable trait

This trait provides a method for easy comparison of Bakced Enums

enum Status: string
{
    use Comparable;

    case ACTIVE = 'active';

    case INACTIVE = 'inactive';

    case RETIED = 'retired';
}
// Compare Enum peer to peer
$maybeActive = Status::ACTIVE;
Status::ACTIVE->is($maybeActive); // true
Status::INACTIVE->isNot($maybeActive); // false

// Attempts to convert the compared int or string to an Enum with Enum::tryFrom before comparing
// If tryFrom is null, `isFrom()` returns false and `isNotFrom()` returns true.
$value = 'active';
Status::ACTIVE->isFrom($value); // true
Status::INACTIVE->isNotFrom($value); // false
Status::ACTIVE->isFrom('foo'); // false
Status::ACTIVE->isNotFrom('foo') // true