PHPackages                             meius/flag-forge - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. meius/flag-forge

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

meius/flag-forge
================

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.

v1.0.1(1y ago)0561MITPHPPHP &gt;=8.1CI passing

Since Mar 3Pushed 1y ago2 watchersCompare

[ Source](https://github.com/brann-meius/flag-forge)[ Packagist](https://packagist.org/packages/meius/flag-forge)[ Fund](https://www.buymeacoffee.com/bohdanmeiuv)[ RSS](/packages/meius-flag-forge/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (3)Versions (4)Used By (1)

Flag Forge Package
==================

[](#flag-forge-package)

[![Build Status](https://camo.githubusercontent.com/34cbae330b018041378586b9b9a3a5f6fde1a027832a397cc29f733d598ba983/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6272616e6e2d6d656975732f666c61672d666f7267652f63692e796d6c)](https://github.com/brann-meius/flag-forge/actions)[![codecov](https://camo.githubusercontent.com/69fb0d871c6e8c7c22feb09639c7a788e5dfcc984db7cce351bf5eeafbb809cb/68747470733a2f2f636f6465636f762e696f2f67682f6272616e6e2d6d656975732f666c61672d666f7267652f67726170682f62616467652e7376673f746f6b656e3d3058483741414b485332)](https://codecov.io/gh/brann-meius/flag-forge)[![Codacy Badge](https://camo.githubusercontent.com/3640e17831418c5902e134816912208f6fb9c34ae9328c69ea69550fae04aea3/68747470733a2f2f6170702e636f646163792e636f6d2f70726f6a6563742f62616467652f47726164652f6436323338616365623037643430326638373432643462303539376135626137)](https://app.codacy.com/gh/brann-meius/flag-forge/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)[![License](https://camo.githubusercontent.com/d3bdb7a9da34607a697d90189a6d2e2d835bda54189e8f090d22e2f999bc9b0c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6272616e6e2d6d656975732f666c61672d666f726765)](LICENSE)[![PHP Version](https://camo.githubusercontent.com/761bb52ce7743ca1b551cf048da0506b2cfe6c9510e25672526f393f06542739/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253345253344253230382e312d626c7565)](https://www.php.net/)

Table of Contents
-----------------

[](#table-of-contents)

- [Overview](#overview)
- [Requirements](#requirements)
- [Getting Started](#getting-started)
- [Installation](#installation)
- [Usage](#usage)
    - [Working with FlagManager](#working-with-flagmanager)
- [Database Integration Example](#database-integration-example)
    - [Table Schema: chat\_user](#table-schema-chat_user)
    - [Sample Records](#sample-records)
    - [Example Query: Checking a Specific Permission](#example-query-checking-a-specific-permission)
    - [Example PHP Code Using PDO](#example-php-code-using-pdo)
- [API Reference](#api-reference)
- [Support](#support)
- [License](#license)

Overview
--------

[](#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
------------

[](#requirements)

- PHP &gt;= 8.1

Getting Started
---------------

[](#getting-started)

To get started with the `meius/flag-forge` package, follow the installation instructions below and check out the usage examples.

Installation
------------

[](#installation)

1. Install the package via Composer: ```
    composer require meius/flag-forge
    ```

Usage
-----

[](#usage)

Below is an example enum and how to work with the FlagManager.

```
 enum Permission: int implements Bitwiseable
 {
     case SendMessages = 1 combine(
     Permission::SendMessages, // Already set.
     Permission::DeleteMessages, // Not set; will be added.
     Permission::AddUsers // Already set.
 )->getMask(); // Expected mask: 23 (SendMessages=1, DeleteMessages=2, AddUsers=4, PinMessages=16: 21+2=23)

 // ----------------------------------------------------------------------
 // REMOVE FLAGS
 // ----------------------------------------------------------------------
 $manager->remove(Permission::AddUsers) // - Remove AddUsers: bit is set, so it will be removed.
     ->remove(Permission::ManageModerators) // - Remove ManageModerators: bit is not set, so nothing changes.
     ->getMask(); // Expected mask: 19

 // ----------------------------------------------------------------------
 // TOGGLE FLAGS
 // ----------------------------------------------------------------------
 $manager->toggle(
     Permission::SendMessages, // Bit is set; toggled off.
     Permission::DeleteMessages, // Bit is set; toggled off.
     Permission::AddUsers, // Bit is not set; toggled on.
     Permission::RemoveUsers // Bit is not set; toggled on.
 )->getMask(); // Expected mask: 28

 // ----------------------------------------------------------------------
 // CHECK FLAGS
 // ----------------------------------------------------------------------
 $manager->has(Permission::SendMessages); // false
 $manager->has(Permission::AddUsers); // true
 $manager->doesntHave(Permission::SendMessages); // true
 $manager->doesntHave(Permission::AddUsers); // false

 // ----------------------------------------------------------------------
 // ITERATE OVER ACTIVE FLAGS
 // ----------------------------------------------------------------------
 foreach ($manager as $flag) {
     /**
      * Example output:
      * Active flag: AddUsers (4)
      * Active flag: RemoveUsers (8)
      * Active flag: PinMessages (16)
      */
     echo "Active flag: " . $flag->name . " (" . $flag->value . ")" . PHP_EOL;
 }

 // ----------------------------------------------------------------------
 // CLEAR FLAGS
 // ----------------------------------------------------------------------
 $manager->clear(); // Expected mask: 0
```

Database Integration Example
----------------------------

[](#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

[](#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

[](#table-schema-chat_user)

ColumnData TypeConstraintsDescriptionidUUIDPRIMARY KEYUnique identifier for the record.chat\_idUUIDFOREIGN KEY, NOT NULLIdentifier of the chat.user\_idUUIDFOREIGN KEY, NOT NULLIdentifier of the user.permissionsUNSIGNED TINYINTNOT NULL, DEFAULT (17)Bitmask representing user permissions.### Sample Records

[](#sample-records)

Below are a few example rows inserted into the `chat_user` table:

idchat\_iduser\_idpermissions11111111-1111-1111-1111-111111111111chat-1234user-12342122222222-2222-2222-2222-222222222222chat-1234user-56782333333333-3333-3333-3333-333333333333chat-4321user-98765### Example Query: Checking a Specific Permission

[](#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 add(Permission::SendMessages);

 // Prepare the SQL statement
 $stmt = $pdo->prepare('
     SELECT *
     FROM chat_user
     WHERE chat_id = :chat_id
       AND user_id = :user_id
       AND (permissions & :flag) = :flag
 ');

 // Execute the query with the parameters
 $stmt->execute([
     ':chat_id' => $chatId,
     ':user_id' => $userId,
     ':flag' => $manager,
 ]);

 // Fetch the result
 $result = $stmt->fetch(PDO::FETCH_ASSOC);

 if ($result) {
     echo "User has the SendMessages permission.";
 } else {
     echo "User does NOT have the SendMessages permission.";
 }
```

API Reference
-------------

[](#api-reference)

### FlagManager

[](#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
-------

[](#support)

For support, please open an issue on the [GitHub repository](https://github.com/brann-meius/flag-forge/issues).

### Contributing

[](#contributing)

We welcome contributions to the `meius/flag-forge` library. To contribute, follow these steps:

1. **Fork the Repository**: Fork the repository on GitHub and clone it to your local machine.
2. **Create a Branch**: Create a new branch for your feature or bugfix.
3. **Write Tests**: Write tests to cover your changes.
4. **Run Tests**: Ensure all tests pass by running `phpunit`.
5. **Submit a Pull Request**: Submit a pull request with a clear description of your changes.

For more details, refer to the [CONTRIBUTING.md](CONTRIBUTING.md) file.

License
-------

[](#license)

This package is open-sourced software licensed under the [MIT license](LICENSE).

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance45

Moderate activity, may be stable

Popularity9

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 50% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~9 days

Total

2

Last Release

432d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/9436066baff14a6bcf412f22489e9c476f7e792aed4d36db360bcc23ac1b42af?d=identicon)[brann-meius](/maintainers/brann-meius)

---

Top Contributors

[![b-meius](https://avatars.githubusercontent.com/u/174311900?v=4)](https://github.com/b-meius "b-meius (15 commits)")[![brann-meius](https://avatars.githubusercontent.com/u/177737890?v=4)](https://github.com/brann-meius "brann-meius (15 commits)")

---

Tags

bitwisebitwise-operationsconfigurationefficiencyenumerationenumsflagsperformancephpconfigurationperformanceflagsenumerationenumsbitwiseefficiencybitwise-operations

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/meius-flag-forge/health.svg)

```
[![Health](https://phpackages.com/badges/meius-flag-forge/health.svg)](https://phpackages.com/packages/meius-flag-forge)
```

###  Alternatives

[symfony/options-resolver

Provides an improved replacement for the array\_replace PHP function

3.2k493.9M1.6k](/packages/symfony-options-resolver)[league/config

Define configuration arrays with strict schemas and access values with dot notation

564302.2M24](/packages/league-config)[marc-mabe/php-enum

Simple and fast implementation of enumerations with native PHP

49644.8M97](/packages/marc-mabe-php-enum)[chillerlan/php-settings-container

A container class for immutable settings objects. Not a DI container.

3427.3M21](/packages/chillerlan-php-settings-container)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
