定制 morilog/php-enum 二次开发

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

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

morilog/php-enum

最新稳定版本:1.0.1

Composer 安装命令:

composer require morilog/php-enum

包简介

A Library for easy working with Enums in php programming language

README 文档

README

PHP-ENUM

There is not any ENUM in php programming language. BTW every programmer may need to this feature.

In this library i provide the simple and easy way for working with enums.

Installation

Run bellow command:

composer require morilog/php-enum

Usage

Writing new Enums

Create a class that extends the \Morilog\PhpEnum\Enum class

Constants keys must be all UPPER_CASE and separate words by _ (underscore)

Constants values can be all scalar values such as string, integer, boolean, etc.

<?php

use Morilog\PhpEnum\Enum;

final class CommentStatus extends Enum
{
    const APPROVED = 1;
    const REJECTED = 2;
    const SOMETHING_ELSE = 3;
}
Creating enum instances
By class constructor
<?php

$status = new CommentStatus(1);
$status->key(); // APPROVED
$status->value(); // 1
$status->isApproved(); // true
$status->isRejected(); // false
By camelCase constant name
<?php

$status = CommentStatus::rejected();
$status->key(); // REJECTED
$status->value(); // 2
$status->isApproved(); // false
$status->isRejected(); // true

$other = CommentStatus::somethingElse();
$other->key(); // SOMETHING_ELSE
$other->value(); // 3
$other->isApproved(); // false
$other->isRejected(); // false
$other->isSomethingElse(); // true
By fromKey() method
<?php

$status = CommentStatus::fromKey('APPROVED');

By fromValue() method

<?php

$status = CommentStatus::fromValue(1);

Static methods

<?php

CommentStatus::keys(); // [APPROVED, REJECTED, SOMETHING_ELSE]

CommentStatus::values(); // [1, 2, 3]

CommentStatus::toArray(); // [APPROVED => 1, REJECTED => 2, SOMETHING_ELSE => 3]

Comparison enums

<?php

$first = CommentStatus::approved();
$second = CommentStatus::rejected();
$third = CommentStatus::approved();

$first->equalsTo($second); // false
$first->equalsTo($third); // true

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Unknown
  • 更新时间: 2019-08-14