convoflo/ordered-enum

There is no license information available for the latest version (1.0.0) of this package.

Give your enums an order. Provides utilities such as greaterThan, lessThan, min, max and more.

1.0.0 2024-12-03 16:41 UTC

This package is auto-updated.

Last update: 2025-03-31 17:25:22 UTC


README

This package gives your enums an order. It provides helper utilities such as ->greaterThan(), ->lessThan(), ::min(), ::max() and more.

Installation

You can install the package via composer:

composer require convoflo/ordered-enum

Usage

This is how an ordered enum can be defined.

use \Convoflo\OrderedEnum\OrderedBackedEnum;
use \Convoflo\OrderedEnum\OrderedTrait;

enum UserRole: string implements OrderedBackedEnum
{
    use OrderedTrait;

    case Admin = 'admin';
    case Regular = 'user';
    case Basic = 'basic';

    public static function order(): array
    {
        return [self::Basic, self::Regular, self::Admin];
    }
}

Here's how ordered enums are used with their helpers:

$showAdminConsole = $userRole->greaterThan(UserRole::Regular);
$showMinimalInterface = $userRole->lessThan(UserRole::Regular);

$isNotBasic = $userRole->greaterThanOrEqualsTo(UserRole::Regular);
$isNotAdmin = $userRole->lessThanOrEqualsTo(UserRole::Regular);

UserRole::min(UserRole::Basic, UserRole::Admin); // UserRole::Basic
UserRole::max(UserRole::Basic, UserRole::Admin); // UserRole::Admin

$highestRole = UserRole::max(); // UserRole::Admin
$lowestRole = UserRole::min(); // UserRole::Basic

Credits

License

The MIT License (MIT). Please see License File for more information.