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. litgroup/enumerable

AbandonedArchivedLibrary

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

Implementation of the enumerable type for PHP.

v0.8.0(5y ago)573.7k↓33.9%23MITPHPPHP ^7.3 || ^8.0

Since Nov 16Pushed 5y ago2 watchersCompare

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

READMEChangelogDependencies (1)Versions (10)Used By (3)

Enumerable
==========

[](#enumerable)

> Library provides support of enumerable classes for PHP.

[![Version](https://camo.githubusercontent.com/87c38723acaa3f3bd581d5480b5030096d9b40c09c7e434c191ead6f02d94ded/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6c697467726f75702f656e756d657261626c652e737667)](https://packagist.org/packages/litgroup/enumerable)[![Dev Version](https://camo.githubusercontent.com/927f2840ec589536a3f1afc5bd7ec6fa614fb5ae544265ff37ed74583bd08780/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f767072652f6c697467726f75702f656e756d657261626c652e737667)](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)](https://raw.githubusercontent.com/LitGroup/enumerable.php/master/LICENSE)[![Build Status](https://camo.githubusercontent.com/b41dd6c31b9f029bbc693b5b5c608968bb9fe8fec00867bbb588c114f8e9b7fa/68747470733a2f2f7472617669732d63692e6f72672f4c697447726f75702f656e756d657261626c652e7068702e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/LitGroup/enumerable.php)

- [Enumerable](#enumerable)
    - [](#installation)Installation
    - [](#example-of-usage)Example of usage
        - [](#definition)Definition
        - [](#equalityidentity-checking)Equality/Identity 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.8.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::createEnum()` with some index of value.

> **Note:**
>
> - Enumerable class must be `final`!
> - Index can be of type `string` or `int`.

**Enum definition example:**

```
namespace Acme;

use LitGroup\Enumerable\Enumerable;

final class ColorEnum extends Enumerable
{
    /**
     * @return self
     */
    public static function red()
    {
        return self::createEnum('red');
    }

    /**
     * @return self
     */
    public static function green()
    {
        return self::createEnum('green');
    }

    /**
     * @return self
     */
    public static function blue()
    {
        return self::createEnum('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. Therefor enumerable values can be checked on **identity**. And we recommend to use check on identity (`===`) instesd 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
```

### 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 index of enumerable value as representation.

```
$enum->getRawValue();
```

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

```
$colorIndex = getFromDatabase(/* something */);

$enum = ColorEnum::getValueOf($colorIndex);
```

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

```
ColorEnum::getValues(); // => Returns array of ColorEnum with index as key
```

### Extensibility

[](#extensibility)

Instances of your enumerable classes can have additional behaviour if it needed. But you cannot define any `public static` methods with behaviour. Public static methods used only for creation of values.

> **Note:** You cannot define any `public static` methods with behaviour. Public static methods used only for creation of values.

**Example:**

```
final class MergeRequestStatus extends Enumerable {

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

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

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

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

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

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

[](#run-tests)

```
composer install
./tests.sh
```

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

[](#license)

See [LICENSE](https://raw.githubusercontent.com/LitGroup/enumerable.php/master/LICENSE) file.

###  Health Score

37

—

LowBetter than 83% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity35

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity64

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 ~238 days

Recently: every ~416 days

Total

9

Last Release

1934d ago

PHP version history (4 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

### 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.7k227.9M637](/packages/myclabs-php-enum)[spatie/enum

PHP Enums

84529.1M68](/packages/spatie-enum)[bensampo/laravel-enum

Simple, extensible and powerful enumeration implementation for Laravel.

2.0k15.9M104](/packages/bensampo-laravel-enum)[dasprid/enum

PHP 7.1 enum implementation

382146.0M11](/packages/dasprid-enum)[marc-mabe/php-enum

Simple and fast implementation of enumerations with native PHP

49644.8M97](/packages/marc-mabe-php-enum)[fresh/doctrine-enum-bundle

Provides support of ENUM type for Doctrine2 in Symfony applications.

4636.8M12](/packages/fresh-doctrine-enum-bundle)

PHPackages © 2026

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