asolomatin/php-big-bit-mask

Bitmask compatible with JavaScript big-bit-mask - the bitmask serializable into a base64-like, url-safe string.

1.0.2 2020-07-19 21:35 UTC

This package is auto-updated.

Last update: 2024-04-29 03:57:23 UTC


README

Packagist Packagist downloads Travis-CI Coverage Status GitHub

When bits is not enough ...

This library implements an fully compatible PHP version of big-bit-mask - the bitmask serializable into a base64-like, url-safe string.

Other platform compatibility

Platform Repository Package
JavaScript / TypeScript big-bit-mask NPM
.NET BigBitMask.NET NuGet

Install

> composer require asolomatin/php-big-bit-mask

Usage

Namespace

use asolomatin\BigBitMask\BitMask;

What next?

Now we can create new empty bitmask

$bitmask = new BitMask();

or load it from string

$bitmask = new BitMask("CE3fG_gE-56");

//Let's see what inside now
$content = "";
for ($i = 0; $i < 11 * 6; $i++) { // Each character contains 6 bits, as in base64
    $content .= $bitmask[$i] ? "1" : "0";
}
echo $content.PHP_EOL;

output: 010000001000111011111110011000111111000001001000011111100111010111

Then we can change some bits and get back our string representation

$bitmask[65] = false;
$bitmask[64] = false;
$bitmask[63] = false;
$bitmask[61] = false;

$bitmask[19] = false;
$bitmask[5] = true;

echo strval($bitmask).PHP_EOL;

output: iE3dG_gE-5

But what if I want to have a named flags?

You can extend BitMask class with your model:

class MyCoolCheckboxes extends BitMask
{
    const CHECKBOX_0 = 0;
    const CHECKBOX_1 = 1;
    const CHECKBOX_2 = 2;
    const CHECKBOX_3 = 3;
    const CHECKBOX_4 = 4;
    const CHECKBOX_5 = 5;
    const CHECKBOX_6 = 6;
    const CHECKBOX_7 = 7;
    const CHECKBOX_8 = 8;
    const CHECKBOX_9 = 9;

    // Some magic
    public function __set(string $name, bool $value) { call_user_func([$this, "set".ucfirst($name)], $value); }
    public function __get(string $name): bool { return call_user_func([$this, "set".ucfirst($name)]); }

    public function getCheckbox0(): bool { return $this[self::CHECKBOX_0]; }
    public function setCheckbox0(bool $value) { $this[self::CHECKBOX_0] = $value; }

    public function getCheckbox1(): bool { return $this[self::CHECKBOX_1]; }
    public function setCheckbox1(bool $value) { $this[self::CHECKBOX_1] = $value; }

    public function getCheckbox2(): bool { return $this[self::CHECKBOX_2]; }
    public function setCheckbox2(bool $value) { $this[self::CHECKBOX_2] = $value; }

    public function getCheckbox3(): bool { return $this[self::CHECKBOX_3]; }
    public function setCheckbox3(bool $value) { $this[self::CHECKBOX_3] = $value; }

    public function getCheckbox4(): bool { return $this[self::CHECKBOX_4]; }
    public function setCheckbox4(bool $value) { $this[self::CHECKBOX_4] = $value; }

    public function getCheckbox5(): bool { return $this[self::CHECKBOX_5]; }
    public function setCheckbox5(bool $value) { $this[self::CHECKBOX_5] = $value; }

    public function getCheckbox6(): bool { return $this[self::CHECKBOX_6]; }
    public function setCheckbox6(bool $value) { $this[self::CHECKBOX_6] = $value; }

    public function getCheckbox7(): bool { return $this[self::CHECKBOX_7]; }
    public function setCheckbox7(bool $value) { $this[self::CHECKBOX_7] = $value; }

    public function getCheckbox8(): bool { return $this[self::CHECKBOX_8]; }
    public function setCheckbox8(bool $value) { $this[self::CHECKBOX_8] = $value; }

    public function getCheckbox9(): bool { return $this[self::CHECKBOX_9]; }
    public function setCheckbox9(bool $value) { $this[self::CHECKBOX_9] = $value; }
}

$checkboxes = new MyCoolCheckboxes();
$checkboxes->checkbox5 = true;
$checkboxes->checkbox7 = true;
$checkboxes->checkbox8 = true;

echo strval($checkboxes).PHP_EOL;

output: gG

License

MIT

Copyright (C) 2020 Aleksej Solomatin