ali-eltaweel / bitmask
There is no license information available for the latest version (1.0.0) of this package.
A simple bitmask library for PHP
1.0.0
2025-06-20 08:01 UTC
Requires
- php: ^8.1
- ali-eltaweel/attr-action: ^1.0.0
This package is auto-updated.
Last update: 2025-06-20 08:05:37 UTC
README
A simple bitmask library for PHP
Installation
Install bitmask via Composer:
composer require ali-eltaweel/bitmask
Usage
Declaring Bitmask
use Bitmask\{ Annotations\Bit, Bitmask }; class JsonOptions extends Bitmask { #[Bit] const PRETTY_PRINT = JSON_PRETTY_PRINT; #[Bit] const UNESCAPED_SLASHES = JSON_UNESCAPED_SLASHES; #[Bit] const OBJECT_AS_ARRAY = JSON_OBJECT_AS_ARRAY; }
Using the #[Bit]
annotation with no arguments will result in the name of the constant being used as the bit's name. You can also pass a string to the annotation to specify a custom name for the bit:
use Bitmask\{ Annotations\Bit, Bitmask }; class JsonOptions extends Bitmask { #[Bit('pretty')] const PRETTY_PRINT = JSON_PRETTY_PRINT; #[Bit('forceArray')] const OBJECT_AS_ARRAY = JSON_OBJECT_AS_ARRAY; }
Working with Bitmasks
$jsonOptions = new JsonOptions(JsonOptions::PRETTY_PRINT); $jsonOptions->has(JsonOptions::PRETTY_PRINT); // true $jsonOptions->has(JsonOptions::UNESCAPED_SLASHES); // false $jsonOptions = $jsonOptions->add(JsonOptions::UNESCAPED_SLASHES); $jsonOptions->has(JsonOptions::UNESCAPED_SLASHES); // true $jsonOptions = $jsonOptions->remove(JsonOptions::PRETTY_PRINT); $jsonOptions->has('pretty'); // false $jsonOptions = $jsonOptions->add('pretty|forceArray'); $jsonOptions->has('pretty'); // true $jsonOptions->has(JsonOptions::OBJECT_AS_ARRAY); // true $jsonOptions->toArray(); // [ 128 => 'pretty', 64 => 'UNESCAPED_SLASHES', 1 => 'forceArray' ] $jsonOptions->toInt(); // 193