phoenixrvd / bitmask
bitmask utility package
Installs: 18
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:utility
Requires
- php: >=7.0
Requires (Dev)
- ext-xdebug: *
- codeclimate/php-test-reporter: dev-master
- phpunit/phpunit: ^6.1
This package is auto-updated.
Last update: 2025-03-12 06:28:31 UTC
README
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.