vijinho/enums 问题修复 & 功能扩展

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

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

vijinho/enums

Composer 安装命令:

composer require vijinho/enums

包简介

PHP7 Enum Implementation

README 文档

README

Travis CI Build Status Code Coverage Scrutinizer Code Quality

This is an implementation for ENUM types in PHP7 which lives on github at vijinho/php7-enums. It's a little different from the other implementations I saw which didn't quite fit my needs and it uses quite a bit of PHP7 magic and overloading to achieve the results I wanted.

Quick Start

Real-world example

class Storage extends Enum
{
    protected static $caseSensitive = true;
    protected static $capitalize = true;

    protected static $values = [
        'BIT' => 1,
        'BYTE' => 8,
        'KILOBYTE' => 8 * 1024,
        'GIGABYTE' => 8 * 1024 * 1024 * 1024,
        'TERABYTE' => 8 * 1024 * 1024 * 1024 * 1024,
    ];
}

// add definition of a megabyte in bits
$s = new Storage;
$s(['MEGABYTE' => 1024 * Storage::KILOBYTE()]);
echo $s;

// get definition of 8 bits
$name = $s->key(8);
echo $name; // BYTE

echo $s->GIGABYTE; // 8589934592
echo $s::KILOBYTE(); // 8192
echo $s->value('TERABYTE'); // 8796093022208
echo Storage::value('BYTE'); // 8
echo Storage::BYTE(); // 8

Using Enum without extending it

I do not advise you to do this because we're using static class members.

use vijinho\Enums\Enum;

$e = new Enum(); // new empty enum
$e([
    'mercedes' => 'luxury',
    'ferrari' => 'sports',
    'BMW'
]);
echo $e; // outputs to JSON serialized string by magic!

/*
{
    "mercedes": "luxury",
    "ferrari": "sports",
    "BMW": "BMW"
}
*/

$e->add(['BMW' => 'Bob Marley & The Wailers']); // cannot override existing value
echo $e->value('BMW'); // BMW
$e->capitalize(true);
$e->add('Audi');
echo $e;

/*
{
    "MERCEDES": "luxury",
    "FERRARI": "sports",
    "BMW": "BMW",
    "AUDI": "Audi"
}
*/

echo $e->MERCEDES; // luxury
echo $e->FERRARI(); // sports
echo Enum::AUDI(); // Audi

// add non-string value (array)
echo "Example 17\n";
$e(['trabant' => ['Germany', 'Eastern Europe']]);

// get key by non-string
echo $e->key(['Germany', 'Eastern Europe']); // trabant

This is how it ought to be used:

use vijinho\Enums\Enum;

class Fruits extends Enum
{
    protected static $values = [
        'apple' => 'Apple',
        'pear' => 'Pear',
        'banana' => 'Banana',
        'orange' => 'Orange',
        'grapefruit' => 'Grapefruit',
        'tomato' => 'Cucumber',
    ];
}

### Using ENUM statically

// get an enum value by key
echo Fruits::apple(); // Apple
echo Fruits::APPLE(); // Apple

// add a key => value to the enum
Fruits::add([
	'STRAWBERRY' => 'Strawberry',
    'Avocado' => 'Avocado'
]);
// alternative way to fetch a value by key
echo Fruits::value('strawberry'); // Strawberry

// return the key for a value
echo Fruits::key('cucumber'); // tomato

// return all fruits
print_r(Fruits::values());
/*
(
    [apple] => Apple
    [pear] => Pear
    [banana] => Banana
    [orange] => Orange
    [grapefruit] => Grapefruit
    [tomato] => Cucumber
    [STRAWBERRY] => Strawberry
    [Avocado] => Avocado
)
*/

### Using ENUM as an object

Continuing from above...

$f = new Fruits;
$f(['mango']); // add a new fruit - magic!
$f(['pineapple' => 'Pineapple']); // add another new fruit
$f->add(['potato' => 'Not a fruit']);
var_dump($f); // special var_dump magic!

object(Fruits)#5 (5) {
  ["overwrite"]=>
  bool(false)
  ["delete"]=>
  bool(false)
  ["capitalize"]=>
  bool(false)
  ["caseSensitive"]=>
  bool(false)
  ["values"]=>
  array(11) {
    ["apple"]=>
    string(5) "Apple"
    ["pear"]=>
    string(4) "Pear"
    ["banana"]=>
    string(6) "Banana"
    ["orange"]=>
    string(6) "Orange"
    ["grapefruit"]=>
    string(10) "Grapefruit"
    ["tomato"]=>
    string(8) "Cucumber"
    ["STRAWBERRY"]=>
    string(10) "Strawberry"
    ["Avocado"]=>
    string(7) "Avocado"
    ["mango"]=>
    string(5) "mango"
    ["pineapple"]=>
    string(9) "Pineapple"
    ["potato"]=>
    string(11) "Not a fruit"
  }
}

### Using ENUM as an array directly

Implements PHP ArrayAccess interface

// create a new enum $e
echo "Example 1\n";
$e = new Enum(['apple', 'pear', 'peach']);

// retrieve apple using array access
echo $e['apple']; // apple

// retrieve apple using array access
echo "Example 2\n";
echo isset($e['pear']); // 1

// remove a value
unset($e['pear']);
echo $e;

/*
{
    "apple": "apple",
    "peach": "peach"
}
*/

More Usage Examples

The class uses static members, so though it can be instantiated with new there are some caveats detailed in the examples folder:

  • Using enums as an object
  • Using static enums.
  • Strict usage example of strict enums (case-sensitive, capitalized key)

Installation

Add to your composer.json the following:

"vijinho/enums": "dev-dev-master"

Then composer update to get it.

then import to the top of your PHP script with:

use \vijinho\Enums\Enum;

Vijay Mahrra

vijinho/enums 适用场景与选型建议

vijinho/enums 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 23.74k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2016 年 08 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「enum」 「enums」 「PHP7」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 vijinho/enums 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 vijinho/enums 我们能提供哪些服务?
定制开发 / 二次开发

基于 vijinho/enums 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: GPL-3.0
  • 更新时间: 2016-08-21