viijnho / enums
PHP7 枚举实现
1.0.2
2017-10-20 12:20 UTC
Requires
- php: >=5.6
This package is not auto-updated.
Last update: 2024-09-27 01:28:41 UTC
README
这是一个在github上生活的PHP7枚举类型的实现,地址是vijinho/php7-enums。它与其他实现略有不同,那些实现并没有完全满足我的需求,并且它使用了相当多的PHP7 魔术方法和重载来达到我想要的结果。
快速开始
真实世界示例
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
不扩展枚举使用枚举
我建议你不要这样做,因为我们使用的是静态类成员。
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
这就是它的使用方式
use vijinho\Enums\Enum; class Fruits extends Enum { protected static $values = [ 'apple' => 'Apple', 'pear' => 'Pear', 'banana' => 'Banana', 'orange' => 'Orange', 'grapefruit' => 'Grapefruit', 'tomato' => 'Cucumber', ]; }
### 使用枚举静态
// 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 ) */
### 将枚举作为对象使用
从上面的内容继续...
$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" } }
### 直接将枚举作为数组使用
// 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"
}
*/
更多使用示例
该类使用静态成员,所以尽管可以用new
关键字实例化,但在示例文件夹中有一些需要注意的问题。
安装
将以下内容添加到您的composer.json
文件中
"vijinho/enums": "dev-dev-master"
然后执行composer update
以获取它。
然后在您的PHP脚本顶部导入
use \vijinho\Enums\Enum;