|
1 | | -# laravel-bitwise-trait |
| 1 | +# Laravel Bitwise Trait |
2 | 2 | Simple trait to use bitwise operators on any class |
| 3 | +Inspired by http://php.net/manual/de/language.operators.bitwise.php#108679 |
| 4 | + |
| 5 | +I just used it in Laravel so far, but you should be able to use it anyhwere else with minor modifications. |
| 6 | + |
| 7 | +## Installation |
| 8 | +Just put the file in app/Traits (create the folder if it doesn't exist yet) and you're good to go. |
| 9 | +You are free to place it anywhere else, just change the namespace accordingly. |
| 10 | + |
| 11 | +## Usage |
| 12 | + |
| 13 | +*This example is for a default Laravel (5.4) Model within the "App" namespace.* |
| 14 | +You need an (ideally unsigned) integer field in your database which will store the properties. |
| 15 | +The length does depend on the number of values you would like to store. You only need one bit per value, so it's 8 values for each byte, if the column is unsigned. |
| 16 | +Examples (based on laravel migrations): |
| 17 | +```php |
| 18 | +$table->tinyInteger('status'); // 1 byte -> maximum of 7 different values |
| 19 | +$table->unsignedTinyInteger('status'); // maximum of 8 different values |
| 20 | +$table->smallInteger('status'); // 2 byte -> maximum of 16 different values |
| 21 | +$table->unsignedSmallInteger('status'); // maximum of 17 different values |
| 22 | +$table->mediumInteger('status'); // 3 byte -> maximum of 24 different values |
| 23 | +``` |
| 24 | +You get the idea. Most times, you probably only need an unsigned tinyInteger :) |
| 25 | +There are only a few use-cases, but you can add as many fields as you like. |
| 26 | + |
| 27 | +Include the Trait in your model like this: |
| 28 | +```php |
| 29 | +<?php namespace App; |
| 30 | +use App\Traits\BitwiseFlagTrait; |
| 31 | + |
| 32 | +class Message extends Model |
| 33 | +{ |
| 34 | + |
| 35 | + use BitwiseFlagTrait; |
| 36 | +``` |
| 37 | + |
| 38 | +The best way to define your properties is via constants. |
| 39 | +```php |
| 40 | +const MESSAGE_SENT = 1; // BIT #1 of has the value 1 |
| 41 | +const MESSAGE_RECEIVED = 2; // BIT #2 of has the value 2 |
| 42 | +const MESSAGE_SEEN = 4; // BIT #3 of has the value 4 |
| 43 | +const MESSAGE_READ = 8; // BIT #4 of has the value 8 |
| 44 | +``` |
| 45 | + |
| 46 | +To set a property, just call the function like this: |
| 47 | +```php |
| 48 | +$this->setFlag('status', MESSAGE_SENT, true); |
| 49 | +``` |
| 50 | + |
| 51 | +To get a property, just call the function like this: |
| 52 | +```php |
| 53 | +$sent = $this->getFlag('status', MESSAGE_SENT); |
| 54 | +``` |
| 55 | +The first parameter is always the field you set in the database. |
| 56 | + |
| 57 | +To make your life easier, I recommend to use custom getters and setters. |
| 58 | +```php |
| 59 | + |
| 60 | + /** |
| 61 | + * @param bool $sent |
| 62 | + * @return bool |
| 63 | + */ |
| 64 | + public function setSentAttribute($sent = true) |
| 65 | + { |
| 66 | + return $this->setFlag('status',MESSAGE_SENT, $sent); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @return bool |
| 71 | + */ |
| 72 | + public function getSentAttribute() |
| 73 | + { |
| 74 | + return $this->getFlag('status', MESSAGE_SENT); |
| 75 | + } |
| 76 | + |
| 77 | +``` |
| 78 | + |
| 79 | +## Scopes |
| 80 | +If you want to use the new field in scopes, you can do that like this: |
| 81 | +```php |
| 82 | + /** |
| 83 | + * @param Builder $query |
| 84 | + * @return Builder |
| 85 | + */ |
| 86 | + public function scopeUnread($query) |
| 87 | + { |
| 88 | + return $query->whereRAW('NOT status & ' . MESSAGE_READ); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * @param Builder $query |
| 93 | + * @return Builder |
| 94 | + */ |
| 95 | + public function scopeRead($query) |
| 96 | + { |
| 97 | + return $query->where('status', '&', MESSAGE_READ); |
| 98 | + } |
| 99 | + |
| 100 | +``` |
0 commit comments