PHPackages                             skywalker-labs/enum - 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. skywalker-labs/enum

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

skywalker-labs/enum
===================

A robust and type-safe Enum implementation for PHP, featuring singleton-based constants, mapping capabilities, and extensive compatibility from PHP 7.0 to 9.0.

v1.0.0(4mo ago)121MITPHPPHP &gt;=7.0 &lt;10.0CI passing

Since Feb 7Pushed 4mo agoCompare

[ Source](https://github.com/skywalker-labs/enum)[ Packagist](https://packagist.org/packages/skywalker-labs/enum)[ RSS](/packages/skywalker-labs-enum/feed)WikiDiscussions main Synced today

READMEChangelogDependencies (2)Versions (2)Used By (1)

Skywalker Enum
==============

[](#skywalker-enum)

A robust, type-safe, and singleton-based Enum implementation for PHP.

This library provides a clean way to implement enums in PHP, ensuring that each enum value is a unique singleton instance. It works across PHP versions from 7.0 to 9.0+.

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

[](#installation)

Install the package via Composer:

```
composer require skywalker-labs/enum
```

Basic Usage
-----------

[](#basic-usage)

To create an enum, extend `Skywalker\Enum\AbstractEnum` and define your constants as `protected const`.

```
use Skywalker\Enum\AbstractEnum;

/**
 * @method static self MONDAY()
 * @method static self TUESDAY()
 * ...
 */
final class WeekDay extends AbstractEnum
{
    protected const MONDAY = null;
    protected const TUESDAY = null;
    // ...
}
```

Pro Features
------------

[](#pro-features)

### 1. Flexible JSON Serialization

[](#1-flexible-json-serialization)

Control how your enums appear in API responses.

```
// Choose between 'name', 'value', or 'object'
AbstractEnum::$jsonMode = 'object';

echo json_encode(WeekDay::MONDAY());
// Output: {"name":"MONDAY","value":null,"label":"MONDAY","ordinal":0}
```

### 2. Native PHP Compatibility

[](#2-native-php-compatibility)

Use aliases that match PHP 8.1+ native enums for a smoother transition.

```
$day = WeekDay::from('MONDAY');     // Throws exception if not found
$day = WeekDay::tryFrom('INVALID'); // Returns null if not found
```

### 3. Randomization

[](#3-randomization)

Perfect for seeders and unit tests.

```
$randomDay = WeekDay::random();
```

### 4. String Helpers

[](#4-string-helpers)

```
echo WeekDay::MONDAY()->lowerName(); // "monday"
echo WeekDay::MONDAY()->camelName(); // "Monday"
```

### 5. Collection Power-Ups (`EnumSet`)

[](#5-collection-power-ups-enumset)

Functional methods for modern collections.

```
$set = EnumSet::allOf(WeekDay::class);

$weekends = $set->filter(fn($d) => $d->isAnyOf([WeekDay::SATURDAY(), WeekDay::SUNDAY()]));
$names = $set->map(fn($d) => $d->name());

$first = $set->first(); // MONDAY
$last = $set->last();   // SUNDAY
```

Ultimate Edition Features
-------------------------

[](#ultimate-edition-features)

### 1. Navigational Helpers

[](#1-navigational-helpers)

Sequence through your enums easily.

```
$day = WeekDay::MONDAY();
$next = $day->next();         // TUESDAY
$prev = $day->previous();     // null

if ($day->isBefore(WeekDay::FRIDAY())) {
    echo "Hang in there!";
}
```

### 2. Set Theory (`EnumSet`)

[](#2-set-theory-enumset)

Perform mathematical set operations.

```
$workDays = EnumSet::range(WeekDay::MONDAY(), WeekDay::FRIDAY());
$holidays = EnumSet::of(WeekDay::class, WeekDay::MONDAY());

$actualWork = $workDays->diff($holidays);
$allDays = $workDays->union($holidays);
$intersect = $workDays->intersect($holidays);
$restOfYear = $workDays->complement();
```

### 3. Tolerant Lookups

[](#3-tolerant-lookups)

Handle user input with case-insensitivity.

```
$day = WeekDay::fromNameInsensitive('monday'); // Returns MONDAY instance
```

Advanced Features
-----------------

[](#advanced-features)

### Collection &amp; UI Helpers

[](#collection--ui-helpers)

```
// Returns ['MONDAY' => 'MONDAY', 'TUESDAY' => 'TUESDAY', ...]
$options = WeekDay::toArray();
```

### Type-Safe Comparisons

[](#type-safe-comparisons)

```
if ($day->isAnyOf([WeekDay::SATURDAY(), WeekDay::SUNDAY()])) {
    echo "It's the weekend!";
}
```

### Bitmask Support

[](#bitmask-support)

Efficiently store multiple enum values in a single integer.

```
$mask = $set->toBitmask(); // integer e.g. 5
$restored = EnumSet::fromBitmask(WeekDay::class, $mask);
```

License
-------

[](#license)

This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.

###  Health Score

37

—

LowBetter than 81% of packages

Maintenance75

Regular maintenance activity

Popularity4

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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

Unknown

Total

1

Last Release

147d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/68241409c48fd5184c979320937b363826a5a9aeeb4260e8d1fa753be39458b9?d=identicon)[ermradulsharma](/maintainers/ermradulsharma)

---

Top Contributors

[![ermradulsharma](https://avatars.githubusercontent.com/u/15956546?v=4)](https://github.com/ermradulsharma "ermradulsharma (10 commits)")

---

Tags

compatibilityenummapconstantsphp-enumsingletontype-safe

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/skywalker-labs-enum/health.svg)

```
[![Health](https://phpackages.com/badges/skywalker-labs-enum/health.svg)](https://phpackages.com/packages/skywalker-labs-enum)
```

###  Alternatives

[dasprid/enum

PHP 7.1 enum implementation

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

Simple and fast implementation of enumerations with native PHP

50458.3M110](/packages/marc-mabe-php-enum)[rexlabs/enum

Enumeration (enum) implementation for PHP

47517.0k2](/packages/rexlabs-enum)[zlikavac32/php-enum

Better PHP enum support

225.7k5](/packages/zlikavac32-php-enum)

PHPackages © 2026

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