PHPackages                             codekandis/phlags - 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. codekandis/phlags

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

codekandis/phlags
=================

Introduces the possibility to use flagable enums in PHP.

3.0.1(8mo ago)37622MITPHPPHP ^7.4

Since Nov 17Pushed 8mo ago2 watchersCompare

[ Source](https://github.com/codekandis/phlags)[ Packagist](https://packagist.org/packages/codekandis/phlags)[ RSS](/packages/codekandis-phlags/feed)WikiDiscussions main Synced 3w ago

READMEChangelog (6)Dependencies (2)Versions (8)Used By (2)

CodeKandis / Phlags
===================

[](#codekandis--phlags)

[![Version](https://camo.githubusercontent.com/29e9e0aa8903b409540527372a4aa3eee09f4ba7c2f09cc030a10f25d8823f1d/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f76657273696f6e2d332e302e312d626c75652e737667)](./CHANGELOG.md)[![License](https://camo.githubusercontent.com/07a7d0169027aac6d7a0bfa8964dfef5fbc40d5a2075cabb3d8bc67e17be3451/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d79656c6c6f772e737667)](./LICENSE)[![Minimum PHP Version](https://camo.githubusercontent.com/0e9ac047546796cfdbe1423d1f4d91c8f37d2fbb11614a7900bb7686aaa5401f/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253345253344253230372e342d3838393242462e737667)](https://php.net)[![Code Coverage](https://camo.githubusercontent.com/af813ae002de8e31bfd234a3d5eab4fe1963ed998df54d1f3c5e3e1fe334f7d6/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f636f7665726167652d3130302532352d677265656e2e737667)](https://camo.githubusercontent.com/af813ae002de8e31bfd234a3d5eab4fe1963ed998df54d1f3c5e3e1fe334f7d6/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f636f7665726167652d3130302532352d677265656e2e737667)

With Phlags you can declare flagable enums to provide types with varying and multiple states. While depending on binary operations Phlags provides high performance and reliabilty.

Index
-----

[](#index)

- [Installation](#installation)
- [How to use](#how-to-use)
    - [Declaration](#declaration)
    - [General hints](#general-hints)
    - [Instantiation](#instantiation)
    - [Reading](#reading)
    - [Determination](#determination)
    - [Manipulation](#manipulation)
    - [Fluent Manipulation](#fluent-manipulation)
    - [String Representation](#string-representation)
    - [Traitful Extensions](#traitful-extensions)
        - [Conditional Manipulation](#conditional-manipulation)
- [Validation](#validation)
    - [Flagables](#flagables)
    - [Values](#values)

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

[](#installation)

Install the latest version with

```
$ composer require codekandis/phlags
```

How to use
----------

[](#how-to-use)

**Example: Simple permissions in a file system**

### Declaration

[](#declaration)

Declare a class extending the flagable base class [`AbstractFlagable`](./src/AbstractFlagable.php).

```
class Permissions extends AbstractFlagable
{
    public const READ    = 1;
    public const WRITE   = 2;
    public const EXECUTE = 4;
}
```

### General Hints

[](#general-hints)

In the context of manipulating the flagable the following values are supposed to be equal and can similarly passed to all methods of the flagable.

```
1
Permissions::READ
new Permissions( 1 )
new Permissions( Permission::READ )
new Permissions( 'READ' );
```

In the other hand the type restriction of PHP does not allow any combination of an integer value with a string with a flagable.

```
new Permission( 1 | 'READ' | new Permissions( READ ) )
```

### Instantiation

[](#instantiation)

You can easily instantiate your flagable in different ways.

```
// with the default flag 'Permissions::NONE' (inherited from `FlagableInterface::NONE`)
$permissions = new Permissions();

// with a single flag
$permissions = new Permissions( Permissions::READ );

// with multiple flags
$permissions = new Permissions( Permissions::READ | Permissions::WRITE );

// with another flagable
$permissions = new Permissions( new Permissions( Permissions::READ ) );

// with string representations
$permissions = new ( 'READ' );
$permissions = new ( 'READ|WRITE' );
$permissions = new ( 'READ|WRITE|EXECUTE' );

// with mixed string representations
$permissions = new ( '1' );
$permissions = new ( '1|2' );
$permissions = new ( '1|WRITE|4' );
```

### Reading

[](#reading)

You can read the value of the flagable in 2 different ways.

```
$permissions = new Permissions( Permissions::READ );
echo $permissions->getValue();  // 1
echo $permissions();            // 1
```

### Determination

[](#determination)

You can determine if one or more specific flags have been set.

```
$permissions = new Permissions( Permissions::READ | Permissions::WRITE );
$permissions->has( Permissions::READ );     // true
$permissions->has( Permissions::WRITE );    // true
$permissions->has( Permissions::EXECUTE );  // false
```

### Manipulation

[](#manipulation)

You can set, unset and switch flags.

```
$permissions = new Permissions();
$permissions->set( Permissions::READ );
$permissions->unset( Permissions::READ );
$permissions->switch( Permissions::READ );
$permissions->has( Permissions::READ );     // true
```

### Fluent Manipulation

[](#fluent-manipulation)

The base class [`AbstractFlagable`](./src/AbstractFlagable.php) implements the fluent interface. So the manipulation of the flagable can be chained.

```
$permissions = new Permissions();
$permissions->set( Permissions::READ )
            ->unset( Permissions::READ )
            ->switch( Permissions::READ )
            ->has( Permissions::READ );    // true
```

### String Representation

[](#string-representation)

A flagable can stringified in different ways with different outputs.

```
$permissions = new Permissions();
(string)$permissions->getValue();  // 0
(string)$permissions();            // 0
(string)$permissions;              // NONE
$permissions->__toString();        // NONE

$permissions = new Permissions( PERMISSIONS::READ | PERMISSIONS::EXECUTE );
(string)$permissions->getValue();  // 5
(string)$permissions();            // 5
(string)$permissions;              // READ|EXECUTE
$permissions->__toString();        // READ|EXECUTE
```

### Traitful Extensions

[](#traitful-extensions)

To keep the simplicity and performance Phlags provides [`Traitful Extensions`](./src/TraitfulExtensions). Instead of implementing a complex and heavyweight inheritance you can combine the extensions of your choice into the flagable of your needs.

```
class Permissions extends AbstractFlagable SomeTraitfulInterface
{
    use SomeTraitfulExtension;

    public const READ    = 1;
    public const WRITE   = 2;
    public const EXECUTE = 4;
}
```

#### Conditional Manipulation

[](#conditional-manipulation)

— [`ConditionalManipulationExtension`](./src/TraitfulExtensions/ConditionalManipulationExtension.php)

The Conditional Manipulation provides you with methods to set, unset and switch a flag value while a passed statement must evaluate to true.

```
$pathToFile = '/some-random-file.txt';
$permissions = new Permissions();
$permissions->ifSet( Permissions::DIRECTORY, is_dir( $pathToFile ) );
$permissions->has( Permissions::DIRECTORY );  // false

$pathToFile = '/some-random-directory';
$permissions = new Permissions();
$permissions->ifSet( Permissions::DIRECTORY, is_dir( $pathToDirectory ) );
$permissions->has( Permissions::DIRECTORY );  // true
```

Validation
----------

[](#validation)

### Flagables

[](#flagables)

While instantiating your very first flagable your flagable has to pass a one-time validation.

- all declared constants are an `unsigned integer`
- all constants are a power of 2
- there is no duplicates of any of the constant values
- there is no missing values, e. g. a flagable with a flags set `1, 2, 8` ist invalid, while the flag `4` is missing

If the flagable does not pass the validation an [`InvalidFlagableException`](src/Validation/InvalidFlagableException.php) will be thrown and you can retreive an array of detailed error messages of the validation.

```
try
{
    $permissions = new Permissions();
}
catch ( InvalidFlagableException $e )
{
    $errorMessages = $e->getErrorMessages();
}
```

### Values

[](#values)

A flag value passed to the methods of the flagable has to pass a validation on every method call.

- it is an `unsigned integer` less or equal than the maximum value of the called flagable
- it is a `string` representation of a flagable with an identic type as the type of the called flagable
- it is a flagable with an identic type as the type of the called flagable
- it does not exceeds the maximum flag value of the called flagable

If the value does not pass the validation an [`InvalidValueException`](src/Validation/InvalidValueException.php) will be thrown and you can retreive an array of detailed error messages of the validation.

```
try
{
    $permissions->set( $value );
}
catch ( InvalidValueException $e )
{
    $errorMessages = $e->getErrorMessages();
}
```

###  Health Score

41

—

FairBetter than 87% of packages

Maintenance61

Regular maintenance activity

Popularity17

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 100% 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 ~433 days

Recently: every ~541 days

Total

6

Last Release

245d ago

Major Versions

1.2.0 → 2.0.02019-11-20

2.0.0 → 3.0.02021-01-17

PHP version history (3 changes)1.0.0PHP &gt;=7.1

2.0.0PHP ^7.3

3.0.0PHP ^7.4

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/31327217?v=4)[Christian Ramelow](/maintainers/codekandis)[@codekandis](https://github.com/codekandis)

---

Top Contributors

[![codekandis](https://avatars.githubusercontent.com/u/31327217?v=4)](https://github.com/codekandis "codekandis (154 commits)")

---

Tags

enumsflagableflagsphpphpflagsenumsflagable

### Embed Badge

![Health badge](/badges/codekandis-phlags/health.svg)

```
[![Health](https://phpackages.com/badges/codekandis-phlags/health.svg)](https://phpackages.com/packages/codekandis-phlags)
```

PHPackages © 2026

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