ornament/bitflag 问题修复 & 功能扩展

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

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

ornament/bitflag

Composer 安装命令:

composer require ornament/bitflag

包简介

Bitflag decorator for Ornament ORM

README 文档

README

Bitflag decorator for Ornament ORM

For a model Foo with a property 'status', we often want to define a number of bitflags, e.g. 'on = 1', 'valid = 2' etc. The Bitflag decorator makes this easy.

Create a backed enum defining the desired flags. Type hint the property to receive the Bitflag as such, and add the attribute Ornament\Bitflag\Options to said property. The argument to Options is the classname of your enum.

All defined cases of the enum are now magically available for getting and setting as properties on bitflag property:

<?php
<?php

use Ornament\Core;
use Ornament\Bitflag\{ Bitflag, Options };

enum Status : int
{
    case on = 1;
    case initialized = 2;
}

class Model
{
    use Core\Model;

    #[Options(Status::class)]
    public Bitflag $status;
}

$model = Model::fromIterable(['status' => 3]);

// Now this works, assuming `$model` is the instance:
var_dump($model->status->on); // true in this example, since 3 & 1 = 1
$model->status->on = true; // bit 1 is now on (status |= 1)
$model->status->on = false; // bit 1 is now off (status &= ~1)
var_dump($model->status->initialized); // true, since 2 & 2 = 2

Bitflag properties also support JSON serialization (via Ornament\Bitflag\Property::jsonSerialize()). A map of true/false values will be exported. Similarly, the getArrayCopy method will return a hash of flags/bits where the bit was set to true.

Accessing the underlying bit value

You don't need to; simply use the backed enum:

<?php

echo Status::on->value; // 1

Similarly, use Status::cases() if you need all possible values.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-09-25

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固