wilensky/bitmask-trait
Composer 安装命令:
composer require wilensky/bitmask-trait
包简介
Universal PHP7 trait that provides easy bitmask management and integration
README 文档
README
PHP7 bitmask modem trait provides seamless integration of bitmasks in your application.
Bitmasks are useful in case when multiple states should be assigned to a single entity or/and exist simultaneously. With help of this trait you can easily pack up to 32/64 states in a single integer like no one else.
Generic example
Lets take regular invoice to demonstrate the essence.
Assuming invoice has several states:
| State | Sent | Opened | Closed | Failed | Archived |
|---|---|---|---|---|---|
| Bit | 0 | 1 | 2 | 3 | 4 |
And related payment can have more than one state:
| State | In progress | Successful | Auth. failed | Canceled | Failed | Refused | Refunded |
|---|---|---|---|---|---|---|---|
| Bit | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
Looks like to organize states for both invoice and payment we would need 12 fields, but not with the bitmask.
What we can do is easily pack all 12 states in a single well known integer. Assumed state map can be the following:
| State | Sent | Opened | Closed | Failed | Archived | In progress | Successful | Auth. failed | Canceled | Failed | Refused | Refunded | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Bit | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
It can look the following way over time:
| Bitmask | Sent | Opened | Closed | Failed | Archived | In progress | Successfull | Auth. failed | Cancelled | Failed | Refused | Refunded | ||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Bit | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | ||||
| Invoice sent to customer | 3 | + | + | |||||||||||||
| Invoice payment initiated | 35 | + | + | + | ||||||||||||
| Payment successfull | 69 | + | + | + | ||||||||||||
| Payment refunded | 2117 | + | + | + | + | + | ||||||||||
| Payment auth. failed | 137 | + | + | + |
Basic usage
<?php use Wilensky\Traits\BitmaskTrait; class MyClass { use BitmaskTrait; // Injecting trait in your class }
For ease of use const with relevant bit positions can be created.
const INVOICE_SENT = 0; const INVOICE_OPENED = 1; // ... for the sake of brevity const PAYMENT_REFUSED = 10; const PAYMENT_REFUNDED = 11;
Creating a state
// All further code assumed inside a class scope function as trait has no public methods $state = 0; // Initial/Zero state $sent = $this->setBit( $state, // Passing initial/existing state to assign few more to self::INVOICE_SENT, // (bit 0) self::INVOICE_OPENED // (bit 1) ); // 3 (bits 0/1) // Adding `PAYMENT_INPROGRESS` state to already arranged mask with 2 states $paymentInit = $this->setBit( $sent, // Passing existing mask `3` (with 2 states, 0 and 1 bits) self::PAYMENT_INPROGRESS // (bit 5) ); // 35 (bits 0/1/5) // or the same stuff with use of another method (@see BitmaskTrait::manageMaskBit()) $paymentInit = $this->manageMaskBit($sent, self::PAYMENT_INPROGRESS, true); // Proceeding to some terminal payment state $noOpenInProgress = $this->unsetBit( $paymentInit, // 35 self::PAYMENT_INPROGRESS, // Removing `PAYMENT_INPROGRESS` state (bit 5) self::INVOICE_OPENED // among with `INVOICE_OPENED` (bit 1) ); // 1 (bits 0) // to add relevant terminal states $invoiceClosed = $this->setBit( $noOpenInProgress, // Passing mask with relevant unset states self::PAYMENT_SUCCESSFUL, // (bit 6) self::INVOICE_CLOSED // (bit 2) ); // 69 (bits 0/2/6)
Checking a state for a state
$state = 69; // Invoice closed $isInvoiceSent = $this->hasBit($state, self::INVOICE_SENT); // true $isPaymentSuccessful = $this->hasBit($state, self::PAYMENT_SUCCESSFUL); // true $isRefunded = $this->hasBit($state, self::PAYMENT_REFUNDED); // false
Checking segments
We remember that we have invoice states along with payment states in a single mask. It is useful sometimes to check whether bit passed relates to a particular segment (if such are being used) or not exceeds the mask size itself.
$invoiceSegment = [0, 4]; // Segment range as a single variable // No exception as state is in allowed range $this->isBitInRange(self::INVOICE_CLOSED, ...$invoiceSegment); // `BitAddressingException` will be thrown as payment state is not in allowed range $this->isBitInRange(self::PAYMENT_SUCCESSFUL, ...$invoiceSegment);
wilensky/bitmask-trait 适用场景与选型建议
wilensky/bitmask-trait 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.85k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2017 年 01 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「state」 「bit」 「trait」 「binary」 「bitmask」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 wilensky/bitmask-trait 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 wilensky/bitmask-trait 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 wilensky/bitmask-trait 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Doctrine implementation of the MetaborStd (Statemachine) for PHP 8.2+
A simple state dropdown field for SilverStripe forms
A state Machine library for business processes
Metamel Addresses is a polymorphic Laravel package, for address book management. You can add addresses to any eloquent model with ease.
Convert and operate with FIPS codes for states, counties, etc.
Phwoolcon Finite State Machine
统计信息
- 总下载量: 1.85k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 27
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-01-06