fatihirday / enummethods
PHP 枚举方法
v1.0.0
2023-02-05 18:19 UTC
Requires
- php: ^8.1
README
安装
composer require fatihirday/enummethods
示例
创建枚举
<?php use Fatihirday\EnumMethods\EnumMethods; enum HttpMethods: string { use EnumMethods; case GET = 'get'; case POST = 'post'; }
获取枚举所有名称
<?php HttpMethods::names(); // Array // ( // [0] => GET // [1] => POST // ) HttpMethods::names(operator: ', '); // GET, POST
获取枚举所有值
<?php HttpMethods::values(); // Array // ( // [0] => get // [1] => post // ) HttpMethods::values(operator: ', '); // get, post
枚举转数组
<?php HttpMethods::toArray(); // Array // ( // [GET] => get // [POST] => post // ) HttpMethods::toArray(reverse: true); // Array // ( // [get] => GET // [post] => POST // )
枚举转对象
<?php HttpMethods::toObject(); // Object // ( // get => GET // post => POST // ) HttpMethods::toObject(reverse: true); // Object // ( // get => GET // post => POST // )
枚举召唤者
<?php HttpMethods::getValueByName('GET'); // get HttpMethods::getNameByValue('get'); // GET
枚举名称检查
<?php HttpMethods::tryFromName('GET'); // True