Download the PHP package alazzi-az/php-bitmask without Composer
On this page you can find all versions of the php package alazzi-az/php-bitmask. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download alazzi-az/php-bitmask
More information about alazzi-az/php-bitmask
Files in alazzi-az/php-bitmask
Package php-bitmask
Short Description php package to work with bitmasking
License MIT
Homepage https://github.com/alazzi-az/php-bitmask
Informations about the package php-bitmask
PHP package to work with bitmasking
PHP Bitmask is a powerful package for managing bitmask operations in php applications. It provides an elegant and intuitive interface for reading, validating, converting bitmasks, and casting them to and from enum values, enabling developers to leverage bitmasking techniques efficiently.
Features
- Bitmask Reading: Easily retrieve active bits from a given bitmask.
- Bitmask Validation: Ensure that provided bits and masks are valid, including checks for single-bit settings.
- Bitmask Conversion: Convert indices to bitmasks and vice versa, along with conversions to binary string representations.
- Casting for Masks and Enums: Automatically handle the casting of bitmask values to and from enum types, providing a seamless experience when working with enumerated bitmasks.
Installation
You can install the package via composer:
Usage
Bitmask Class
The Bitmask
class provides a collection of static methods for handling bitmask operations.
It includes functionality for creating bitmask handlers, converting between indexes and bitmasks,
and validating bit values.
Creating an Enum Bitmask Handler
You can create a bitmask handler that works with an enum by using the enumBitmaskHandler
method.
Example
Here’s the complete documentation, including the missing methods for both the EnumBitmaskFactory
and Bitmask
classes.
EnumBitmaskFactory
The EnumBitmaskFactory
provides methods to create and manipulate bitmask handlers for enum values.
Example Usage
Here EnumBitmaskFactory Methods Overview
-
create(string $enum, UnitEnum ...$bits): EnumBitmaskHandler
Creates a handler with the specified enum cases. -
createWithMask(string $enum, int $mask): EnumBitmaskHandler
Creates a handler with a predefined bitmask. -
createNone(string $enum): EnumBitmaskHandler
Creates a handler with an empty bitmask (no bits set). -
createAll(string $enum): EnumBitmaskHandler
Creates a handler with all enum cases active. -
without(string $enum, UnitEnum ...$bits): EnumBitmaskHandler
Creates a handler with all cases except the specified ones. none(string $enum): EnumBitmaskHandler
Same ascreateNone
—creates a handler with no bits set.
Here Extra Bitmask Class Methods
1. Exposing BitmaskConverter Methods
-
indexToBitMask(int $index): int
Converts an index to its corresponding bitmask.
Example: -
bitMaskToIndex(int $mask): int
Converts a bitmask to its index.
Example: -
getEnumMaxBitValue(string $enum): int
Retrieves the maximum bit value for the given enum. -
bitMaskToArray(int $mask): array
Converts a bitmask into an array of active bit values.
Example: arrayToBitMask(array $bits): int
Converts an array of bit values to a bitmask.
Example:
2. Exposing BitmaskReader Methods
-
getActiveBits(int $bitmask): array
Retrieves the active bit values from a bitmask.
Example: -
getActiveIndexes(int $bitmask): array
Retrieves the active bit indexes from a bitmask.
Example: -
countActiveBits(int $bitmask): int
Counts the number of active bits in a bitmask.
Example: -
getMostSignificantBitIndex(int $bitmask): int
Returns the index of the most significant active bit.
Example: -
getLeastSignificantBitIndex(int $bitmask): int
Returns the index of the least significant active bit.
Example: convertToBinaryString(int $bitmask): string
Converts a bitmask to its binary string representation.
Example:
3. Exposing BitmaskValidator Methods
-
validateBit(int $bit): void
Validates a single bit to ensure it's valid.
Example: validateBits(array $bits): void
Validates an array of bits to ensure all are valid.
Example:
BitmaskHandler
The BitmaskHandler
class provides an interface for managing bitmask operations in a Laravel application. It allows for
the manipulation of bitmasks through various methods, including adding, deleting, and checking for specific bits.
EnumBitmaskHandler
The EnumBitmaskHandler
class provides an interface for managing bitmask operations specific to enumerations in a
Laravel application. It allows manipulation of bitmasks using enums, enabling you to add, delete, and check for specific
bits represented by these enums.
Methods Overview
-
remove(UnitEnum ...$bits): self
- Removes specified bits from the current instance.
-
add(UnitEnum ...$bits): self
- Adds specified bits to the current instance.
-
getValue(): int
- Returns the current mask value as an integer.
-
toArray(): array
- Returns an array representation of the current mask, indicating which bits are set.
has(UnitEnum ...$bits): bool
- Checks if the specified bits are set in the current mask.
Example Usage
Here's an example that illustrates how to use the EnumBitmaskHandler
class:
Note on toArray
Method
If you want to customize the keys in the resulting array from the toArray
method, consider implementing
the Alazziaz\Bitmask\Contracts\MaskableEnum
interface for your enum. You can define a toMaskKey
method to specify
custom keys for each enum value. For example:
With this approach, the toArray
method in EnumBitmaskHandler
can utilize the toMaskKey
method to generate a more
descriptive and meaningful array representation of the current mask.
Testing
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Credits
- Mohammed Azman
- All Contributors
License
The MIT License (MIT). Please see License File for more information.