PHPackages                             litgroup/enumerable - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. litgroup/enumerable

ArchivedLibrary[Utility &amp; Helpers](/categories/utility)

litgroup/enumerable
===================

Implementation of the enumerable type for PHP.

v0.9.0(1mo ago)576.5k—7.1%23MITPHPPHP &gt;=8.5

Since Nov 16Pushed 5y ago1 watchersCompare

[ Source](https://github.com/LitGroup/enumerable.php)[ Packagist](https://packagist.org/packages/litgroup/enumerable)[ RSS](/packages/litgroup-enumerable/feed)WikiDiscussions master Synced 2w ago

READMEChangelogDependencies (2)Versions (12)Used By (3)

Enumerable
==========

[](#enumerable)

This library was developed to support enum types before PHP 8.1.

Since release `0.9.0` of the library it provides API similar to current PHP 8 backed enum types. Previous API is marked as deprecated. This change was made intentionally to support migration to native enums in PHP 8.

See [change log](CHANGELOG.md) for more details about API changes.

[![Version](https://camo.githubusercontent.com/87c38723acaa3f3bd581d5480b5030096d9b40c09c7e434c191ead6f02d94ded/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6c697467726f75702f656e756d657261626c652e737667)](https://packagist.org/packages/litgroup/enumerable)[![Downloads](https://camo.githubusercontent.com/98b29bbf7e4573806864d85eab26064f603b3017b2bd8fa302573f8de0d6f893/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6c697467726f75702f656e756d657261626c652e737667)](https://packagist.org/packages/litgroup/enumerable)[![License](https://camo.githubusercontent.com/7013272bd27ece47364536a221edb554cd69683b68a46fc0ee96881174c4214c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e737667)](LICENSE)

---

- [Enumerable](#enumerable)
    - [Installation](#installation)
    - [Example of usage](#example-of-usage)
        - [Definition](#definition)
        - [Equality/Identity checking](#equalityidentity-checking)
        - [Usage in switch-case statement](#usage-in-switch-case-statement)
        - [Serialization and Persistence](#serialization-and-persistence)
        - [Extensibility](#extensibility)
    - [Run tests](#run-tests)
    - [LICENSE](#license)

Installation
----------------------------------------------------

[](#installation)

Install via composer:

```
composer require litgroup/enumerable:^0.9.0
```

Example of usage
------------------------------------------------------------

[](#example-of-usage)

### Definition

[](#definition)

1. Create `final` class, which extends `Enumerable`;
2. For each variant of values create a static method, which will creates an instance of value. For this purpose your method must call `Enumerable::case()` with some backed value.

> **Note:**
>
> - Enumerable class must be `final`!
> - Backed can be ether `string` or `int`.

**Enum definition example:**

```
namespace Acme;

use LitGroup\Enumerable\Enumerable;

final class ColorEnum extends Enumerable
{
    public static function red(): self
    {
        return self::case('red');
    }

    public static function green(): self
    {
        return self::case('green');
    }

    public static function blue(): self
    {
        return self::case('blue');
    }
}
```

### Equality/Identity checking

[](#equalityidentity-checking)

You can use enumerable values in equality/identity expressions:

```
ColorEnum::red() == ColorEnum::red() // => true
ColorEnum::red() === ColorEnum::red() // => true

ColorEnum::red() == ColorEnum::blue() // => false
ColorEnum::red() === ColorEnum::blue() // => false
```

> **Note:** Enumerables works as runtime constants. Therefore enumerable values can be checked on **identity**. And we recommend to use check on identity (`===`) instead of equality (`==`) if possible.

### Usage in switch-case statement

[](#usage-in-switch-case-statement)

```
$color = ColorEnum::green();

switch ($color) {
    case ColorEnum::red():
        echo "Red!\n";
        break;
    case ColorEnum::green():
        echo "Green!\n";
        break;
    case ColorEnum::blue():
        echo "Blue!\n";
        break;
}

// "Green!" will be printed
```

A `match` expression also works es expected.

### Serialization and Persistence

[](#serialization-and-persistence)

`Enumerable` works as runtime-constant. Enumerable type cannot be serialized. If you need to store representation of enumerable in a database or send it via an API you can use a backed value of enumerable as its representation.

```
$enum->value;
```

To restore an instance of enumerable type by its backed value from database or from API-request you can use static method `tryFrom()` on the concrete enum-class.

```
$colorRawValue = fetchValueFromDatabase(/* something */);

$enum = ColorEnum::tryFrom($colorRawValue);
```

If you need to get all values of enumerable type, use static method `cases()` on the concrete enum-class.

```
ColorEnum::cases(); // => Returns a list of enum cases
```

### Extensibility

[](#extensibility)

Instances of your enumerable classes can have additional behavior if it needed. But you cannot define any `public static` methods with custom behavior. Public static methods used only for initialization of the enum.

> **Note:** You cannot define any `public static` methods with custom behavior. Public static methods used only for initialization of the enum.

**Example:**

```
final class MergeRequestStatus extends Enumerable {

    public static function open(): self
    {
        return self::case('open');
    }

    public static function approved(): self
    {
        return self::case('approved');
    }

    public static function merged(): self
    {
        return self::case('merged');
    }

    public static function declined(): self
    {
        return self::case('declined');
    }

    /**
     * Returns true if status is final.
     */
    public function isFinal(): bool
    {
        return $this === self::merged() || $this === self::declined();
    }
}
```

Run tests
----------------------------------------------

[](#run-tests)

```
composer install
composer test
```

LICENSE
------------------------------------------

[](#license)

See [LICENSE](LICENSE) file.

###  Health Score

49

—

FairBetter than 94% of packages

Maintenance52

Moderate activity, may be stable

Popularity35

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity77

Established project with proven stability

 Bus Factor1

Top contributor holds 98.5% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~433 days

Recently: every ~895 days

Total

10

Last Release

30d ago

PHP version history (5 changes)v0.2.0PHP ~5.5|~7.0

v0.5.0PHP ~5.5|^7.0

v0.6.0PHP ^7.0

v0.8.0PHP ^7.3 || ^8.0

v0.9.0PHP &gt;=8.5

### Community

Maintainers

![](https://www.gravatar.com/avatar/b22ffdbd95942ae3f342f2073f97f3a5716459ad5892f37f443948952b85c012?d=identicon)[Sharom](/maintainers/Sharom)

---

Top Contributors

[![Sharom](https://avatars.githubusercontent.com/u/694901?v=4)](https://github.com/Sharom "Sharom (67 commits)")[![sbooker](https://avatars.githubusercontent.com/u/3658174?v=4)](https://github.com/sbooker "sbooker (1 commits)")

---

Tags

enumeration-valuesphpenumenumerable

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/litgroup-enumerable/health.svg)

```
[![Health](https://phpackages.com/badges/litgroup-enumerable/health.svg)](https://phpackages.com/packages/litgroup-enumerable)
```

###  Alternatives

[myclabs/php-enum

PHP Enum implementation

2.7k238.8M718](/packages/myclabs-php-enum)[spatie/enum

PHP Enums

85133.9M80](/packages/spatie-enum)[dasprid/enum

PHP 7.1 enum implementation

384173.2M12](/packages/dasprid-enum)[marc-mabe/php-enum

Simple and fast implementation of enumerations with native PHP

49863.5M114](/packages/marc-mabe-php-enum)[spatie/laravel-enum

Laravel Enum support

3655.9M36](/packages/spatie-laravel-enum)[cerbero/enum

Zero-dependencies package to supercharge enum functionalities.

360244.5k2](/packages/cerbero-enum)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
