承接 wscore/enum 相关项目开发

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

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

wscore/enum

Composer 安装命令:

composer require wscore/enum

包简介

Enumerated class.

README 文档

README

Enum and list implementation

Licence

MIT License

Installation

composer require "wscore/enum: ^1.0"

Sample Code

create a EnumList class

The intended way of creating a enumerated class is,

  1. define constants,
  2. define static $choices array.

as in the example below.

use WScore\Enum\AbstractEnum;

class EnumList extends AbstractEnum
{
    const ENUM = 'enum';
    const VALUE = 'value';
    
    protected static $choices = [
        self::ENUM => 'enumerated',
        self::VALUE => 'value',
    ];
}

The $choices variable defines available values along side with labels (i.e. human readable string).

get enumerated object

static function, enum, returns an instantiated enumerated object, which is EnumInterface

$enum = EnumList::enum(EnumList::ENUM);
(string) $enum; // enum
$enum->label(); // enumerated
$enum->is(EnumList::ENUM); // true

enum list and keys

Use static methods such as, choices(), keys(), or flipped(), to get the list of available enum values and keys, that may be used for validating inputs.

$list = EnumList::choices(); // returns list of keys and labels. 
$keys = EnumList::keys();    // returns keys of enumerated list. 
$flip = EnumList::flipped(); // returns array of key/value flipped.

subset of choices

In some use cases, you may need to restrict the selectable values. For instance, the ActiveList class below, has 3 statuses but may want to restrict to only 2 options for the end-user, as defined in in userChoice.

class ActiveList extends AbstractEnum
{
    const ACTIVE = 'active';
    const CANCEL = 'cancel';
    const HOLD   = 'hold';
    
    protected static $choices = [
        self::ACTIVE => 'activated',
        self::CANCEL => 'canceled',
        self::HOLD   => 'hold-on',
    ];

    /**
     * @return array
     */
    public static function userChoice()
    {
        return [
            self::ACTIVE => 'activated',
            self::CANCEL => 'canceled',
        ];
    }
}

To get the available choices,

$list = ActiveList::userChoice(); 
$keys = ActiveList::keys('userChoice');
$flip = ActiveList::flipped('userChoice');

really restricting use of list

To really limit choices to the subset of the original static::$choices...

use WScore\Enum\AbstractEnum;

class EnumList extends AbstractEnum
{
    const ENUM = 'enum';
    const VALUE = 'value';
    protected static $choices = [
        self::ENUM => 'enumerated',
        self::VALUE => 'value',
    ];

    /**
     * @param string $value
     * @return EnumList
     */
    public static function getUserEnum($value)
    {
        $choices = self::$choices;
        unset($choices[self::VALUE]);
        return new EnumList($value, $choices);
    }
}

then, the following code will throw an \InvalidArgumentException.

$enum = EnumList::getUserEnum(EnumList::VALUE);

统计信息

  • 总下载量: 24
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-11-05

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固