Skip to content

dotgksh/bitmask

Repository files navigation

Bitmask banner

gksh/bitmask

A simple way to use bitmask and bitwise operations in PHP.

Latest Version on Packagist GitHub Tests Action Status License Total Downloads

Installation

Requires PHP 8.2+

composer require gksh/bitmask

🧪 Usage

Streamline flag handling by encoding boolean options into simple integers through bitmasking.

Please see ide.php for full example and playground for more.

enum Panel: int
{
    case Project = 1;
    case Terminal = 2;
    case SourceControl = 4;
    case Extensions = 8;
}

class Panels extends TinyBitmask
{
    public function isVisible(Panel $panel): bool
    {
        return $this->has($panel->value);
    }

    public function togglePanel(Panel $panel): Panels
    {
        return $this->toggle($panel->value);
    }
}

class Ide
{
    public Panels $panels;

    public function togglePanel(Panel $panel): self
    {
        $this->panels->togglePanel($panel);

        return $this;
    }
}

$ide = (new Ide())
    ->togglePanel(Panel::Project)
    ->togglePanel(Panel::Terminal);

$ide->panels->isVisible(Panel::Terminal); // true
$ide->panels->isVisible(Panel::Extensions); // false

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Credits

License

The MIT License (MIT). Please see License File for more information.