Download the PHP package meius/flag-forge without Composer
On this page you can find all versions of the php package meius/flag-forge. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download meius/flag-forge
More information about meius/flag-forge
Files in meius/flag-forge
Package flag-forge
Short Description FlagForge is a lightweight, high-performance library for bitwise enumerations. It offers intuitive tools to create, combine, and verify flags for efficient management of Boolean states in applications—ideal for access control, configuration options, and scenarios demanding compact storage and fast processing.
License MIT
Informations about the package flag-forge
Flag Forge Package
Table of Contents
- Overview
- Requirements
- Getting Started
- Installation
- Usage
- Working with FlagManager
- Database Integration Example
- Table Schema: chat_user
- Sample Records
- Example Query: Checking a Specific Permission
- Example PHP Code Using PDO
- API Reference
- Support
- License
Overview
The meius/flag-forge
package provides an intuitive API for defining and managing bitwise enumerations in PHP. By
assigning power-of-two values to each flag, the package allows you to combine multiple flags using bitwise operators and
efficiently check individual flags using simple methods.
Requirements
- PHP >= 8.1
Getting Started
To get started with the meius/flag-forge
package, follow the installation instructions below and check out the usage
examples.
Installation
- Install the package via Composer:
Usage
Below is an example enum and how to work with the FlagManager.
Working with FlagManager
Database Integration Example
FlagForge can be easily integrated with a database. The following example demonstrates how to store and retrieve a flag mask using PDO.
Database Schema and Usage Example
The following section describes the chat_user
table schema used to store user permissions as a bitmask,
provides a few sample records, and shows an example of how to query the database to check a specific permission.
Table Schema: chat_user
Column | Data Type | Constraints | Description |
---|---|---|---|
id | UUID | PRIMARY KEY | Unique identifier for the record. |
chat_id | UUID | FOREIGN KEY, NOT NULL | Identifier of the chat. |
user_id | UUID | FOREIGN KEY, NOT NULL | Identifier of the user. |
permissions | UNSIGNED TINYINT | NOT NULL, DEFAULT (17) | Bitmask representing user permissions. |
Sample Records
Below are a few example rows inserted into the chat_user
table:
id | chat_id | user_id | permissions |
---|---|---|---|
11111111-1111-1111-1111-111111111111 | chat-1234 | user-1234 | 21 |
22222222-2222-2222-2222-222222222222 | chat-1234 | user-5678 | 23 |
33333333-3333-3333-3333-333333333333 | chat-4321 | user-9876 | 5 |
Example Query: Checking a Specific Permission
Suppose you want to check if a specific user in a chat has the "SendMessages" permission.
Assume that the SendMessages
permission corresponds to the bit value 1
(i.e. 1 << 0
).
The following SQL query uses the bitwise AND operator to verify that the permission bit is set:
Example PHP Code Using PDO
Below is an example of how you might execute the above query using a PDO instance (assumed to be available as $pdo
):
API Reference
FlagManager
add(Bitwiseable $flag): self
— Adds a flag to the current mask.remove(Bitwiseable $flag): self
— Removes a flag from the current mask.combine(Bitwiseable ...$flags): self
— Combines multiple flags into the current mask.toggle(Bitwiseable ...$flags): self
— Toggles specified flags.clear(): self
— Clears all flags in the current mask.has(Bitwiseable $flag): bool
— Checks if the specified flag is present.doesntHave(Bitwiseable $flag): bool
— Checks if the specified flag is not present.getMask(): int
— Returns the current mask value.toArray(): array
— Returns an array representation of the current mask.
Support
For support, please open an issue on the GitHub repository.
Contributing
We welcome contributions to the meius/flag-forge
library. To contribute, follow these steps:
- Fork the Repository: Fork the repository on GitHub and clone it to your local machine.
- Create a Branch: Create a new branch for your feature or bugfix.
- Write Tests: Write tests to cover your changes.
- Run Tests: Ensure all tests pass by running
phpunit
. - Submit a Pull Request: Submit a pull request with a clear description of your changes.
For more details, refer to the CONTRIBUTING.md file.
License
This package is open-sourced software licensed under the MIT license.