PHP Classes
elePHPant
Icontem

PHP Bitwise Flags: Manipulate sets of binary flag values

Recommend this page to a friend!
  Info   View files Documentation   View files View files (13)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2019-03-04 (20 days ago) RSS 2.0 feedNot yet rated by the usersTotal: 48 This week: 6All time: 9,458 This week: 72Up
Version License PHP version Categories
php-bitwise-flags 1.0.0Custom (specified...5Data types, PHP 7
Description Author

This package can manipulate sets of binary flag values.

It provides a base class that can be extended by classes that define the names and values of boolean flags that can be associated to given names.

The base class uses a trait that can perform several types of operations with boolean values like:

- Get or set the values of flags
- Add or remove a given flag
- Get the names of a given flags value
- Register a callback function that will be called when the flags are changed

  Performance   Level  
Name: Niko <contact>
Classes: 6 packages by
Country: Finland Finland
Age: ???
All time rank: 39387 in Finland Finland
Week rank: 12 Up1 in Finland Finland Up

Details

PHP Flags

The number of flags you can use is limited to the architecture of your system, e.g.: 32 flags on a 32-bit system or 64 flags on 64-bit system. To store 64-bits flags in a database, you will need to store it as UNSIGNED BIGINT in MySQL.

Install

Via composer:

composer require niko9911/bitwise-flags

Usage

Below some example usage code

<?php
declare(strict_types=1);

use Niko9911\Flags\Bits;
use Niko9911\Flags\Flags;

final class User extends Flags
{
    public const BANNED = Bits::BIT_1;              // 0x1
    public const ADMIN = Bits::BIT_2;               // 0x2
    public const ACTIVATED = Bits::BIT_3;           // 0x4
}
/ @var User|Flags $entity */
$entity = new User();

/ Usage when using single flag. */
$entity->addFlag(User::BANNED);

var_dump($entity->matchFlag(User::ADMIN));          // False
var_dump($entity->matchFlag(User::BANNED));         // True

$entity->removeFlag(User::BANNED);
var_dump($entity->matchFlag(User::BAR));            // False

/ Usage when using multiple flags. */
$entity->addFlag(User::ACTIVATED | User::ADMIN);

var_dump($entity->matchFlag(User::ACTIVATED));      // True
var_dump($entity->matchFlag(User::ACTIVATED | User::BANNED)); // False (Banned not set.)
var_dump($entity->matchFlag(User::ACTIVATED | User::ADMIN));  // True (Both set)
var_dump($entity->matchAnyFlag(User::ACTIVATED | User::BANNED)); // True. (One is set.)

/ Usage with flag names. */
// Flag name is taken from constant name
$entity = new User();

$entity->addFlag(User::BANNED | User::ADMIN | User::ACTIVATED);

var_dump($entity->getFlagNames()); // [Banned, Admin, Activated]
var_dump($entity->getFlagNames()); // // [Banned, Admin, Activated]
var_dump($entity->getFlagNames(User::ACTIVATED | User::BANNED)); // [Activated, Banned]

/ Overriding automatically defined flag names. */
final class UserWCustomNames extends BinaryFlags
{
    public const BANNED = Bits::BIT_1;
    public const ADMIN = Bits::BIT_2;
    public const ACTIVATED = Bits::BIT_3;
    
    // Implementing this specific function you can register
    // flags with custom naming. 
    public static function registerFlags(): array
    {
        return [
            static::BANNED => 'IsUserBanned',
            static::ADMIN => 'IsUserAdmin',
            static::ACTIVATED => 'IsUserActivated',
        ];
    }
}

$entity = new UserWCustomNames();
$entity->addFlag(
    UserWCustomNames::BANNED | 
    UserWCustomNames::ADMIN | 
    UserWCustomNames::ACTIVATED
);

var_dump($entity->getFlagNames()); 
// [
//      0 => IsUserBanned,
//      1 => IsUserAdmin,
//      2 => IsUserActivated,
// ]
  Files folder image Files  
File Role Description
Files folder imagescripts (1 file)
Files folder imagesrc (3 files)
Files folder imagetests (1 file, 1 directory)
Accessible without login Plain text file .php_cs.dist Example Example script
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml.dist Data Auxiliary data
Accessible without login Plain text file README.md Doc. Read me

  Files folder image Files  /  scripts  
File Role Description
  Accessible without login Plain text file composer.sh Data Auxiliary data

  Files folder image Files  /  src  
File Role Description
  Plain text file Bits.php Class Class source
  Plain text file Flags.php Class Class source
  Plain text file FlagsTrait.php Class Class source

  Files folder image Files  /  tests  
File Role Description
Files folder imageStubs (2 files)
  Plain text file FlagsTest.php Class Class source

  Files folder image Files  /  tests  /  Stubs  
File Role Description
  Plain text file ExampleFlags.php Class Class source
  Plain text file ExampleFlagsWithNames.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:48
This week:6
All time:9,458
This week:72Up