Code Monkey home page Code Monkey logo

bitmasked-properties's Introduction

Bitmasked Properties

This library provides a trait to use bitmasked properties with PHP 8.1 enums. I was reading this and could think a few different places where this would be useful for me, so I decided to make this library to make it a bit more re-usable; it also felt like a good way to start playing around with PHP 8.1 enums.

Installation

composer require henrywhitaker3/bitmasked-properties

Usage

For example, you have a model Person that can be opted in or out for email and sms comms. A simple (not v good) way of doing this could be having individual columns in the database to store each of these methods:

class Person
{
    public function __construct(
        public bool $sms,
        public bool $email
    ) {}

    public function isOptedIn(string $type): bool
    {
        // Should really check the value of type is valid first...
        return $this->{$type};
    }

    public function optin(string $type): void
    {
        $this->updateOptin($type, true);
    }

    public function optout(string $type): void
    {
        $this->updateOptin($type, false);
    }

    private function updateOptin(string $type, bool $value): void
    {
        switch($type) {
            case 'sms':
                $this->sms = $value;
                break;
            case 'email':
                $this->email = $value;
                break;
        }
    }
}

This requires hard-coding the field names and having to run migrations to add columns when a new communication type comes along, which is a bit gross.

For some scenarios, using a bitmasked field would be a far nicer solution - only some as you can't index these values and therefore can't query them very efficiently. But, it allows you to just add a new case to the enum whenever a new communication type gets added with no change to the database. Using this you can have up to 32 different boolean values in a standard integer field. Here's how to use it for the person example above:

enum Optin: int implements BitmaskEnum
{
    case SMS = 1 << 0; // 1
    case EMAIL = 1 << 1; // 2
}
class Person
{
    use HasBitmaskedProperties;

    public function __construct(
        public bool $optin
    ) {}

    public function isOptedIn(Optin $type): bool
    {
        return $this->getFlag('optin', $type);
    }

    public function optin(Optin $type): void
    {
        $this->setFlag('optin', $type, true);
    }

    public function optout(Optin $type): void
    {
        $this->setFlag('optin', $type, false);
    }

    public function getOptins(): WeakMap
    {
        return $this->flagToWeakmap('optin', Optin::class);
    }
}

Now it's really simple to use:

$person = new Person; // $optin === 0

$person->isOptedIn(Optin::SMS); // returns false
$person->optin(Optin::SMS); // $optin === 1
$person->isOptedIn(Optin::SMS); // returns true

$person->optin(Optin::EMAIL); // $optin === 3
$person->isOptedIn(Optin::EMAIL); // returns true

$person->optout(Optin::SMS); // $optin === 2
$person->isOptedIn(Optin::SMS); // returns false

$person->getOptins()[Optin::EMAIL]; // return true

You can add a new value to the Optin enum with no changes to the database or code.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.