1. Go to this page and download the library: Download alazzi-az/laravel-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 / laravel-bitmask example snippets
use Alazziaz\LaravelBitmask\Casts\EnumBitmaskCast;
class YourModel extends Model
{
protected $casts = [
'permissions' => EnumBitmaskCast::class . YourEnumClass::class
];
}
$yourModel = YourModel::find(1);
$permissions = $yourModel->permissions; // Returns an instance of EnumBitmaskHandler so we can use all availabilities as you see below
use YourVendor\YourPackage\BitmaskCast;
class YourModel extends Model
{
protected $casts = [
'flags' => BitmaskCast::class . ':8' // Optional maxBit
];
}
$yourModel = YourModel::find(1);
$flags = $yourModel->flags; // Returns an instance of BitmaskHandler
use \Alazziaz\LaravelBitmask\Facades\BitmaskFacade;
// Create a BitmaskHandler instance with the combined permissions
$permissions = 1 | 2 | 4;
$bitmask = BitmaskFacade::bitmaskHandler($permissions);
$maskValue = $bitmask->getValue(); // Returns 7
// Create a BitmaskHandler with an initial mask of 0
$bitmaskHandler = BitmaskFacade::bitmaskHandler(0);
// Create a BitmaskHandler with an initial mask and a highest bit
$bitmaskHandlerWithLimit = BitmaskFacade::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\LaravelBitmask\Facades\BitmaskFacade;
enum YourEnum: int {
case FIRST = 1;
case SECOND = 2;
case THIRD = 4;
}
// Create an EnumBitmaskHandler with specific bits set
$enumBitmaskHandler = BitmaskFacade::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\LaravelBitmask\Facades\BitmaskFacade;
// 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 = BitmaskFacade::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',
};
}