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

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

tigrov/yii2-enum
================

Enum type behavior for Yii2 based on class constants

1.2.1(7y ago)244.0k↓50%1MITPHPPHP &gt;=5.5.0

Since May 30Pushed 7y ago1 watchersCompare

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

READMEChangelogDependencies (1)Versions (8)Used By (1)

yii2-enum
=========

[](#yii2-enum)

Enum type behavior for Yii2 based on class constants.

[![Latest Stable Version](https://camo.githubusercontent.com/3432a0d716c302183ff6b4a9d3aa44b8896040a31eb3a25adce6c0ce7f85cca5/68747470733a2f2f706f7365722e707567782e6f72672f546967726f762f796969322d656e756d2f762f737461626c65)](https://packagist.org/packages/Tigrov/yii2-enum)[![Build Status](https://camo.githubusercontent.com/b6c3f981a115d95c316af7a7fbac1890a812e0103a5d9e909d2ddf5ac90c5f20/68747470733a2f2f7472617669732d63692e6f72672f546967726f762f796969322d656e756d2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/Tigrov/yii2-enum)

Limitation
----------

[](#limitation)

Since 1.1.0 requires PHP &gt;= 5.5

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

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
php composer.phar require --prefer-dist tigrov/yii2-enum

```

or add

```
"tigrov/yii2-enum": "~1.0"

```

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

Usage
-----

[](#usage)

Once the extension is installed, you can create an enum behavior as follow:

```
class Status extends \tigrov\enum\EnumBehavior
{
    const ACTIVE = 'active';
    const PENDING = 'pending';
    const REJECTED = 'rejected';
    const DELETED = 'deleted';

    /** @var array list of attributes that are to be automatically humanized value */
    public $attributes = ['status' => 'status_key'];

    /** @var string|null a message category for translation the values */
    public static $messageCategory = 'status';
}
```

Create a table with the enum field

```
\Yii::$app->getDb()->createCommand()
    ->createTable('model', [
       'id' => 'pk',
       'status_key' => 'string',
   ])->execute();
```

Create a model for the table

```
class Model extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            Status::class,
            // 'status' => [
            //     'class' => Status::class,
            //     'attributes' => ['status' => 'status_key'],
            // ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['status_key'], 'in', 'range' => Status::codes()],
        ];
    }
}
```

and then use them in your code

```
/**
 * @var ActiveRecord $model
 */
$model = new Model;
$model->status_key = Status::PENDING;

// The field 'status' has humanize and translated value, see \yii\helpers\Inflector::humanize($word, true)
$model->status; // is 'Pending' or translated value

// To get all enum values
Status::values();

// To get a display value
Status::value(Status::PENDING); // is 'Pending' or translated value
```

Examples
--------

[](#examples)

Gender codes:

```
class GenderEnum extends \tigrov\enum\EnumBehavior
{
    const MALE = 'M';
    const FEMALE = 'F';

    /**
     * @var array list of attributes that are to be automatically humanized value
     * humanized => original attribute
     */
    public $attributes = ['gender' => 'gender_code'];

    /** @var string|null a message category for translation the values */
    public static $messageCategory = 'gender';

    /**
    * Returns value for empty attribute value
    * @return string|null
    */
    public static function emptyValue()
    {
        return static::t('Unspecified');
    }
}

class Model extends \yii\db\ActiveRecord
{
    public function behaviors()
    {
        return [
            GenderEnum::class,
        ];
    }

    public function rules()
    {
        return [
            [['gender_code'], 'in', 'range' => GenderEnum::codes()],
        ];
    }
}

$model->gender_code = GenderEnum::MALE; // is 'M'

// The field 'gender' has humanize and translated value
$model->gender; // is 'Male' or translated value

$model->gender_code = null;
$model->gender; // is 'Unspecified' or translated value. @see GenderEnum::emptyValue()
```

Messenger names:

```
class MessengerEnum extends \tigrov\enum\EnumBehavior
{
    const SKYPE = 'skype';
    const WHATSAPP = 'whatsapp';
    const VIBER = 'viber';
    const FACEBOOK = 'facebook';
    const IMESSAGE = 'imessage';
    const TELEGRAM = 'telegram';
    const LINE = 'line';
    const JABBER = 'jabber';
    const QQ = 'qq';
    const BLACKBERRY = 'blackberry';
    const AIM = 'aim';
    const EBUDDY = 'ebuddy';
    const YAHOO = 'yahoo';
    const OTHER = 'other';

    /** @var array list of attributes that are to be automatically humanized value */
    public $attributes = ['type' => 'type_key'];

    /**
     * Values of Messengers
     * @param bool $withEmpty with empty value at first
     * @return array
     */
    public static function values($withEmpty = false)
    {
        $values = parent::values($withEmpty);

        // Correct some values
        $values['whatsapp'] = 'WhatsApp';
        $values['imessage'] = 'iMessage';
        $values['qq'] = 'QQ';
        $values['blackberry'] = 'BlackBerry';
        $values['aim'] = 'AIM';
        $values['ebuddy'] = 'eBuddy';
        $values['other'] = \Yii::t('enum', 'Other'),

        return $values;
    }
}

$model->type_key = MessengerEnum::WHATSAPP; // is 'whatsapp'
$model->type; // is 'WhatsApp'
```

License
-------

[](#license)

[MIT](LICENSE)

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity29

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity63

Established project with proven stability

 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

Every ~87 days

Recently: every ~98 days

Total

7

Last Release

2744d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/77d285672dec831b7ce5746ab8bb1dd20fffe81fb9d11c232534686706de9697?d=identicon)[Tigros](/maintainers/Tigros)

---

Top Contributors

[![Tigrov](https://avatars.githubusercontent.com/u/8563175?v=4)](https://github.com/Tigrov "Tigrov (18 commits)")

---

Tags

enumyii2-enumyii2-extensionenumyii2extension

### Embed Badge

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

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

###  Alternatives

[dmstr/yii2-cookie-consent

Yii2 Cookie Consent Widget

1452.6k](/packages/dmstr-yii2-cookie-consent)[richardfan1126/yii2-js-register

Yii2 widget to register JS into view

1357.2k7](/packages/richardfan1126-yii2-js-register)

PHPackages © 2026

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