PHP code example of flow / bitwiser

1. Go to this page and download the library: Download flow/bitwiser library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

flow / bitwiser example snippets


class PermissionsBitwiser extends AbstractBitwiser
{
    const CAN_EDIT_POSTS = 0;
    const CAN_DELETE_POSTS = 1;
    const CAN_CREATE_USERS = 2;
}

$state = 0; // this value is passed by reference

$permissions = new PermissionsBitwiser($state, function (AbstractBitwiser $bitwiser) {
	echo $bitwiser->getState();
});

$permissions->add(PermissionsBitwiser::CAN_EDIT_POSTS); // echoes 1
$permissions->add(PermissionsBitwiser::CAN_DELETE_POSTS); // echoes 3
$permissions->add(PermissionsBitwiser::CAN_CREATE_USERS); // echoes 7
$permissions->remove(PermissionsBitwiser::CAN_DELETE_POSTS); // echoes 5

$permissions->getState(); // int(5)
$permissions->has(PermissionsBitwiser::CAN_EDIT_POSTS); // true
$permissions->has(PermissionsBitwiser::CAN_DELETE_POSTS); // false


class User extends Model
{
    public function getPermissionsAttribute()
    {
        $state = $this->attributes['permissions']; // Don't pass this by reference
        $self = $this;
        return new PermissionsBitwiser($state, function ($bitwiser) use ($self) {
            $self->permissions = $bitwiser->getState();
        });
    }
}

$user = new User;

$user->permissions->add(PermissionsBitwiser::CAN_CREATE_USERS);

$user->save();

$user->permissions->has(PermissionsBitwiser::CAN_DELETE_POSTS); // false
$user->permissions->has(PermissionsBitwiser::CAN_CREATE_USERS); // true. etc