rousi/bitery

位管理库

1.0.0-beta 2024-09-18 19:25 UTC

This package is auto-updated.

Last update: 2024-09-23 12:56:37 UTC


README

Bitery 是一个库,它允许您以更高的层次操作位,以及组织位的存储信息。

安装

您可以通过 Composer 安装此库。只需在您的工作目录中执行以下命令即可

composer require rousi/bitery

使用

位管理器使用

Bitery\BitManager 类用于更改字符串中的某些位或位范围。

<?php

/**
 * 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();

请参阅 BitManager 接口范围接口 获取更多信息。

警告

Bitery\BitManager 类中的位在每个字节中从左到右编号,而不是从右到左,如应。因此,例如,更改位 7(编号从零开始)实际上将影响右边的第一个位。

Bitery 使用

Bitery\Bitery 用于将字符串“分割”成区域,可以使用它们的键在其中写入和读取信息。这允许您以紧凑的格式存储许多布尔或整数变量。

<?php

$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;

请参阅 区域接口控制器类Bitery 类 获取更多信息。

适用于何处?

我相信这可以应用于三种情况

  1. 存储某物的设置
  2. 存储某物的访问标志
  3. 存储任何可以表示为布尔值或小数的其他数据

用户访问标志示例

让我们假设我们有一个用户表,我们面临着存储每个用户访问信息的任务。当然,您可以将访问信息存储在 JSON 格式或其他任何文本格式中,但这样的字段将占用相当多的空间,尤其是在数据库中有大量此类记录的情况下。相反,我们可以在用户访问表中创建一个用户访问表,其中描述了用户可能拥有的所有访问权限。

示例 usersAccess

<?php

$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())
{
    // ...
}

待办事项

  • 单元测试;
  • 按正确顺序(每个字节从右到左)获取位的位管理器。

许可证

此库根据 MIT 许可证分发。请参阅 LICENSE 文件以获取更多信息。