PHP code example of roaresearch / yii2-enum

1. Go to this page and download the library: Download roaresearch/yii2-enum library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

roaresearch / yii2-enum example snippets


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

enum UserStatus: int implements DescriptiveEnum
{
    use DescriptiveEnumTrait;

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

UserStatus::Pending->getDesc();

// is a shorcut for

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


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();
    }
}

use roaresearch\yii2\enum\Validator as EnumValidator;

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

$ php composer.phar