8000 Merge branch 'release/v2.0.0' · Fanmade/laravel-bitwise-trait@e8a1abb · GitHub
[go: up one dir, main page]

Skip to content

Commit e8a1abb

Browse files
committed
Merge branch 'release/v2.0.0'
2 parents 924971e + b645532 commit e8a1abb

File tree

8 files changed

+2312
-35
lines changed

8 files changed

+2312
-35
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/vendor/
2+
/.idea/
3+
/.phpunit.result.cache

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_REC 8000 EIVED = 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:

composer.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,24 @@
44
"keywords": ["fanmade","laravel","bitwise","trait"],
55
"homepage": "https://github.com/Fanmade/laravel-bitwise-trait",
66
"license": "MIT",
7-
"version": "1.0.1",
7+
"version": "2.0.0",
88
"authors": [
99
{
1010
"name": "Benjamin Reuter",
1111
"email": "ben@reuterben.de",
12-
"homepage": "http://reuterben.de",
1312
"role": "Developer"
1413
}
1514
],
1615
"require": {
17-
"php": ">=5.4.0"
16+
"php": ">=7.4"
1817
},
1918
"autoload": {
2019
"psr-4": {
21-
"Fanmade\\Bitwise\\": "src"
20+
"Fanmade\\Bitwise\\": "src",
21+
"Fanmade\\Bitwise\\Tests\\": "tests"
2222
}
23+
},
24+
"require-dev": {
25+
"phpunit/phpunit": "^9.5"
2326
}
2427
}

0 commit comments

Comments
 (0)
0