nick-denry/managed-constant-models
Composer 安装命令:
composer require nick-denry/managed-constant-models
包简介
Allow to declare constants with named attributes via separate class model
README 文档
README
Allow to declare constants with named attributes via standalone class modelInstallation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist nick-denry/managed-constant-models
or add
"nick-denry/managed-constant-models": "^0.1.1"
to the require section of your composer.json file.
Usage
-
Create managed constats model
<?php namespace app\models; use nickdenry\managedConstants\interfaces\ManagedConstantInterface; use nickdenry\managedConstants\traits\ManagedConstantTrait; /** * TaskStatus constant model */ class TaskStatus implements ManagedConstantInterface { use ManagedConstantTrait; const ACTIVE = 0; const DONE = 1; const _ATTRIBUTES = [ self::ACTIVE => [ 'class' => 'task-active', 'label' => 'Активна', ], self::DONE => [ 'class' => 'task-done', 'label' => 'Завершена', ], ]; }
-
Use it with your another model class, i.e. yii2 model
<?php namespace app\models; use app\models\TaskStatus; /** * This is the model class for table "task". * * @property int $id * @property string $title * @property int|null $created_at * @property int|null $updated_at */ class Task extends \yii\db\ActiveRecord { ... /** * Get task statuses. */ public static function getStatuses() { return new TaskStatus(); //TaskStatus()::class; } ... }
-
Get constatns with the code
<?php $constValue = Task::getStatuses()::ACTIVE; //$constValue = 0; Task::getStatuses()::ACTIVE; // ACTIVE constant; Task::getStatuses()::DONE; // DONE constant; Task::getStatuses()::constants(); // Returns array ['ACTIVE' => 0, 'DONE' => 1] Task::getStatuses()::values(); // Returns array [0, 1] Task::getStatuses()::listAttributes($constValue); // Returns array ['class' => 'task-active', 'label' => 'Активна'] Task::getStatuses()::attribute($constValue, 'class'); // Returns 'task-active' Task::getStatuses()::getList(); // Returns [ // ['id' => 0, 'class' => 'task-active', 'label' => 'Активна', ] // ['id' => 1, 'class' => 'task-done', 'label' => 'Завершена', ], // ]
统计信息
- 总下载量: 6
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-04-25