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

ActiveLibrary

fatturaelettronicaphp/enum
==========================

PHP Enums

1.2.2(7y ago)0433MITPHPPHP ^7.1

Since Feb 7Pushed 7y ago2 watchersCompare

[ Source](https://github.com/fatturaelettronicaphp/enum)[ Packagist](https://packagist.org/packages/fatturaelettronicaphp/enum)[ Docs](https://github.com/fatturaelettronicaphp/enum)[ RSS](/packages/fatturaelettronicaphp-enum/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependencies (2)Versions (10)Used By (0)

PHP Enum
========

[](#php-enum)

[![Latest Version on Packagist](https://camo.githubusercontent.com/a3fa4fa627d6975700eb0860441b62235b97625aa64f6723e761599c918d0073/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f66617474757261656c657474726f6e6963617068702f656e756d2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/fatturaelettronicaphp/enum)[![Build Status](https://camo.githubusercontent.com/fba8e859f5406e1c336154a09a5b87e24892801f0c7c76862a5a6fac802df538/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f66617474757261656c657474726f6e6963617068702f656e756d2f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/fatturaelettronicaphp/enum)[![Total Downloads](https://camo.githubusercontent.com/b69f4e76ee662bce83e0958231d2c58cd88803f8b529bfdd7cdb8bcf18e1c310/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f66617474757261656c657474726f6e6963617068702f656e756d2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/fatturaelettronicaphp/enum)

**THIS PACKAGE IS A FORK OF [Spatie/Enum](https://github.com/spatie/enum) to allow for PHP 7.1 support.**

This package offers strongly typed enums in PHP. We don't use a simple "value" representation, so you're always working with the enum object. This allows for proper autocompletion and refactoring in IDEs.

Here's how enums are created with this package:

```
/**
 * @method static self draft()
 * @method static self published()
 * @method static self archived()
 */
class StatusEnum extends Enum
{
}
```

And this is how they are used:

```
public function setStatus(StatusEnum $status): void
{
    $this->status = $status;
}

// …

$class->setStatus(StatusEnum::draft());
```

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

[](#installation)

You can install the package via composer:

```
composer require fatturaelettronicaphp/enum
```

Usage
-----

[](#usage)

This is how an enum can be defined.

```
/**
 * @method static self draft()
 * @method static self published()
 * @method static self archived()
 */
class StatusEnum extends Enum
{
}
```

This is how they are used:

```
public function setStatus(StatusEnum $status)
{
    $this->status = $status;
}

// …

$class->setStatus(StatusEnum::draft());
```

[![](./docs/autocomplete.gif)](./docs/autocomplete.gif)

[![](./docs/refactor.gif)](./docs/refactor.gif)

### Creating an enum from a value

[](#creating-an-enum-from-a-value)

```
$status = StatusEnum::from('draft');

// or

$status = new StatusEnum('published');
```

### Override enum values

[](#override-enum-values)

By default, the string value of an enum is simply the name of that method. In the previous example it would be `draft`.

You can override this value, by adding the `$map` property:

```
/**
 * @method static self draft()
 * @method static self published()
 * @method static self archived()
 */
class StatusEnum extends Enum
{
    protected static $map = [
        'draft' => '1',
        'published' => 'other published value',
        'archived' => '-10',
    ];
}
```

Mapping values is optional.

> Note that mapped values should always be strings.

### Comparing enums

[](#comparing-enums)

Enums can be compared using the `equals` method:

```
$status->equals($otherStatus);
```

You can also use dynamic `is` methods:

```
$status->isDraft(); // return a boolean
```

Note that if you want auto completion on these `is` methods, you must add extra doc blocks on you enum classes.

### Enum specific methods

[](#enum-specific-methods)

There might be a case where you want to have functionality depending on the concrete enum value.

There are several ways to do this:

- Add a function in the enum class and using a switch statement or array mapping.
- Use a separate class which contains this switch logic, something like enum extensions in C#.
- Use enum specific methods, similar to Java.

This package also supports these enum specific methods. Here's how you can implement them:

```
abstract class MonthEnum extends Enum
{
    abstract public function getNumericIndex(): int;

    public static function january(): MonthEnum
    {
        return new class() extends MonthEnum
        {
            public function getNumericIndex(): int
            {
                return 1;
            }
        };
    }

    public static function february(): MonthEnum
    {
        return new class() extends MonthEnum
        {
            public function getNumericIndex(): int
            {
                return 2;
            }
        };
    }

    // …
}
```

By declaring the enum class itself as abstract, and using static constructors instead of doc comments, you're able to return an anonymous class per enum, each of them implementing the required methods.

You can use this enum the same way as any other:

```
MonthEnum::january()->getNumericIndex()
```

Note that one drawback of this approach is that the value of the enum **is always** the name of the static function, there's no way of mapping it.

### Testing

[](#testing)

```
composer test
```

### Changelog

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

### Security

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Brent Roose](https://github.com/brendt)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

29

—

LowBetter than 60% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 50% 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 ~7 days

Total

8

Last Release

2600d ago

Major Versions

0.0.1 → 1.0.02019-02-08

PHP version history (2 changes)0.0.1PHP ^7.2

1.2.0PHP ^7.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/2fe88932c3280467c9a30f79205c0f2c85aecd28dae3a9e8b035d773ef6218ef?d=identicon)[Skullbock](/maintainers/Skullbock)

---

Top Contributors

[![brendt](https://avatars.githubusercontent.com/u/6905297?v=4)](https://github.com/brendt "brendt (39 commits)")[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (18 commits)")[![Gummibeer](https://avatars.githubusercontent.com/u/6187884?v=4)](https://github.com/Gummibeer "Gummibeer (11 commits)")[![Skullbock](https://avatars.githubusercontent.com/u/1104083?v=4)](https://github.com/Skullbock "Skullbock (6 commits)")[![b1rdex](https://avatars.githubusercontent.com/u/312855?v=4)](https://github.com/b1rdex "b1rdex (2 commits)")[![chapeupreto](https://avatars.githubusercontent.com/u/834048?v=4)](https://github.com/chapeupreto "chapeupreto (1 commits)")[![bramus](https://avatars.githubusercontent.com/u/213073?v=4)](https://github.com/bramus "bramus (1 commits)")

---

Tags

enum

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[myclabs/php-enum

PHP Enum implementation

2.7k227.9M637](/packages/myclabs-php-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

379146.0M11](/packages/dasprid-enum)[spatie/enum

PHP Enums

84429.1M68](/packages/spatie-enum)[marc-mabe/php-enum

Simple and fast implementation of enumerations with native PHP

49444.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)
