1. Go to this page and download the library: Download alazzi-az/php-bitmask library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
alazzi-az / php-bitmask example snippets
use Alazziaz\Bitmask\Bitmask;
// Create a bitmask handler with an initial mask of 3
$maskable = Bitmask::bitmaskHandler(3); // The mask value is initialized to 3
use Alazziaz\Bitmask\Bitmask;
// Define an enum for demonstration
enum SomeEnum: int {
case FIRST = 1;
case SECOND = 2;
case THIRD = 4;
}
$enumClass = SomeEnum::class;
// Create an enum bitmask handler with the FIRST and SECOND values
$enumMaskable = Bitmask::enumBitmaskHandler($enumClass, SomeEnum::FIRST, SomeEnum::SECOND);
use Alazziaz\Bitmask\Bitmask;
enum YourEnum: int {
case FIRST = 1;
case SECOND = 2;
case THIRD = 4;
}
// Create a bitmask handler with FIRST and SECOND enum values
$enumHandler = Bitmask::enumBitmaskHandlerFactory()->create(
YourEnum::class, YourEnum::FIRST, YourEnum::SECOND
);
use Alazziaz\Bitmask\Bitmask;
// Create a BitmaskHandler instance with the combined permissions
$permissions = 1 | 2 | 4;
$bitmask = Bitmask::bitmaskHandler($permissions);
$maskValue = $bitmask->getValue(); // Returns 7
// Create a BitmaskHandler with an initial mask of 0
$bitmaskHandler = Bitmask::bitmaskHandler(0);
// Create a BitmaskHandler with an initial mask and a highest bit
$bitmaskHandlerWithLimit = Bitmask::bitmaskHandler(0, 7);
// Returns the current mask (e.g., 0)
$currentValue = $bitmaskHandler->getValue();
// Returns the binary string representation
$binaryString = $bitmaskHandler->toString();
// Adds bits 1 and 2 to the current mask
$bitmaskHandler->add(1, 2);
// Deletes bit 1 from the current mask
$bitmaskHandler->delete(1);
// Returns true if bits 1 and 2 are set
$hasBits = $bitmaskHandler->has(1, 2);
use Alazziaz\Bitmask\Bitmask;
enum YourEnum: int {
case FIRST = 1;
case SECOND = 2;
case THIRD = 4;
}
// Create an EnumBitmaskHandler with specific bits set
$enumBitmaskHandler = Bitmask::enumBitmaskHandler(YourEnum::class, YourEnum::BIT_ONE, YourEnum::BIT_TWO);
// Returns the current mask value (e.g., 0)
$currentValue = $enumBitmaskHandler->getValue();
// Add bits to the current mask
$enumBitmaskHandler->add(YourEnum::BIT_THREE);
// Delete a bit from the current mask
$enumBitmaskHandler->delete(YourEnum::BIT_ONE);
// Check if specific bits are set in the current mask
$hasBits = $enumBitmaskHandler->has(YourEnum::BIT_TWO, YourEnum::BIT_THREE);
// Convert the current mask to an array representation
$arrayRepresentation = $enumBitmaskHandler->toArray(); // e.g., ['bit_one' => false, 'bit_two' => true, 'bit_three' => true]
use \Alazziaz\Bitmask\Bitmask;
// Assuming you have an enum defined as:
enum YourEnum: int {
case BIT_ONE = 1;
case BIT_TWO = 2;
case BIT_THREE = 4;
}
// Creating an instance with no bits set
$bitmaskHandler = Bitmask::enumBitmaskHandlerFactory()
->none(YourEnum::class);
// Adding bits
$bitmaskHandler->add(YourEnum::BIT_ONE, YourEnum::BIT_TWO);
// Checking current value
$currentValue = $bitmaskHandler->getValue(); // Returns 3
// Checking if specific bits are set
$hasBitOne = $bitmaskHandler->has(YourEnum::BIT_ONE); // Returns true
// Converting to an array
$arrayRepresentation = $bitmaskHandler->toArray(); // ['bit_one' => true, 'bit_two' => true, 'bit_three' => false]
public function toMaskKey(): string
{
return match ($this) {
self::READ => 'read_permission',
self::WRITE => 'write_permission',
self::EXECUTE => 'execute_permission',
};
}
bash
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.