PHP code example of rousi / bitery

1. Go to this page and download the library: Download rousi/bitery 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/ */

    

rousi / bitery example snippets




/**
 * Creating an instance of the Bitery\BitManager class.
 * A string can be given as the first argument.
 */
$bits = new Bitery\BitManager;

// Setting bit number 7 to true.
$bits->setBit(7, true);

// Setting bits from 0 to 7 (1st byte) to the value 65.
$bits->setBitRange(
    Bitery\Range::fromInterval(0, 7),
    65
);

/**
 * Outputs the string "A".
 * Because we set the first byte (bits 0 to 7) to 65,
 * which gives the letter A.
 */
echo $bits->getString();



$areas = Bitery\Factory\AreaCollection::fromArray([
    [
        'key' => 'isAdminItem'
        // Specifies only one bit area
        'bit' => 0,
        'default' => false
    ],
    [
        'key' => 'isItemFrozen',
        'range' => (new Bitery\Range)->add(1),
        'default' => false
    ],
    [
        'key' => 'itemLevel',
        // Specifies the bit interval area from bitStart to bitEnd
        'bitStart' => 2,
        'bitEnd' => 4,
        'default' => 5
    ]
]);

$bits = new Bitery\BitManager();
$bitery = new Bitery\Bitery($areas, $bits);

// Sets the value of all areas to their default values
$bitery->toDefaults();

// Outputs an associative array of all areas with their values
print_r($bitery->getData());

// Gets the controller for the area with the key 'itemLevel'
$controller = $bitery->getController('itemLevel');

// Gets the current value of the 'itemLevel' area
// Outputs integer 5
echo $controller->getData();

// Sets the value 2 in the 'itemLevel' area
$controller->setData(2);

// Outputs binary data
echo $bitery;



$connection = new PDO($dsn, $user, $password);

$areas = Bitery\Factory\AreaCollection::fromArray(
    /**
     * We can do this because the factory accepts an array
     * with the keys that we specified in the columns
     * of the table. If the names of the columns of the table
     * do not match, then you will need to create 
     * a collection "manually".
     */
    $connection->query('SELECT * FROM `usersAccess`')->fetchAll()
);

// Creating an access string for a new user
$bitery = new Bitery\Bitery($areas);
$bitery->toDefaults();

$connection
    ->prepare('INSERT INTO `users` (`access`) VALUES (:access)')
    ->execute([
        'access' => (string) $bitery
    ]);

// Changing access rights for an existing user
$user = $connection
    ->query('SELECT `access` FROM `users` WHERE ...')
    ->fetch();

$bitery->withBits(
    new Bitery\BitManager($user['access'])
);
// or $bitery = new Bitery\Bitery($areas, $user['access']);

// Setting the values of the create and update accesses to true
$bitery->getController('create')->setData(true);
$bitery->getController('update')->setData(true);

// {Saving changes to the database}

// Checking whether the user has read access
if ($bitery->getController('read')?->getData())
{
    // ...
}