|Classes of Reinder Reinders||PHP Binary Flags||README.md||Download|
[![Build Status][build-icon]][build-link] [![Coverage Status][coverage-icon]][coverage-link]
With this class you can easily add flags to your projects.
The number of flags you can use is limited to the architecture of your system, e.g.: 32 flags on a 32-bit system or 64 flags on 64-bit system. To store 64-bits flags in a database, you will need to store it as UNSIGNED BIGINT in MySQL or an equivalant in your datastorage.
This package also comes with a trait which you can use to implement binary flags directly in your own class.
To install this package simply run the following command in the root of your project.
The following methods can be used:
Overwrite the current mask. This can be passed as first argument in the constructor.
Retrieve the current mask.
Set a callback function which is called when the mask changes. This can be passed as second argument in the constructor.
getFlagNames([int $mask, [bool $asArray=false]])
Give the name(s) for the given
Adds one or multiple flags to the current mask.
Removes one or multiple flags from the current mask.
checkFlag(int $flag, [bool $checkAll=true]): bool
Check if given flag(s) are set in the current mask.
By default it will check all bits in the given flag.
When you want to match any of the given flags set
checkAnyFlag(int $mask): bool
_Since: v1.0.1_ \
For you convenient I've added an alias to checkFlag with
_Since: v1.2.0_ \ Returns the number of flags that have been set.
_Since: v1.2.0_ \
Return a value that can be encoded by json_encode() in the form of
The following static methods can be used:
_Since: v1.1.0_ \
Return all the flags with their names as an array, using their flag mask as key.
This method can also be overloaded to return custom names for the flags,
which will be used by the
_Since: v1.1.0_ \ Return mask of all the flags together
_Since: v1.2.0_ \ You can treat a BinaryFlags object as an iterable, where each iteration will return the next bit value that has been set including its description (or the name of the constant representing the bit value).
Below some example usage code
Usage with multiple flags
Flag names example
_By default the flag names are based on the constant names_
Custom flag names example
If you want custom flag names that are not equal to the constant names, you can override these with
Example usage with Eloquent models
For bugs or feature requests feel free to contact me or submit an issue or pull request. Or you can support me by buying me a coffee:
[![Buy me a coffee][buymeacoffee-icon]][buymeacoffee-link]
[build-icon]: https://travis-ci.org/reinder83/binary-flags.svg?branch=master [build-link]: https://travis-ci.org/reinder83/binary-flags [coverage-icon]: https://coveralls.io/repos/github/reinder83/binary-flags/badge.svg?branch=master&v=1 [coverage-link]: https://coveralls.io/github/reinder83/binary-flags?branch=master [buymeacoffee-icon]: https://www.buymeacoffee.com/assets/img/guidelines/download-assets-sm-2.svg [buymeacoffee-link]: https://www.buymeacoffee.com/reinder83