1
- ``` diff
2
- - Work in progress, you probably shouldn't use it yet!
3
- ```
4
-
5
1
# Laravel Bitwise Trait
6
2
Simple trait to use bitwise operators on any class
7
3
Inspired by http://php.net/manual/de/language.operators.bitwise.php#108679
8
4
9
5
I just used it in Laravel so far, but you should be able to use it anyhwere else with minor modifications.
10
6
11
7
## Installation
12
- Just put the file in app/Traits (create the folder if it doesn't exist yet) and you're good to go.
13
- You are free to place it anywhere else, just change the namespace accordingly.
8
+
9
+ You can install the package via composer:
10
+
11
+ ``` bash
12
+ composer require fanmade/laravel-bitwise-trait
13
+ ```
14
+ That's all, no provider registration needed :)
14
15
15
16
## Usage
16
17
@@ -29,7 +30,7 @@ $table->mediumInteger('status'); // 3 byte -> maximum of 24 different values
29
30
```
30
31
You get the idea. Most times you probably only need an unsigned tinyInteger :)
31
32
32
- There are only a few use-cases, but you can add as many fields as you like.
33
+ There are only a few use-cases for more than one database field , but you can add as many fields as you like.
33
34
34
35
Include the Trait in your model like this:
35
36
``` php
@@ -43,7 +44,8 @@ class Message extends Model
43
44
use BitwiseFlagTrait;
44
45
```
45
46
46
- The best way to define your properties is via constants.
47
+ 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.
47
49
``` php
48
50
const MESSAGE_SENT = 1; // BIT #1 of has the value 1
49
51
const MESSAGE_RECEIVED = 2; // BIT #2 of has the value 2
@@ -53,14 +55,15 @@ const MESSAGE_READ = 8; // BIT #4 of has the value 8
53
55
54
56
To set a property, just call the function like this:
55
57
``` php
56
- $this->setFlag('status', MESSAGE_SENT, true);
58
+ $this->setFlag('status', self:: MESSAGE_SENT, true);
57
59
```
58
60
59
61
To get a property, just call the function like this:
60
62
``` php
61
- $sent = $this->getFlag('status', MESSAGE_SENT);
63
+ $sent = $this->getFlag('status', self:: MESSAGE_SENT);
62
64
```
63
- The first parameter is always the field you set in the database.
65
+ The first parameter * ('status' in the example)* is always the column you set in the database.
66
+ Maybe you want to define that in a constant or variable.
64
67
65
68
To make your life easier, I recommend to use custom getters and setters.
66
69
``` php
@@ -71,15 +74,15 @@ To make your life easier, I recommend to use custom getters and setters.
71
74
*/
72
75
public function setSentAttribute($sent = true)
73
76
{
74
- return $this->setFlag('status', MESSAGE_SENT, $sent);
77
+ return $this->setFlag('status', self:: MESSAGE_SENT, $sent);
75
78
}
76
79
77
80
/**
78
81
* @return bool
79
82
*/
80
83
public function getSentAttribute()
81
84
{
82
- return $this->getFlag('status', MESSAGE_SENT);
85
+ return $this->getFlag('status', self:: MESSAGE_SENT);
83
86
}
84
87
85
88
```
@@ -93,7 +96,7 @@ If you want to use the new field in scopes, you can do that like this:
93
96
*/
94
97
public function scopeUnread($query)
95
98
{
96
- return $query->whereRAW('NOT status & ' . MESSAGE_READ);
99
+ return $query->whereRAW('NOT status & ' . self:: MESSAGE_READ);
97
100
}
98
101
99
102
/**
@@ -102,7 +105,7 @@ If you want to use the new field in scopes, you can do that like this:
102
105
*/
103
106
public function scopeRead($query)
104
107
{
105
- return $query->where('status', '& ', MESSAGE_READ);
108
+ return $query->where('status', '& ', self:: MESSAGE_READ);
106
109
}
107
110
108
111
```
0 commit comments