gksh/bitmask

A bitmask value object for PHP

Fund package maintenance!
karkowg

v0.0.1 2024-05-03 19:20 UTC

This package is auto-updated.

Last update: 2024-12-05 21:51:29 UTC


README

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.