1- ``` diff
2- - Work in progress, you probably shouldn't use it yet!
3- ```
4-
51# Laravel Bitwise Trait
62Simple trait to use bitwise operators on any class
73Inspired by http://php.net/manual/de/language.operators.bitwise.php#108679
84
95I just used it in Laravel so far, but you should be able to use it anyhwere else with minor modifications.
106
117## 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 :)
1415
1516## Usage
1617
@@ -29,7 +30,7 @@ $table->mediumInteger('status'); // 3 byte -> maximum of 24 different values
2930```
3031You get the idea. Most times you probably only need an unsigned tinyInteger :)
3132
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.
3334
3435Include the Trait in your model like this:
3536``` php
@@ -43,7 +44,8 @@ class Message extends Model
4344 use BitwiseFlagTrait;
4445```
4546
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.
4749``` php
4850const MESSAGE_SENT = 1; // BIT #1 of has the value 1
4951const 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
5355
5456To set a property, just call the function like this:
5557``` php
56- $this->setFlag('status', MESSAGE_SENT, true);
58+ $this->setFlag('status', self:: MESSAGE_SENT, true);
5759```
5860
5961To get a property, just call the function like this:
6062``` php
61- $sent = $this->getFlag('status', MESSAGE_SENT);
63+ $sent = $this->getFlag('status', self:: MESSAGE_SENT);
6264```
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.
6467
6568To make your life easier, I recommend to use custom getters and setters.
6669``` php
@@ -71,15 +74,15 @@ To make your life easier, I recommend to use custom getters and setters.
7174 */
7275 public function setSentAttribute($sent = true)
7376 {
74- return $this->setFlag('status', MESSAGE_SENT, $sent);
77+ return $this->setFlag('status', self:: MESSAGE_SENT, $sent);
7578 }
7679
7780 /**
7881 * @return bool
7982 */
8083 public function getSentAttribute()
8184 {
82- return $this->getFlag('status', MESSAGE_SENT);
85+ return $this->getFlag('status', self:: MESSAGE_SENT);
8386 }
8487
8588```
@@ -93,7 +96,7 @@ If you want to use the new field in scopes, you can do that like this:
9396 */
9497 public function scopeUnread($query)
9598 {
96- return $query->whereRAW('NOT status & ' . MESSAGE_READ);
99+ return $query->whereRAW('NOT status & ' . self:: MESSAGE_READ);
97100 }
98101
99102 /**
@@ -102,7 +105,7 @@ If you want to use the new field in scopes, you can do that like this:
102105 */
103106 public function scopeRead($query)
104107 {
105- return $query->where('status', '& ', MESSAGE_READ);
108+ return $query->where('status', '& ', self:: MESSAGE_READ);
106109 }
107110
108111```
0 commit comments