tenthfeet/enums

Helpers for PHP enums

Installs: 4

Dependents: 0

Suggesters: 0

Security: 0

Stars: 1

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/tenthfeet/enums

v1.0.0 2026-01-03 09:02 UTC

This package is not auto-updated.

Last update: 2026-01-04 07:39:08 UTC


README

A lightweight PHP trait that adds powerful, type-safe helper methods to PHP 8.1+ enums. Designed for pure PHP projects, with seamless compatibility for frameworks such as Laravel, Symfony, or Slim.

Requirements

  • PHP 8.1 or higher

  • Native PHP enums (PureEnum, BackedEnum)

No external dependencies.

Installation

Via Composer (recommended)

  composer require tenthfeet/enums

Usage/Examples

1.Add the trait to your enum

use Tenthfeet\Enums\Traits\InteractWithCases;

enum Status
{
    use InteractWithCases;

    case Active;
    case Inactive;
}

For backed enums:

use Tenthfeet\Enums\Traits\InteractWithCases;

enum PaymentStatus: int
{
    use InteractWithCases;

    case Pending = 1;
    case Paid = 2;
}

Instance Methods

is() / isNot()

Strict identity comparison between enum cases.

Status::Active->is(Status::Active);      // true
Status::Active->is(Status::Inactive);    // false

Status::Active->isNot(Status::Inactive); // true

in() / notIn()

Check if an enum exists within an iterable collection.

Status::Active->in([Status::Inactive, Status::Active]); // true
Status::Active->notIn([Status::Inactive]);              // true
  • Ignores non-enum values safely

  • Uses strict enum identity comparison

__invoke()

Calling an enum case as a function returns:

  • the value for backed enums

  • the name for pure enums

Status::Active();         // "Active"
PaymentStatus::Paid();   // 2

Static Methods

names()

Returns all enum case names.

Status::names(); // ["Active", "Inactive"]

values()

Returns:

  • enum values for backed enums

  • enum names for pure enums

PaymentStatus::values();  // [1, 2]

Status::values();         // ["Active", "Inactive"]

options()

Generates a standardized array of options, ideal for:

  • HTML select inputs

  • API responses

  • Frontend frameworks

Status::options();

// Output
[
    ['id' => 'Active', 'text' => 'Active'],
    ['id' => 'Inactive', 'text' => 'Inactive'],
]

Using a custom label method

enum PaymentStatus: int
{
    use InteractWithCases;

    case Pending = 1;
    case Paid = 2;

    public function label(): string
    {
        return match ($this) {
            self::Pending => 'Not Received',
            self::Paid => 'Received',
        };
    }
}

PaymentStatus::options('label');

// Output
[
    ['id' => 1, 'text' => 'Not Received'],
    ['id' => 2, 'text' => 'Received'],
]

Custom keys

PaymentStatus::options(property: 'label', valueKey: 'value', nameKey: 'label');

// Output
[
    ['value' => 1, 'label' => 'Not Received'],
    ['value' => 2, 'label' => 'Received'],
]