Skip to content

Commit f44a27c

Browse files
authored
Merge pull request #2 from reinder83/development
Version 0.5.0
2 parents fa6f5d1 + e4a62ec commit f44a27c

File tree

7 files changed

+282
-88
lines changed

7 files changed

+282
-88
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ language: php
22

33
sudo: false
44

5+
dist: trusty
6+
57
cache:
68
directories:
79
- $HOME/.composer/cache

README.md

Lines changed: 191 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,195 @@ With this class you can easily add flags to your models.
66

77
The number of flags you can use is limited to the architecture of your system, e.g.: 32 flags on a 32-bit system or 64 flags on 64-bit system.
88
To store 64-bits flags in a database, you will need to store it as UNSIGNED BIGINT in MySQL or an equivalant in your datastorage.
9-
9+
10+
This package also comes with a trait which use can use to implement binary flags directly in your own class.
11+
12+
## Installing
13+
To install this package simply run the following command in the root of your project.
14+
```
15+
composer require reinder83\binary-flags
16+
```
17+
18+
## Methods
19+
The following methods can be used:
20+
21+
##### setMask(int $mask)
22+
Overwrite the current mask.
23+
This can be passed as first argument in the constructor.
24+
25+
##### getMask()
26+
Retrieve the current mask.
27+
28+
##### setOnModifyCallback(callable $onModify)
29+
Set a callback function which is called when the mask changes.
30+
This can be passed as second argument in the contructor.
31+
32+
##### getFlagNames([int $mask, [bool $asArray=false]])
33+
Give the name(s) for the given `$mask` or the current mask when omitted.
34+
When `$asArray` is `true` the method will return an array with the names,
35+
otherwise an comma separated string will be returned (default).
36+
37+
##### addFlag(int $flag)
38+
Adds one or multiple flags the to current mask.
39+
40+
##### removeFlag(int $flag)
41+
Removes one or multiple flags from the current mask.
42+
43+
##### checkFlag(int $flag, [bool $checkAll=true])
44+
Check if given flag(s) are set in the current mask.
45+
By default it will check all bits in the given flag.
46+
When you want to match any of the given flags set `$checkAll` to `false`.
47+
1048
## Example usage
49+
50+
Below some example usage code
51+
52+
##### Create classes
53+
```php
54+
// example classes which the following examples will refer to
55+
use Reinder83\BinaryFlags\BinaryFlags;
56+
use Reinder83\BinaryFlags\Bits;
57+
58+
class ExampleFlags extends BinaryFlags
59+
{
60+
const FOO = Bits::BIT_1;
61+
const BAR = Bits::BIT_2;
62+
const BAZ = Bits::BIT_3;
63+
}
64+
```
65+
66+
##### Simple usage
67+
```php
68+
$exampleFlags = new ExampleFlags();
69+
70+
// add BAR flag
71+
$exampleFlags->addFlag(ExampleFlags::BAR);
72+
73+
var_export($exampleFlags->checkFlag(ExampleFlags::FOO));
74+
// false
75+
var_export($exampleFlags->checkFlag(ExampleFlags::BAR));
76+
// true
77+
78+
// remove BAR flag
79+
$exampleFlags->removeFlag(ExampleFlags::BAR);
80+
81+
var_export($exampleFlags->checkFlag(ExampleFlags::BAR));
82+
// false
83+
```
84+
85+
##### Usage with multiple flags
86+
```php
87+
$exampleFlags = new ExampleFlags();
88+
89+
// add FOO and BAR
90+
$exampleFlags->addFlag(ExampleFlags::FOO | ExampleFlags::BAR);
91+
92+
var_export($exampleFlags->checkFlag(ExampleFlags::FOO));
93+
// true
94+
95+
var_export($exampleFlags->checkFlag(ExampleFlags::FOO | ExampleFlags::BAZ));
96+
// false because BAZ is not set
97+
98+
var_export($exampleFlags->checkFlag(ExampleFlags::FOO | ExampleFlags::BAR));
99+
// true because both flags are set
100+
101+
var_export($exampleFlags->checkFlag(ExampleFlags::FOO | ExampleFlags::BAZ, true));
102+
// true because one of the flags is set (FOO)
103+
```
104+
105+
##### Flag names example
106+
_By default the flag names are based on the constant names_
107+
```php
108+
$exampleFlags = new ExampleFlags();
109+
110+
$exampleFlags->addFlag(ExampleFlags::FOO | ExampleFlags::BAR | ExampleFlags::BAZ);
111+
var_export($exampleFlags->getFlagNames());
112+
// 'Foo, Bar, Baz'
113+
114+
// null will force current mask
115+
var_export($exampleFlags->getFlagNames(null, true));
116+
/*
117+
array (
118+
0 => 'Foo',
119+
1 => 'Bar',
120+
2 => 'Baz',
121+
)
122+
*/
123+
124+
// get mask of given mask
125+
var_export($exampleFlags->getFlagNames(ExampleFlags::FOO | ExampleFlags::BAR));
126+
// 'Foo, Bar'
127+
```
128+
129+
##### Custom flag names example
130+
If you want custom flag names that are not equal to the constant names, you can override these with `getAllFlags()`
131+
132+
```php
133+
class ExampleFlagsWithNames extends BinaryFlags
134+
{
135+
const FOO = Bits::BIT_1;
136+
const BAR = Bits::BIT_2;
137+
const BAZ = Bits::BIT_3;
138+
139+
public static function getAllFlags()
140+
{
141+
return [
142+
static::FOO => 'My foo description',
143+
static::BAR => 'My bar description',
144+
static::BAZ => 'My baz description',
145+
];
146+
}
147+
}
148+
149+
$exampleFlags = new ExampleFlagsWithNames();
150+
151+
$exampleFlags->addFlag(ExampleFlags::FOO | ExampleFlags::BAR | ExampleFlags::BAZ);
152+
153+
// null will force current mask
154+
var_export($exampleFlags->getFlagNames(null, true));
155+
/*
156+
array (
157+
0 => 'My foo description',
158+
1 => 'My bar description',
159+
2 => 'My baz description',
160+
)
161+
*/
162+
```
163+
164+
##### Example usage with Eloquent models
165+
166+
```php
167+
use Illuminate\Database\Eloquent\Model;
168+
169+
class Test extends Model
170+
{
171+
/**
172+
* Retrieve flags
173+
* @return ExampleFlags
174+
*/
175+
public function getFlagsAttribute()
176+
{
177+
static $flags = null;
178+
if ($flags === null) {
179+
$model = $this;
180+
$flags = new ExampleFlags(
181+
$this->attributes['flags'], // set current flags mask
182+
function (ExampleFlags $flags) use ($model) { // set callback function
183+
// update the flags in this model
184+
$model->flags = $flags->getMask();
185+
}
186+
);
187+
}
188+
return $flags;
189+
}
190+
}
191+
192+
// retrieve object from DB
193+
$test = Test::find(1);
194+
195+
// do binary operations on the flags class as described earlier
196+
$test->flags->checkFlag(ExampleFlag::FOO);
197+
```
198+
199+
## Support
200+
For bugs or feature requests feel free to contact me or submit an issue or pull request.

build/phpunit.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
colors="true"
44
backupGlobals="false"
55
backupStaticAttributes="false"
6-
strict="true"
76
verbose="true">
87
<testsuites>
98
<testsuite name="BinaryFlags Tests">

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"name": "reinder83/binary-flags",
33
"description": "Useful class for binary operations",
4-
"keywords": ["binary", "flags", "bitwise", "bitwiser", "operations"],
4+
"keywords": ["binary", "flags", "class", "trait", "eloquent", "model", "bitwise", "bitwiser", "operations"],
5+
"homepage": "https://github.com/reinder83/binary-flags",
56
"minimum-stability": "stable",
67
"license": "MIT",
78
"authors": [

0 commit comments

Comments
 (0)