承接 nick-denry/managed-constant-models 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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 model

Latest Stable Version Total Downloads Latest Unstable Version License

Installation

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

  1. 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' => 'Завершена',
            ],
        ];
    
    }
  2. 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;
        }
        ...
    
    }
  1. 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

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-04-25

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固