https://github.com/aw-studio/laravel-bitflags
Store bitmask in database, cast bitmask for auto de-/encoding bitflags.
https://github.com/aw-studio/laravel-bitflags
bitflag bitflags bitmask bitmasking bitwise laravel
Last synced: 5 days ago
JSON representation
Store bitmask in database, cast bitmask for auto de-/encoding bitflags.
- Host: GitHub
- URL: https://github.com/aw-studio/laravel-bitflags
- Owner: aw-studio
- Created: 2021-07-26T21:03:54.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2021-07-30T08:32:51.000Z (over 4 years ago)
- Last Synced: 2025-12-26T19:40:14.754Z (about 2 months ago)
- Topics: bitflag, bitflags, bitmask, bitmasking, bitwise, laravel
- Language: PHP
- Homepage:
- Size: 19.5 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Laravel Bitflags
Store and receive bitflags from a single database column.
## Installation
```bash
composer require aw-studio/bitflags
```
## Usage
Imagine you want to store multiple status flags in a single `status(int)` column of your `Email` model.
This can be achieved using bitwise operations to create a representative `bitmask`.
In order to enable bitwise operations bitflags `MUST` all be powers of two (1,2,4,8,16 …).
You should also make shure to properly cast the column as `Bitflags::class`.
```php
class Email extends Model
{
// Email status flags, all powers of 2
public const SENT = 1;
public const RECEIVED = 2;
public const SEEN = 4;
public const READ = 8;
protected $fillable = ['status'];
public $casts = [
'status' => Bitflags::class
];
}
```
### Adding a flag to a bitmask
Adding a `bitflag` to a bitmask can be achieved using the `addBitflag()` helper:
```php
public function markRead()
{
$this->update([
'status' => addBitflag(self::READ, $this->status)
]);
}
```
You can also add multiple flags at once:
```php
$this->update([
'status' => addBitflag([self::READ, self::SEEN], $this->status)
]);
```
### Removing a flag from a bitmask
Removing a `bitflag` from a bitmask can be achieved using the `removeBitflag()` helper:
```php
public function markUnread()
{
$this->update([
'status' => removeBitflag(self::READ, $this->status)
]);
}
```
Remove multiple flags at once:
```php
$this->update([
'status' => removeBitflag([self::READ, self::SEEN], $this->status)
]);
```
### Query bitflags
To check if bitflags are included in a bitmask you may use the following query methods:
```php
public function scopeRead($query)
{
return $this->whereBitflag('status', self::READ);
}
public function scopeUnread($query)
{
return $this->whereBitflagNot('status', self::READ);
}
public function scopeSeenOrRead($query)
{
return $this->whereBitflagIn('status', [self::READ, self::SEEN]);
}
public function scopeSeenAndRead($query)
{
return $this->whereBitflags('status', [self::READ, self::SEEN]);
}
```
## Accessors
In order to get single flag attributes you can prepare accessors as follows:
```php
protected $appends = ['read'];
public function getReadAttribute()
{
return inBitmask(self::READ, $this->status);
}
```