Skip to content

Commit 1ba8516

Browse files
committed
📝 Updated and improved readme
1 parent 924971e commit 1ba8516

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

README.md

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
# Laravel Bitwise Trait
2-
Simple trait to use bitwise operators on any class
2+
A simple trait to use bitwise operators on any class
33
Inspired by http://php.net/manual/de/language.operators.bitwise.php#108679
44

5-
I just used it in Laravel so far, but you should be able to use it anyhwere else with minor modifications.
5+
Updated after reading this blog post: https://aaronfrancis.com/2021/bitmasking-in-laravel-and-mysql
6+
7+
I just used it in Laravel so far, but you should be able to use it anywhere else with minor modifications.
8+
9+
## PHP Version
10+
Version v2.* requires PHP 8+. If you're stuck to an older version, please use v1.*
611

712
## Installation
813

@@ -34,23 +39,40 @@ There are only a few use-cases for more than one database field, but you can add
3439

3540
Include the Trait in your model like this:
3641
```php
37-
<?php namespace App;
42+
<?php
43+
44+
namespace App;
3845

3946
use Fanmade\Bitwise\BitwiseFlagTrait;
4047

4148
class Message extends Model
4249
{
43-
4450
use BitwiseFlagTrait;
4551
```
4652

4753
The best way to define your properties is via constants directly in the model.
48-
You're of course free to use config varibales or whatever you prefer.
54+
You're of course free to use config variables or whatever you prefer.
4955
```php
50-
const MESSAGE_SENT = 1; // BIT #1 of has the value 1
56+
const MESSAGE_SENT = 1; // BIT #1 of has the value 1
5157
const MESSAGE_RECEIVED = 2; // BIT #2 of has the value 2
52-
const MESSAGE_SEEN = 4; // BIT #3 of has the value 4
53-
const MESSAGE_READ = 8; // BIT #4 of has the value 8
58+
const MESSAGE_SEEN = 4; // BIT #3 of has the value 4
59+
const MESSAGE_READ = 8; // BIT #4 of has the value 8
60+
```
61+
62+
This alternative syntax may be easier to read:
63+
```php
64+
const MESSAGE_SENT = 1 << 0;
65+
const MESSAGE_RECEIVED = 1 << 1;
66+
const MESSAGE_SEEN = 1 << 2;
67+
const MESSAGE_READ = 1 << 3;
68+
```
69+
70+
Or directly in binary notation:
71+
```php
72+
const MESSAGE_SENT = 0b00000001;
73+
const MESSAGE_RECEIVED = 0b00000010;
74+
const MESSAGE_SEEN = 0b00000100;
75+
const MESSAGE_READ = 0b00001000;
5476
```
5577

5678
To set a property, just call the function like this:

0 commit comments

Comments
 (0)