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

ActiveYii2-extension[Utility &amp; Helpers](/categories/utility)

roaresearch/yii2-enum
=====================

Enum classes for Yii2 models and forms

1.0.0(3y ago)012BSD-3-ClausePHPPHP &gt;=8.1.0

Since Jun 14Pushed 3y ago2 watchersCompare

[ Source](https://github.com/ROAResearch/yii2-enum)[ Packagist](https://packagist.org/packages/roaresearch/yii2-enum)[ Docs](http://www.yiiframework.com/)[ RSS](/packages/roaresearch-yii2-enum/feed)WikiDiscussions main Synced 4w ago

READMEChangelog (1)Dependencies (1)Versions (2)Used By (0)

ROAResearch Yii2 Enum
=====================

[](#roaresearch-yii2-enum)

[![Latest Stable Version](https://camo.githubusercontent.com/7041dcee8d72b58cfc2cb9a32f8d06976a8dad2887fa8f697779c1c994e15d69/68747470733a2f2f706f7365722e707567782e6f72672f524f4152657365617263682f796969322d656e756d2f762f737461626c65)](https://packagist.org/packages/ROAResearch/yii2-enum) [![Total Downloads](https://camo.githubusercontent.com/2e6bc93972e773f5070379c7faf47d64652a0422b369102a92b61746f048ee18/68747470733a2f2f706f7365722e707567782e6f72672f524f4152657365617263682f796969322d656e756d2f646f776e6c6f616473)](https://packagist.org/packages/ROAResearch/yii2-enum) [![Latest Unstable Version](https://camo.githubusercontent.com/36bf51dce075331fd5d84adf26b2ce1fa1462916655f1218e1d22631f3a1a80a/68747470733a2f2f706f7365722e707567782e6f72672f524f4152657365617263682f796969322d656e756d2f762f756e737461626c65)](https://packagist.org/packages/ROAResearch/yii2-enum) [![License](https://camo.githubusercontent.com/3737c49426e0b3debac4d6e95a5c4d83c3e717a9e9fa8d0e56b00469a11e269b/68747470733a2f2f706f7365722e707567782e6f72672f524f4152657365617263682f796969322d656e756d2f6c6963656e7365)](https://packagist.org/packages/ROAResearch/yii2-enum)

ROAResearch Yii2 Enum extension provides support for the ussage of enumarions in Yii2 models and forms.

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

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/). Check the [composer.json](https://github.com/ROAResearch/yii2-enum/blob/master/composer.json) for this extension's requirements and dependencies.

To install, either run

```
$ php composer.phar require roaresearch/yii2-enum "*"

```

or add

```
"roaresearch/yii2-enum": "*"

```

to the `require` section of your `composer.json` file.

Usage
-----

[](#usage)

### Create Enums

[](#create-enums)

This library uses the enum feature added in php8.1, to use them you need to implement the `roaresearch\yii2\enum\DescriptiveEnum` interface

```
use roaresearch\yii2\enum\{DescriptiveEnum, DescriptiveEnumTrait}

enum UserStatus: int implements DescriptiveEnum
{
    use DescriptiveEnumTrait;

    case Pending = 1;
    case Active = 2;
    case Banned = 3;
}
```

By default the `DescriptiveEnumTrait` defines all the required methods including automatically trying to translate the case names for the enum to make them human readable. For example in the case above

```
UserStatus::Pending->getDesc();

// is a shorcut for

Yii::t('UserStatus', 'Pending');
```

### Declare Enum for a Model Property

[](#declare-enum-for-a-model-property)

To use the full power of this library its necessary to create models that implement the `roaresearch\yii2\enum\models\EnumMap` interface.

```
use roaresearch\yii2\enum\{DescriptiveEnum, models\EnumMap, models\EnumMapTrait};
use common\enums\UserStatus;

class User extends ActiveRecord implements EnumMap
{
    use EnumMapTrait;

    public static function enums(): array
    {
        return [
            `status` => UserStatus::class,
        ];
    }

    /**
     * @return string human readable and translated description of the status
     */
    public function getStatusDesc(): ?string
    {
        return $this->tryEnum('status')?->getDesc();
    }
}
```

The method `getStatusDesc()` is optional, its useful to easily access the human readable description of the status.

This way other tools like the validators and widgets can easily interact with the model.

### Validator

[](#validator)

You can validate an attribute value can be extracted from an enum using the provided validator

```
use roaresearch\yii2\enum\Validator as EnumValidator;

public function rules()
{
    return [
        ['status', EnumValidator::class],
    ];
}
```

More informations for [enums](https://php.watch/versions/8.1/enums)
-------------------------------------------------------------------

[](#more-informations-for-enums)

Upgrade from [Farystha](https://github.com/Faryshta/yii2-enum) library
----------------------------------------------------------------------

[](#upgrade-from-farystha-library)

- Models must implement the `EnumMap` interface.
- Models have no longer the method `getAttributeDesc()`, lost in favor of using `null` safe operator
- Models `enums()` method no longer defines the enums themselves but a map for the enum classes. Method signature changed too.
- Validator `$skipOnEmpty` is set to `true`.
- Validator will throw exception if the provided model is not an `EnumMap` or has no mapped enum for the validated attributes.

License
-------

[](#license)

**ROAResearch Yii2 Enum** is released under the BSD 3-Clause License. See the bundled `LICENSE.md` for details.

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity56

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

1425d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/2341d88f3cdea0c2474cfbf59e5cf6dab5dd6a026d7846fabf219f2a93be1641?d=identicon)[neverabe](/maintainers/neverabe)

---

Top Contributors

[![Faryshta](https://avatars.githubusercontent.com/u/2029247?v=4)](https://github.com/Faryshta "Faryshta (2 commits)")

---

Tags

enumyii2

### Embed Badge

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

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

###  Alternatives

[faryshta/yii2-enum

Enum classes for Yii2 models and forms

15332.9k](/packages/faryshta-yii2-enum)[dmstr/yii2-cookie-consent

Yii2 Cookie Consent Widget

1452.6k](/packages/dmstr-yii2-cookie-consent)

PHPackages © 2026

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