brysem/phpenums
Composer 安装命令:
composer require brysem/phpenums
包简介
Enums made simple in PHP.
README 文档
README
Enums made simple in PHP. Providing you with an easy to use interface.
<?php // Assume that this model will now give us access to the property "status" // which has been set to an instance of the UserStatus enum class. $user = new Models\User([ 'status' => new UserStatus(UserStatus::ACTIVE) ]); echo("Hello, your account is currently ". $user->status ."."); // __toString() -> Hello, your account is currently Active. // Get all values. UserStatus::all(); // [1 => 'Pending', 2 => 'Active', 3 => 'Banned'] // Get all keys from the enum. UserStatus::keys(); // [1, 2, 3] $userStatus = new UserStatus(UserStatus::ACTIVE); $userStatus->values(); // [1 => 'Pending', 2 => 'Active', 3 => 'Banned'] // Cast to a string. (string) $user->status; // Active // Perform checks $user->status->is(UserStatus::ACTIVE); // true $user->status->is([UserStatus::ACTIVE, UserStatus::PENDING]); // true (checkes whether the status is active or pending.) $user->status->has(UserStatus::PENDING); // true $user->status->has(1337); // false // Set and get the value. $user->status->value(); // 2 $user->status->set(UserStatus::ACTIVE); // Active $user->status->get(UserStatus::ACTIVE); // Active // Safegaurding against invalid values try { $user->status->set(1337); $user->status->set('undefined'); } catch (UndefinedEnumValueException $e) { // } catch (InvalidArgumentException $e) { // }
Installation
PHP Enums can be installed with Composer.
$ composer require brysem/phpenums
{
"require": {
"brysem/phpenums": "1.*"
}
}
<?php require 'vendor/autoload.php'; use Bryse\Enums\Enum; class UserStatus extends Enum { const PENDING = 1; const ACTIVE = 2; const BANNED = 3; /** * Returns all possible key value pairs for the enum. * * @return array */ public function values() { return [ self::PENDING => 'Pending', self::ACTIVE => 'Active', self::BANNED => 'Banned', ]; } }
Laravel Example
<?php namespace App\Models; use App\Models\Enums\UserType; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { public function getTypeAttribute() { return new UserType($this->attributes['type']); } public function setTypeAttribute($value) { $this->attributes['type'] = (new UserType($value))->value(); } }
You can now easily do the following.
$user->status = UserType::ACTIVE;
Features
- Automatic string casting.
- Useful checks and comparisons.
- Easy to use to any framework or even a plain PHP file.
- PSR-4 autoloading compliant structure.
- Unit-Testing with PHPUnit.
Contributing
Thank you for considering contributing to PHP Enums. Any and all help is appreciated. Please do not hesitate to send a pull request.
License
The PHP Enums package is open-sourced software licensed under the MIT license.
brysem/phpenums 适用场景与选型建议
brysem/phpenums 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 176.84k 次下载、GitHub Stars 达 10, 最近一次更新时间为 2017 年 07 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「model」 「value」 「enums」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 brysem/phpenums 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 brysem/phpenums 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 brysem/phpenums 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Enums for Laravel Enso
Shared backed enums classifying HTML elements by category (block, inline, list, table, void, metadata, root) for the UI Awesome ecosystem.
Runn Me! Value Objects Library
Helpers for handling enums and enum translations in Laravel
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
A Laravel package that adds extra power to your PHP enums, and lets you use them in your frontend with type definitions.
统计信息
- 总下载量: 176.84k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 10
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-07-09