PHPackages                             greg0ire/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. [Templating &amp; Views](/categories/templating)
4. /
5. greg0ire/enum

ActiveLibrary[Templating &amp; Views](/categories/templating)

greg0ire/enum
=============

work around the missing enum type in php

v4.3.1(2y ago)45390.5k↓17.5%4[2 issues](https://github.com/greg0ire/enum/issues)9CC-BY-SA-3.0PHPPHP ^7.1 || ^8.0

Since Jun 17Pushed 2y ago2 watchersCompare

[ Source](https://github.com/greg0ire/enum)[ Packagist](https://packagist.org/packages/greg0ire/enum)[ RSS](/packages/greg0ire-enum/feed)WikiDiscussions stable Synced 1mo ago

READMEChangelog (10)Dependencies (14)Versions (26)Used By (9)

Enums
=====

[](#enums)

This package holds a simple class that may be used as an ancestor for your enum classes.

[![Build Status](https://camo.githubusercontent.com/c531169c281a35991c76789a1a2edb18f8883542c910a2bdabc40b25ebc9d5a3/68747470733a2f2f7472617669732d63692e6f72672f67726567306972652f656e756d2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/greg0ire/enum)

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

[](#installation)

```
composer require greg0ire/enum

```

Usage
-----

[](#usage)

### Basic usage

[](#basic-usage)

Extend the `Greg0ire\Enum\AbstractEnum`, define your enum key values as constants, and Bob's your uncle. You can make the class abstract or final, as you see fit.

```
use Greg0ire\Enum\AbstractEnum;

final class DaysOfWeek extends AbstractEnum {
    const Sunday = 0;
    const Monday = 1;
    const Tuesday = 2;
    const Wednesday = 3;
    const Thursday = 4;
    const Friday = 5;
    const Saturday = 6;
}
```

Then, you may use the DaysOfWeek class for input validation:

```
DaysOfWeek::isValidName('Humpday');                  // false
DaysOfWeek::isValidName('Monday');                   // true
DaysOfWeek::isValidName('monday');                   // false
DaysOfWeek::isValidName(0);                          // false

DaysOfWeek::isValidValue(0);                         // true
DaysOfWeek::isValidValue(5);                         // true
DaysOfWeek::isValidValue(7);                         // false
DaysOfWeek::isValidValue('Friday');                  // false
```

Both methods have an `assert*` counterpart that will throw a `Greg0ire\Enum\Exception\InvalidEnumValue` exception:

```
DaysOfWeek::assertValidName(0);                      // InvalidEnumName
DaysOfWeek::assertValidValue('Friday');              // InvalidEnumValue

```

Additionally, you may get all the constants in your class as a hash:

```
DaysOfWeek::getConstants();
DaysOfWeek::getConstants('strtolower'); // Will combine your values with `DaysOfWeek::getKeys($callback)`.
DaysOfWeek::getConstants('strtolower', true); // Values combine with `DaysOfWeek::getClassPrefixedKeys($callback)`.
DaysOfWeek::getConstants('strtolower', true, '.'); // Same with `DaysOfWeek::getClassPrefixedKeys($callback, $separator)`.
```

You may also get all the keys in your class as an array:

```
DaysOfWeek::getKeys();
DaysOfWeek::getKeys('strtolower'); // Will call `array_map` with the given callback.
```

Or the key with the enum class prefix:

```
DaysOfWeek::getClassPrefixedKeys();
DaysOfWeek::getClassPrefixedKeys('strtolower'); // Will call `array_map` with the given callback.
DaysOfWeek::getClassPrefixedKeys('strtolower', '.'); // Replace the namespace separator ('_' by default).
```

If you would like to get the keys from a value:

```
$key = DaysOfWeek::getKeysFromValue(1); // Monday will be assigned to $key
```

If you have many keys with the same value you will get an array, and a value otherwise.

### Advanced usage

[](#advanced-usage)

If you need to get the constants from a class you cannot modify, or from an interface, or even from several classes / interfaces, you may override `AbstractEnum::getEnumTypes()`.

For example, if you have the following class and interface :

```
namespace Vendor\Namespace;

class ClassFromAVendor
{
   const SOMETHING      = 'something';
   const SOMETHING_ELSE = 'something_else';
}
```

```
namespace My\Namespace;

interface SomeInterface
{
   const ANOTHER_CONST = 'another_const';
}
```

You can get all three constants by creating this Enum :

```
use Greg0ire\Enum\AbstractEnum;
use My\Namespace\SomeInterface;
use Vendor\Namespace\ClassFromAVendor;

final class MyEnum extends AbstractEnum
{
    protected static function getEnumTypes()
    {
        return [ClassFromAVendor::class, SomeInterface::class];
    }
}
```

Alternatively, you can specify a prefix for each type to avoid getting FQCNs in the hash keys.

```
use Greg0ire\Enum\AbstractEnum;
use My\Namespace\SomeInterface;
use Vendor\Namespace\ClassFromAVendor;

final class MyEnum extends AbstractEnum
{
    protected static function getEnumTypes()
    {
        return [
            'prefix1' => ClassFromAVendor::class,
            'prefix2' => SomeInterface::class,
        ];
    }
}
```

### Get label from a service

[](#get-label-from-a-service)

If you want to get the constant label behind an enum value, you can instantiate the `GetLabel` class and invoke it.

```
use Greg0ire\Enum\Bridge\Symfony\Translator\GetLabel;

$label = new GetLabel();
$label(Your\Enum\Class::VALUE, Your\Enum\Class::class);
```

To enable translation, require the `symfony/translation` component and pass a `Symfony\Contracts\Translation\TranslationInterface` instance on the `GetLabel` constructor

```
use Greg0ire\Enum\Bridge\Symfony\Translator\GetLabel;
use Symfony\Contracts\Translation\TranslationInterface;

$label = new GetLabel($translator);
$label(Your\Enum\Class::VALUE, Your\Enum\Class::class);
```

If you're using Symfony, alias the service and simply inject it. If translations are enabled, the `TranslatorInterface` will be automatically injected.

```
services:
    # ...
    Greg0ire\Enum\Bridge\Symfony\Translator\GetLabel: "@greg0ire_enum.symfony.translator.get_label"
```

```
public function index(GetLabel $label)
{
    $label(Your\Enum\Class::VALUE, Your\Enum\Class::class);
    $label(Your\Enum\Class::VALUE, Your\Enum\Class::class, 'another_domain'); // Change the translation domain
    $label(Your\Enum\Class::VALUE, Your\Enum\Class::class, false); // Disable translation. In this case the class prefix wont be added
    $label(Your\Enum\Class::VALUE, Your\Enum\Class::class, false, true); // Disable translation but keep class prefix
    $label(Your\Enum\Class::VALUE, Your\Enum\Class::class, false, true, '.'); // Disable translation but keep class prefix with a custom separator
}
```

### Integration with other libraries

[](#integration-with-other-libraries)

`greg0ire/enum` integrates with other libraries. The list is available in the `suggest` section of the Composer dependency manifest.

#### Symfony validator

[](#symfony-validator)

This package provides a "ready to use" symfony validator. You have to require the `symfony/validator` package to get it working.

```
use Greg0ire\Enum\Bridge\Symfony\Validator\Constraint\Enum;
use Symfony\Component\Validator\Validation;
use Your\Namespace\EnumClass;

$validator = Validation::createValidator();

$violations = $validator->validateValue(42, new Enum(EnumClass::class));
// You can also show the constants keys on the error message:
$violations = $validator->validateValue(42, new Enum(['class' => EnumClass::class, 'showKeys' => true]));
// Enum constraint inherits from Choice constraint. You can use inherited options too:
$violations = $validator->validateValue(42, new Enum(['class' => EnumClass::class, 'strict' => true]));
```

Another example with annotations:

```
use Doctrine\Common\Annotations\AnnotationRegistry;
use Greg0ire\Enum\Bridge\Symfony\Validator\Constraint\Enum as EnumAssert;
use Symfony\Component\Validator\Validation;

class MyClass
{
    /**
     * @EnumAssert("Your\Namespace\EnumClass")
     */
    private $dummy;

    public function __construct($dummy)
    {
        $this->dummy = $dummy
    }
}

AnnotationRegistry::registerLoader('class_exists');
$validator = Validation::createValidatorBuilder()
    ->enableAnnotationMapping()
    ->getValidator();

$object = new MyClass(42);

$violations = $validator->validate($object);
```

Note: You will have to get `doctrine/annotations` and `doctrine/cache` packages to get it working.

#### Symfony form

[](#symfony-form)

This package provides a "ready to use" symfony form type. You have to require the `symfony/form` package to get it working.

```
use Greg0ire\Enum\Bridge\Symfony\Form\Type\EnumType;
use Symfony\Component\Form\Forms;
use Your\Namespace\EnumClass;

$formFactory = Forms::createFormFactory();

$view = $this->factory->create(EnumType::class, null, array(
    'class' => EnumClass::class,
))->createView();
```

#### Twig extension

[](#twig-extension)

This package comes with an `EnumExtension` Twig class. It contains a filter and some functions. You have to require the `twig/twig` package to get it working.

##### Filter

[](#filter)

The `enum_label` filter will try to return the constant label corresponding to the given value.

This filter relies on the `Greg0ire\Enum\Bridge\Symfony\Translator\GetLabel` service.

It will try to translate it if possible. To enable translation, require the `symfony/translation` component and pass a `Symfony\Contracts\Translation\TranslationInterface` instance on the `GetLabel` constructor. `GetLabel` instance will be injected on the `EnumExtension` constructor.

If translation is not available, you will have the default label with class prefixing.

Usage:

```
{{ value|enum_label('Your\\Enum\\Class') }}
{{ value|enum_label('Your\\Enum\\Class', 'another_domain') }} {# Change the translation domain #}
{{ value|enum_label('Your\\Enum\\Class', false) }} {# Disable translation. In this case the class prefix wont be added #}
{{ value|enum_label('Your\\Enum\\Class', false, true) }} {# Disable translation but keep class prefix #}
{{ value|enum_label('Your\\Enum\\Class', false, true, '.') }} {# Disable translation but keep class prefix with a custom separator #}
```

##### Functions

[](#functions)

The 3 available twig functions are ports of some `AbstractEnum` methods that can be useful in a twig template:

- `enum_get_constants` =&gt; `AbstractEnum::getConstants`
- `enum_get_keys` =&gt; `AbstractEnum::getKeys`
- `enum_get_class_prefixed_keys` =&gt; `AbstractEnum::getClassPrefixedKeys`

The arguments are exactly the same except you have to specify the targeted class first (as `enum_label` filter).

Here is a concrete example with `enum_get_constants` function:

```
{% for enum_key, enum_value in enum_get_constants('Your\\Enum\\Class') %}
    {{ enum_key }} -> {{ enum_value }}
{% endfor %}
```

##### Twig extension as a service

[](#twig-extension-as-a-service)

On Symfony projects, the extension can be autoloaded. First, you have to require the `symfony/framework-bundle` and `symfony/twig-bundle` packages, or use Symfony fullstack.

Then, register the bundle in the kernel of your application:

```
// app/AppKernel.php

public function registerBundles()
{
    $bundles = [
        // ...
        new Greg0ire\Enum\Bridge\Symfony\Bundle\Greg0ireEnumBundle(),
    ];

    // ...

    return $bundles
}
```

That's all. You can now directly use the filter.

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

[](#contributing)

see [CONTRIBUTING.md](./CONTRIBUTING.md)

Credits
-------

[](#credits)

This is a shameless rip-off of [this Stack Overflow answer](http://stackoverflow.com/a/254543/353612), with one modification: the `getConstants` method has been made public so that it is available for building choice widgets, for instance. If you want to give credit to someone for this, give it to [Brian Cline](http://stackoverflow.com/users/32536/brian-cline)

###  Health Score

46

—

FairBetter than 93% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity46

Moderate usage in the ecosystem

Community25

Small or concentrated contributor base

Maturity78

Established project with proven stability

 Bus Factor1

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

Recently: every ~289 days

Total

23

Last Release

1090d ago

Major Versions

v1.1.0 → v2.0.02016-04-25

v2.2.0 → v3.0.02016-06-03

v3.5.2 → v4.0.02017-12-02

PHP version history (5 changes)v1.1.0PHP &gt;=5.3.0

v2.0.0PHP ^5.3 || ^7.0

v3.0.0PHP ^5.6 || ^7.0

v4.0.0PHP ^7.1

v4.3.0PHP ^7.1 || ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/67cd398fd8b3fb3e756fe20dcc85bed5db3bc417ed0529932dd6befa9371fd06?d=identicon)[greg0ire](/maintainers/greg0ire)

---

Top Contributors

[![greg0ire](https://avatars.githubusercontent.com/u/657779?v=4)](https://github.com/greg0ire "greg0ire (159 commits)")[![soullivaneuh](https://avatars.githubusercontent.com/u/1698357?v=4)](https://github.com/soullivaneuh "soullivaneuh (48 commits)")[![binotaliu](https://avatars.githubusercontent.com/u/67255597?v=4)](https://github.com/binotaliu "binotaliu (3 commits)")[![laurent-bientz](https://avatars.githubusercontent.com/u/6093572?v=4)](https://github.com/laurent-bientz "laurent-bientz (2 commits)")[![StyleCIBot](https://avatars.githubusercontent.com/u/11048387?v=4)](https://github.com/StyleCIBot "StyleCIBot (2 commits)")[![cyb3rd4d](https://avatars.githubusercontent.com/u/300826?v=4)](https://github.com/cyb3rd4d "cyb3rd4d (1 commits)")[![roukmoute](https://avatars.githubusercontent.com/u/2140469?v=4)](https://github.com/roukmoute "roukmoute (1 commits)")

---

Tags

enumphpsymfonysymfony-validatortwigsymfonyvalidatorenumenumerationform

### Embed Badge

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

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

###  Alternatives

[craue/formflow-bundle

Multi-step forms for your Symfony project.

7484.0M13](/packages/craue-formflow-bundle)[a2lix/auto-form-bundle

Automate form building

873.8M11](/packages/a2lix-auto-form-bundle)[qossmic/rich-model-forms-bundle

Provides additional data mapper options that ease the use of the Symfony Form component with rich models.

218278.7k](/packages/qossmic-rich-model-forms-bundle)[boekkooi/jquery-validation-bundle

Jquery form validation bundle for symfony 2

2773.9k1](/packages/boekkooi-jquery-validation-bundle)[tales-from-a-dev/flowbite-bundle

A Symfony form theme for Flowbite

86252.8k2](/packages/tales-from-a-dev-flowbite-bundle)[nucleos/antispam-bundle

This bundle provides some basic features to reduce spam in symfony forms.

52105.1k](/packages/nucleos-antispam-bundle)

PHPackages © 2026

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