定制 githubjeka/enum 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

githubjeka/enum

Composer 安装命令:

composer require githubjeka/enum

包简介

Simple and base functional to start using Enum in your projects.

关键字:

README 文档

README

Build Status

Usage ENUM

Simple and base functional to start using Enum in your projects. Just initialize enum value by creating Enum class.

final class SizeEnum extends \githubjeka\enum\BaseObjectEnum
{
    const XS = 'xs';
    const SMALL = 's';
    const MEDIUM = 'm';
    const LARGE = 'l';
    const XL = 'xl';
    const XXL = 'xxl';

    public static function getList(): array
    {
        return [
            self::XS => 'Extra small(xs)',
            self::SMALL => 'Small',
            self::MEDIUM => 'Medium',
            self::LARGE => 'Large',
            self::XL => 'Extra large(xl)',
            self::XXL => 'Extra extra large(xxl)',
        ];
    }
}

After that you can use API enums as:

SizeEnum::getList()

Return ready list for use in HTML list elements like checkboxes, select or others

SizeEnum::getKeys()

Return values of ENUM for use it in validation rules like Assertion::inArray('xs', SizeEnum::getKeys());;

SizeEnum::XXL

Return string key. Use to compare with the saved values (bool)('xxl' === SizeEnum::XXL)?>;

SizeEnum::getLabel(SizeEnum::LARGE)

Return human description of value. InvalidArgumentException will be returned if value is wrong.

SizeEnum::isValid('xl')

Use to avoid InvalidArgumentException exception.

new SizeEnum('xxl')

Return Object enum.

class Shirt
{
    private $size;

    public function __construct(SizeEnum $size)
    {
        $this->size = $size;
    }

    public function size(): SizeEnum
    {
        return $this->size;
    }
}

$sizeFromDb = 'xxl';
$size = new SizeEnum($sizeFromDb);
$shirt = new Shirt($size);

$shirt->size()->asKey(); // (string) xxl 

$shirt->size()->asLabel(); // (string) Extra extra large(xxl)

$shirt->size()->equals(new SizeEnum(SizeEnum::XS)); // (bool) false

echo $shirt->size(); // (string) xxl

Extra

Strongly recommend that you do not use numbers as enumeration values:

const XS = '0';         // not recommend
const SMALL = '1';      // not recommend
const MEDIUM = 2;       // not recommend
const LARGE = 3;        // not recommend

because it is easy to mix up the strings and the underlying number values if you quote the ENUM values incorrectly.

By default expected that all Enum(constants) are Strings.

If you want to use integers you can change $stringMode property to FALSE in your Enum class. After that should use only integers value for constants:

const XS = 0;
const SMALL = 1;
const MEDIUM = 2;
const LARGE = 3;

If you use MySQL ENUM see limits also

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-07-17

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固