Recommend this page to a friend! |
Classes of Reinder Reinders | PHP Binary Flags | README.md | Download |
|
![]() [![Build Status][build-icon]][build-link] [![Coverage Status][coverage-icon]][coverage-link] BinaryFlagsWith 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. InstallingTo install this package simply run the following command in the root of your project.
MethodsThe following methods can be used: setMask(int $mask)Overwrite the current mask. This can be passed as first argument in the constructor. getMask(): intRetrieve the current mask. setOnModifyCallback(callable $onModify)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 addFlag(int $flag)Adds one or multiple flags to the current mask. removeFlag(int $flag)Removes one or multiple flags from the current mask. checkFlag(int $flag, [bool $checkAll=true]): boolCheck 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 count(): int_Since: v1.2.0_ \ Returns the number of flags that have been set. jsonSerialize(): mixed_Since: v1.2.0_ \
Return a value that can be encoded by json_encode() in the form of Static MethodsThe following static methods can be used: getAllFlags(): array_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 getAllFlagsMask(): int_Since: v1.1.0_ \ Return mask of all the flags together Iteration_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). Example usageBelow some example usage code Create classes
Simple usage
Usage with multiple flags
Flag names example_By default the flag names are based on the constant names_
Custom flag names exampleIf you want custom flag names that are not equal to the constant names, you can override these with
Example usage with Eloquent models
SupportFor 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 |