mfonte/bitmask

Bitmask class helps you to use all might of bit masks and don't care about what bitwise operations are. It represents a high-level abstraction that looks like simple and manageable object and behaves like collection.

v1.0 2023-02-17 15:44 UTC

This package is auto-updated.

Last update: 2024-12-17 19:58:05 UTC


README

Bitmask class helps you to use all might of bit masks and don't care about what bitwise operations are. It represents a high-level abstraction that looks like simple and manageable object and behaves like collection.

Installation

Add the following dependency to your composer.json file.

{
    "require": {
        "mfonte/bitmask": "^1.0"
    }
}

Example usage

For example we have User class that can have different roles.

use Mfonte\Bit\Mask;

class User
{
    const ROLE_ADMIN = Mask::FLAG_1;
    const ROLE_MANAGER = Mask::FLAG_2;
    const ROLE_CUSTOMER = Mask::FLAG_3;

    /**
     * @var Mask
     */
    private $roles;
    
    public function __construct()
    {
        $this->roles = new Mask();
    }
    
    public function becomeAdmin()
    {
        $this->roles->add(self::ROLE_ADMIN);
    }
    
    public function isAdmin()
    {
        return $this->roles->has(self::ROLE_ADMIN);
    }
    
    ...
    
    public function isCustomer()
    {
        return $this->roles->has(self::ROLE_CUSTOMER);
    }
    
    ...
}

Now we are able to create a user.

$user = new User();

$user->becomeAdmin(); // User now has admin role
$user->becomeAdmin(); // Throws MaskException because role has been already set
                      // and mask is in strict mode

$user->isAdmin(); // Returns true
$user->isCustomer(); // Returns false