codekandis/phlags 问题修复 & 功能扩展

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

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

codekandis/phlags

Composer 安装命令:

composer require codekandis/phlags

包简介

Introduces the possibility to use flagable enums in PHP.

README 文档

README

Version License Minimum PHP Version Code Coverage

With Phlags you can declare flagable enums to provide types with varying and multiple states. While depending on binary operations Phlags provides high performance and reliabilty.

Index

Installation

Install the latest version with

$ composer require codekandis/phlags

How to use

Example: Simple permissions in a file system

Declaration

Declare a class extending the flagable base class AbstractFlagable.

class Permissions extends AbstractFlagable
{
    public const READ    = 1;
    public const WRITE   = 2;
    public const EXECUTE = 4;
}

General Hints

In the context of manipulating the flagable the following values are supposed to be equal and can similarly passed to all methods of the flagable.

1
Permissions::READ
new Permissions( 1 )
new Permissions( Permission::READ )
new Permissions( 'READ' );

In the other hand the type restriction of PHP does not allow any combination of an integer value with a string with a flagable.

new Permission( 1 | 'READ' | new Permissions( READ ) )

Instantiation

You can easily instantiate your flagable in different ways.

// with the default flag 'Permissions::NONE' (inherited from `FlagableInterface::NONE`)
$permissions = new Permissions();

// with a single flag
$permissions = new Permissions( Permissions::READ );

// with multiple flags
$permissions = new Permissions( Permissions::READ | Permissions::WRITE );

// with another flagable
$permissions = new Permissions( new Permissions( Permissions::READ ) );

// with string representations
$permissions = new ( 'READ' );
$permissions = new ( 'READ|WRITE' );
$permissions = new ( 'READ|WRITE|EXECUTE' );

// with mixed string representations
$permissions = new ( '1' );
$permissions = new ( '1|2' );
$permissions = new ( '1|WRITE|4' );

Reading

You can read the value of the flagable in 2 different ways.

$permissions = new Permissions( Permissions::READ );
echo $permissions->getValue();  // 1
echo $permissions();            // 1

Determination

You can determine if one or more specific flags have been set.

$permissions = new Permissions( Permissions::READ | Permissions::WRITE );
$permissions->has( Permissions::READ );     // true
$permissions->has( Permissions::WRITE );    // true
$permissions->has( Permissions::EXECUTE );  // false

Manipulation

You can set, unset and switch flags.

$permissions = new Permissions();
$permissions->set( Permissions::READ );
$permissions->unset( Permissions::READ );
$permissions->switch( Permissions::READ );
$permissions->has( Permissions::READ );     // true

Fluent Manipulation

The base class AbstractFlagable implements the fluent interface. So the manipulation of the flagable can be chained.

$permissions = new Permissions();
$permissions->set( Permissions::READ )
            ->unset( Permissions::READ )
            ->switch( Permissions::READ )
            ->has( Permissions::READ );    // true

String Representation

A flagable can stringified in different ways with different outputs.

$permissions = new Permissions();
(string)$permissions->getValue();  // 0
(string)$permissions();            // 0
(string)$permissions;              // NONE
$permissions->__toString();        // NONE

$permissions = new Permissions( PERMISSIONS::READ | PERMISSIONS::EXECUTE );
(string)$permissions->getValue();  // 5
(string)$permissions();            // 5
(string)$permissions;              // READ|EXECUTE
$permissions->__toString();        // READ|EXECUTE

Traitful Extensions

To keep the simplicity and performance Phlags provides Traitful Extensions. Instead of implementing a complex and heavyweight inheritance you can combine the extensions of your choice into the flagable of your needs.

class Permissions extends AbstractFlagable SomeTraitfulInterface
{
    use SomeTraitfulExtension;

    public const READ    = 1;
    public const WRITE   = 2;
    public const EXECUTE = 4;
}

Conditional Manipulation

ConditionalManipulationExtension

The Conditional Manipulation provides you with methods to set, unset and switch a flag value while a passed statement must evaluate to true.

$pathToFile = '/some-random-file.txt';
$permissions = new Permissions();
$permissions->ifSet( Permissions::DIRECTORY, is_dir( $pathToFile ) );
$permissions->has( Permissions::DIRECTORY );  // false

$pathToFile = '/some-random-directory';
$permissions = new Permissions();
$permissions->ifSet( Permissions::DIRECTORY, is_dir( $pathToDirectory ) );
$permissions->has( Permissions::DIRECTORY );  // true

Validation

Flagables

While instantiating your very first flagable your flagable has to pass a one-time validation.

  • all declared constants are an unsigned integer
  • all constants are a power of 2
  • there is no duplicates of any of the constant values
  • there is no missing values, e. g. a flagable with a flags set 1, 2, 8 ist invalid, while the flag 4 is missing

If the flagable does not pass the validation an InvalidFlagableException will be thrown and you can retreive an array of detailed error messages of the validation.

try
{
    $permissions = new Permissions();
}
catch ( InvalidFlagableException $e )
{
    $errorMessages = $e->getErrorMessages();
}

Values

A flag value passed to the methods of the flagable has to pass a validation on every method call.

  • it is an unsigned integer less or equal than the maximum value of the called flagable
  • it is a string representation of a flagable with an identic type as the type of the called flagable
  • it is a flagable with an identic type as the type of the called flagable
  • it does not exceeds the maximum flag value of the called flagable

If the value does not pass the validation an InvalidValueException will be thrown and you can retreive an array of detailed error messages of the validation.

try
{
    $permissions->set( $value );
}
catch ( InvalidValueException $e )
{
    $errorMessages = $e->getErrorMessages();
}

codekandis/phlags 适用场景与选型建议

codekandis/phlags 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 762 次下载、GitHub Stars 达 3, 最近一次更新时间为 2019 年 07 月 02 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「flags」 「enums」 「flagable」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 codekandis/phlags 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 codekandis/phlags 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 762
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 8
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-07-02