xakepehok/enum-helper 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

xakepehok/enum-helper

最新稳定版本:0.1.3

Composer 安装命令:

composer require xakepehok/enum-helper

包简介

Enum Helper with switch-case alternative and more

README 文档

README

\XAKEPEHOK\EnumHelper\EnumHelper - helper, that can help you work with enum-values (Value-objects) with few helpful functions, such as switch-case, guard invalid value, get all values or don't forget use all enum values as some array keys

Usage

Define you enum class, for example, define ValueObject class Status (ValueObject is not necessary, you can not define constructor and getter, and use your class as simple helper, but much cases it very useful)

<?php

class Status extends \XAKEPEHOK\EnumHelper\EnumHelper
{
    
    const STATUS_PROCESSING = 'processing';
    const STATUS_APPROVED = 'approved';
    const STATUS_CANCELED = 'canceled';

    private $status;

    public function __construct($status) 
    {
        self::guardValidValue($status);
        $this->status = $status;
    }

    public  function getStatus()
    {
        return $this->status;
    }
    
    public static function values() : array{
        return [
            self::STATUS_PROCESSING,
            self::STATUS_APPROVED,
            self::STATUS_CANCELED,
        ];
    }    
}

After that you can use your Status object as your domain type. But this helper provide more futures.

Switch case

Your can easy forgot use one of you status in native php switch-case. Our helper provide alternative switch-case way:

<?php
$statusValue = Status::STATUS_PROCESSING;

$result = Status::switchCase($statusValue, [
    Status::STATUS_PROCESSING => function ($case) {
        //doSomethins();
        return null; //
    },
    Status::STATUS_APPROVED => function ($case) {
        return true;
    },
    Status::STATUS_CANCELED => false,
]);

In this function you should provide all your enum values as keys (if you forgot something, exception \XAKEPEHOK\EnumHelper\Exception\ForgottenSwitchCaseException will be thrown), and use values or callable as result. For more details see EnumHelper code

Associative

Can help you define associative array with your enum as array keys. If you forgot something, exception \XAKEPEHOK\EnumHelper\Exception\NotEqualsAssociationException` will be thrown

<?php
$array = Status::associative([
    Status::STATUS_PROCESSING => 0,
    Status::STATUS_APPROVED => 1,
    Status::STATUS_CANCELED => -1,
]);

Is valid

<?php
Status::isVlaid(Status::STATUS_PROCESSING); //true
Status::isVlaid('qwerty'); //false

Guard valid value

Throw \XAKEPEHOK\EnumHelper\Exception\OutOfEnumException exception if not enum value passed

<?php
Status::guardValidValue(Status::STATUS_PROCESSING); //OK
Status::guardValidValue('qwerty'); //throw exception

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2019-07-31

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固