wilensky/bitmask-trait 问题修复 & 功能扩展

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

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

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 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 1.85k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 27
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-01-06