Skip to content

phoenixrvd/bitmask

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Bitmask

Minimum PHP Version Latest Stable Version composer.lock License

Build Status Code Climate StyleCI Test Coverage BCH compliance Latest Unstable Version

In PHP a number is (mostly) 4 Bytes long. This means that one number actually uses 32 bits of the internal storage.

In this case 32 boolean values can be stored as single integer. The problem ist a 'magic numbers'.

Installation

Install the latest version with

composer require phoenixrvd/bitmask

Example

Without this API before.php

<?php

class StateMap {
    const OPTION_1 = 1;
    const OPTION_2 = 2;
    const OPTION_4 = 4;
    // What is next ????
}

// Check for Active Feature
$activeFeatures = 6;

if(($activeFeatures & StateMap::OPTION_1) === StateMap::OPTION_1){
    // Do this
}

if(($activeFeatures & StateMap::OPTION_2) !== StateMap::OPTION_2) {
    // Do this
}

// Activation and deactivation from options ist not 'Human Readable'.

With this API after.php

<?php

class StateMap {
    const OPTION_1 = 0;
    const OPTION_2 = 1;
    const OPTION_4 = 2;
    const OPTION_5 = 3;
    const OPTION_6 = 4;
}

// Check for Active Feature
$activeFeatures = (new \PhoenixRVD\Bitmask\BitmaskFactory())->fromInt(6);

if($activeFeatures->isOn(StateMap::OPTION_1)){
    // Do this
}

if($activeFeatures->isOff(StateMap::OPTION_2)) {
    // Do that
}

// Activate options
$activeFeatures->on(StateMap::OPTION_5, StateMap::OPTION_6);

// Deactivate options
$activeFeatures->off(StateMap::OPTION_4, StateMap::OPTION_1);

Testing

composer bitmask:test

Copyright and license

Code released under the MIT License.